Skip to content

Commit

Permalink
Split filters by argument
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Feb 13, 2022
1 parent 2d8144a commit b05403a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 42 deletions.
49 changes: 7 additions & 42 deletions filter/awsv4upgrade/aws_s3_bucket.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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
// AWSS3BucketFilter is a filter implementation for upgrading arguments 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 {
Expand All @@ -20,44 +18,11 @@ func NewAWSS3BucketFilter() editor.Filter {
return &AWSS3BucketFilter{}
}

// Filter upgrades configurations of aws_s3_bucket to AWS provider v4.
// Filter upgrades arguments 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
m := editor.NewMultiFilter([]editor.Filter{
NewAWSS3BucketACLFilter(),
NewAWSS3BucketLoggingFilter(),
})
return m.Filter(inFile)
}
51 changes: 51 additions & 0 deletions filter/awsv4upgrade/aws_s3_bucket_acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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"
)

// AWSS3BucketACLFilter is a filter implementation for upgrading the acl
// argument of aws_s3_bucket.
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#acl-argument
type AWSS3BucketACLFilter struct {
}

var _ editor.Filter = (*AWSS3BucketACLFilter)(nil)

// NewAWSS3BucketACLFilter creates a new instance of AWSS3BucketACLFilter.
func NewAWSS3BucketACLFilter() editor.Filter {
return &AWSS3BucketACLFilter{}
}

// Filter upgrades the acl argument of aws_s3_bucket.
func (f *AWSS3BucketACLFilter) 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")
}
}

return inFile, nil
}
50 changes: 50 additions & 0 deletions filter/awsv4upgrade/aws_s3_bucket_logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package awsv4upgrade

import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/minamijoyo/hcledit/editor"
)

// AWSS3BucketLoggingFilter is a filter implementation for upgrading the
// logging argument of aws_s3_bucket.
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#logging-argument
type AWSS3BucketLoggingFilter struct {
}

var _ editor.Filter = (*AWSS3BucketLoggingFilter)(nil)

// NewAWSS3BucketLoggingFilter creates a new instance of AWSS3BucketLoggingFilter.
func NewAWSS3BucketLoggingFilter() editor.Filter {
return &AWSS3BucketLoggingFilter{}
}

// Filter upgrades the logging argument of aws_s3_bucket.
func (f *AWSS3BucketLoggingFilter) 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 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
}

0 comments on commit b05403a

Please sign in to comment.