-
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.
- Loading branch information
1 parent
2d8144a
commit b05403a
Showing
3 changed files
with
108 additions
and
42 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
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,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 | ||
} |
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,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 | ||
} |