-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hcledit to structure rewriting logic
- Loading branch information
1 parent
8cde077
commit 2d8144a
Showing
6 changed files
with
987 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/minamijoyo/tfedit/filter/awsv4upgrade" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func init() { | ||
RootCmd.AddCommand(newAWSV4UpgradeCmd()) | ||
} | ||
|
||
func newAWSV4UpgradeCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "aws_v4_upgrade", | ||
Short: "Upgrade resources to AWS provider v4", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
|
||
cmd.AddCommand( | ||
newAWSV4UpgradeAWSS3BucketCmd(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func newAWSV4UpgradeAWSS3BucketCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "aws_s3_bucket", | ||
Short: "Upgrade aws_s3_bucket to AWS provider v4", | ||
Long: "Upgrade aws_s3_bucket to AWS provider v4", | ||
RunE: runAWSV4UpgradeAWSS3BucketCmd, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runAWSV4UpgradeAWSS3BucketCmd(cmd *cobra.Command, args []string) error { | ||
if len(args) != 0 { | ||
return fmt.Errorf("expected no argument, but got %d arguments", len(args)) | ||
} | ||
|
||
file := viper.GetString("file") | ||
update := viper.GetBool("update") | ||
|
||
filter := awsv4upgrade.NewAWSS3BucketFilter() | ||
c := newDefaultClient(cmd) | ||
return c.Edit(file, update, filter) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/minamijoyo/hcledit/editor" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// RootCmd is a top level command instance | ||
var RootCmd = &cobra.Command{ | ||
Use: "tfedit", | ||
Short: "A command line editor for Terraform", | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
|
||
func init() { | ||
// set global flags | ||
flags := RootCmd.PersistentFlags() | ||
flags.StringP("file", "f", "-", "A path of input file") | ||
flags.BoolP("update", "u", false, "Update files in-place") | ||
_ = viper.BindPFlag("file", flags.Lookup("file")) | ||
_ = viper.BindPFlag("update", flags.Lookup("update")) | ||
|
||
setDefaultStream(RootCmd) | ||
} | ||
|
||
func setDefaultStream(cmd *cobra.Command) { | ||
cmd.SetIn(os.Stdin) | ||
cmd.SetOut(os.Stdout) | ||
cmd.SetErr(os.Stderr) | ||
} | ||
|
||
func newDefaultClient(cmd *cobra.Command) editor.Client { | ||
o := &editor.Option{ | ||
InStream: cmd.InOrStdin(), | ||
OutStream: cmd.OutOrStdout(), | ||
ErrStream: cmd.ErrOrStderr(), | ||
} | ||
return editor.NewClient(o) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/minamijoyo/hcledit/editor" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
// AWSS3BucketFilter is a filter implementation for upgrading configurations of | ||
// aws_s3_bucket to AWS provider v4. | ||
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#s3-bucket-refactor | ||
type AWSS3BucketFilter struct { | ||
} | ||
|
||
var _ editor.Filter = (*AWSS3BucketFilter)(nil) | ||
|
||
// NewAWSS3BucketFilter creates a new instance of AWSS3BucketFilter. | ||
func NewAWSS3BucketFilter() editor.Filter { | ||
return &AWSS3BucketFilter{} | ||
} | ||
|
||
// Filter upgrades configurations of aws_s3_bucket to AWS provider v4. | ||
func (f *AWSS3BucketFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error) { | ||
blocks := inFile.Body().Blocks() | ||
for _, b := range blocks { | ||
if b.Type() != "resource" { | ||
continue | ||
} | ||
|
||
labels := b.Labels() | ||
if len(labels) == 2 && labels[0] != "aws_s3_bucket" { | ||
continue | ||
} | ||
bName := labels[1] | ||
|
||
if b.Body().GetAttribute("acl") != nil { | ||
inFile.Body().AppendNewline() | ||
newblock := inFile.Body().AppendNewBlock("resource", []string{"aws_s3_bucket_acl", bName}) | ||
newblock.Body().SetAttributeTraversal("bucket", hcl.Traversal{ | ||
hcl.TraverseRoot{Name: "aws_s3_bucket"}, | ||
hcl.TraverseAttr{Name: bName}, | ||
hcl.TraverseAttr{Name: "id"}, | ||
}) | ||
newblock.Body().SetAttributeValue("acl", cty.StringVal("private")) | ||
b.Body().RemoveAttribute("acl") | ||
} | ||
|
||
if nested := b.Body().FirstMatchingBlock("logging", []string{}); nested != nil { | ||
inFile.Body().AppendNewline() | ||
newblock := inFile.Body().AppendNewBlock("resource", []string{"aws_s3_bucket_logging", bName}) | ||
newblock.Body().SetAttributeTraversal("bucket", hcl.Traversal{ | ||
hcl.TraverseRoot{Name: "aws_s3_bucket"}, | ||
hcl.TraverseAttr{Name: bName}, | ||
hcl.TraverseAttr{Name: "id"}, | ||
}) | ||
newblock.Body().AppendUnstructuredTokens(nested.Body().BuildTokens(nil)) | ||
b.Body().RemoveBlock(nested) | ||
} | ||
} | ||
|
||
return inFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
module github.com/minamijoyo/tf-aws-v4-upgrade-s3 | ||
module github.com/minamijoyo/tfedit | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/hashicorp/hcl/v2 v2.11.1 | ||
github.com/hashicorp/logutils v1.0.0 | ||
github.com/minamijoyo/hcledit v0.2.3 | ||
github.com/spf13/cobra v1.3.0 | ||
github.com/spf13/viper v1.10.1 | ||
github.com/zclconf/go-cty v1.8.0 | ||
) | ||
|
||
require ( | ||
github.com/agext/levenshtein v1.2.1 // indirect | ||
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect | ||
github.com/google/go-cmp v0.3.1 // indirect | ||
github.com/fsnotify/fsnotify v1.5.1 // indirect | ||
github.com/google/go-cmp v0.5.6 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||
github.com/magiconair/properties v1.8.5 // indirect | ||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect | ||
golang.org/x/text v0.3.5 // indirect | ||
github.com/mitchellh/mapstructure v1.4.3 // indirect | ||
github.com/pelletier/go-toml v1.9.4 // indirect | ||
github.com/spf13/afero v1.6.0 // indirect | ||
github.com/spf13/cast v1.4.1 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/subosito/gotenv v1.2.0 // indirect | ||
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
gopkg.in/ini.v1 v1.66.2 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
) |
Oops, something went wrong.