-
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
1dc6e78
commit 4628b92
Showing
5 changed files
with
216 additions
and
2 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
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,67 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
"github.com/minamijoyo/tfedit/tfwrite" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
// AWSS3BucketVersioningFilter is a filter implementation for upgrading the | ||
// versioning argument of aws_s3_bucket. | ||
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#versioning-argument | ||
type AWSS3BucketVersioningFilter struct{} | ||
|
||
var _ tfeditor.ResourceFilter = (*AWSS3BucketVersioningFilter)(nil) | ||
|
||
// NewAWSS3BucketVersioningFilter creates a new instance of AWSS3BucketVersioningFilter. | ||
func NewAWSS3BucketVersioningFilter() tfeditor.ResourceFilter { | ||
return &AWSS3BucketVersioningFilter{} | ||
} | ||
|
||
// ResourceFilter upgrades the versioning argument of aws_s3_bucket. | ||
func (f *AWSS3BucketVersioningFilter) ResourceFilter(inFile *tfwrite.File, resource *tfwrite.Resource) (*tfwrite.File, error) { | ||
oldNestedBlock := "versioning" | ||
newResourceType := "aws_s3_bucket_versioning" | ||
newNestedBlock := "versioning_configuration" | ||
|
||
nestedBlocks := resource.FindNestedBlocksByType(oldNestedBlock) | ||
if len(nestedBlocks) == 0 { | ||
return inFile, nil | ||
} | ||
|
||
resourceName := resource.Name() | ||
newResource := tfwrite.NewEmptyResource(newResourceType, resourceName) | ||
inFile.AppendResource(newResource) | ||
setBucketArgument(newResource, resource) | ||
|
||
nestedBlock := nestedBlocks[0] | ||
|
||
// Rename a `versioning` block to a `versioning_configuration` block | ||
nestedBlock.SetType(newNestedBlock) | ||
|
||
// Map an `enabled` attribute to a `status` attribute | ||
// enabled = true => status = "Enabled" | ||
// enabled = false => status = "Suspended" | ||
enabledAttr := nestedBlock.GetAttribute("enabled") | ||
if enabledAttr != nil { | ||
enabled, err := enabledAttr.ValueAsString() | ||
if err == nil { | ||
switch enabled { | ||
case "true": | ||
nestedBlock.SetAttributeValue("status", cty.StringVal("Enabled")) | ||
case "false": | ||
nestedBlock.SetAttributeValue("status", cty.StringVal("Suspended")) | ||
default: | ||
// If the value is a variable, not literal, we cannot rewrite it automatically. | ||
// Set original raw tokens as it is. | ||
nestedBlock.SetAttributeRaw("status", enabledAttr.ValueAsTokens()) | ||
} | ||
} | ||
nestedBlock.RemoveAttribute("enabled") | ||
} | ||
|
||
newResource.AppendNestedBlock(nestedBlock) | ||
resource.RemoveNestedBlock(nestedBlock) | ||
|
||
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,135 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/minamijoyo/hcledit/editor" | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
) | ||
|
||
func TestAWSS3BucketVersioningFilter(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
src string | ||
ok bool | ||
want string | ||
}{ | ||
{ | ||
name: "enabled = true", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
versioning { | ||
enabled = true | ||
} | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
} | ||
resource "aws_s3_bucket_versioning" "example" { | ||
bucket = aws_s3_bucket.example.id | ||
versioning_configuration { | ||
status = "Enabled" | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "enabled = false", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
versioning { | ||
enabled = false | ||
} | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
} | ||
resource "aws_s3_bucket_versioning" "example" { | ||
bucket = aws_s3_bucket.example.id | ||
versioning_configuration { | ||
status = "Suspended" | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "enabled = var.enabled", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
versioning { | ||
enabled = var.enabled | ||
} | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
} | ||
resource "aws_s3_bucket_versioning" "example" { | ||
bucket = aws_s3_bucket.example.id | ||
versioning_configuration { | ||
status = var.enabled | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "argument not found", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
foo {} | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
foo {} | ||
} | ||
`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
filter := &AWSS3BucketFilter{filters: []tfeditor.ResourceFilter{&AWSS3BucketVersioningFilter{}}} | ||
o := editor.NewEditOperator(filter) | ||
output, err := o.Apply([]byte(tc.src), "test") | ||
if tc.ok && err != nil { | ||
t.Fatalf("unexpected err = %s", err) | ||
} | ||
|
||
got := string(output) | ||
if !tc.ok && err == nil { | ||
t.Fatalf("expected to return an error, but no error, outStream: \n%s", got) | ||
} | ||
|
||
if got != tc.want { | ||
t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |