-
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.
Add support for replication_configuration of aws_s3_bucket
- Loading branch information
1 parent
bc2e74f
commit 25c36c1
Showing
5 changed files
with
315 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
117 changes: 117 additions & 0 deletions
117
filter/awsv4upgrade/aws_s3_bucket_replication_configuration.go
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,117 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
"github.com/minamijoyo/tfedit/tfwrite" | ||
) | ||
|
||
// AWSS3BucketReplicationConfigurationFilter is a filter implementation for | ||
// upgrading the replication_configuration argument of aws_s3_bucket. | ||
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#replication_configuration-argument | ||
type AWSS3BucketReplicationConfigurationFilter struct{} | ||
|
||
var _ tfeditor.ResourceFilter = (*AWSS3BucketReplicationConfigurationFilter)(nil) | ||
|
||
// NewAWSS3BucketReplicationConfigurationFilter creates a new instance of | ||
// AWSS3BucketReplicationConfigurationFilter. | ||
func NewAWSS3BucketReplicationConfigurationFilter() tfeditor.ResourceFilter { | ||
return &AWSS3BucketReplicationConfigurationFilter{} | ||
} | ||
|
||
// ResourceFilter upgrades the replication_configuration argument of | ||
// aws_s3_bucket. | ||
func (f *AWSS3BucketReplicationConfigurationFilter) ResourceFilter(inFile *tfwrite.File, resource *tfwrite.Resource) (*tfwrite.File, error) { | ||
oldNestedBlock := "replication_configuration" | ||
newResourceType := "aws_s3_bucket_replication_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) | ||
|
||
for _, nestedBlock := range nestedBlocks { | ||
roleAttr := nestedBlock.GetAttribute("role") | ||
if roleAttr != nil { | ||
newResource.AppendAttribute(roleAttr) | ||
} | ||
|
||
rulesBlocks := nestedBlock.FindNestedBlocksByType("rules") | ||
for _, rulesBlock := range rulesBlocks { | ||
// Rename a `rules` block to a `rule` block | ||
rulesBlock.SetType("rule") | ||
newResource.AppendNestedBlock(rulesBlock) | ||
|
||
// Map a `delete_marker_replication_status` attribute to a `delete_marker_replication` block | ||
// delete_marker_replication_status = "Enabled" | ||
// => | ||
// delete_marker_replication { | ||
// status = "Enabled" | ||
// } | ||
deleteMarkerAttr := rulesBlock.GetAttribute("delete_marker_replication_status") | ||
if deleteMarkerAttr != nil { | ||
deleteMarkerBlock := tfwrite.NewEmptyNestedBlock("delete_marker_replication") | ||
rulesBlock.AppendNestedBlock(deleteMarkerBlock) | ||
deleteMarkerBlock.SetAttributeRaw("status", deleteMarkerAttr.ValueAsTokens()) | ||
rulesBlock.RemoveAttribute("delete_marker_replication_status") | ||
} | ||
|
||
destinationBlocks := rulesBlock.FindNestedBlocksByType("destination") | ||
for _, destinationBlock := range destinationBlocks { | ||
// Map a `replication_time.minutes` attribute to a `replication_time.time.minutes` attribute | ||
// replication_time { | ||
// status = "Enabled" | ||
// minutes = 15 | ||
// } | ||
// => | ||
// replication_time { | ||
// status = "Enabled" | ||
// time { | ||
// minutes = 15 | ||
// } | ||
// } | ||
replicationTimeBlocks := destinationBlock.FindNestedBlocksByType("replication_time") | ||
for _, replicationTimeBlock := range replicationTimeBlocks { | ||
minutesAttribute := replicationTimeBlock.GetAttribute("minutes") | ||
if minutesAttribute != nil { | ||
timeBlock := tfwrite.NewEmptyNestedBlock("time") | ||
replicationTimeBlock.AppendNestedBlock(timeBlock) | ||
timeBlock.SetAttributeRaw("minutes", minutesAttribute.ValueAsTokens()) | ||
replicationTimeBlock.RemoveAttribute("minutes") | ||
} | ||
} | ||
|
||
// Map a `metrics.minutes` attribute to a `metrics.event_threshold.minutes` attribute | ||
// metrics { | ||
// status = "Enabled" | ||
// minutes = 15 | ||
// } | ||
// => | ||
// metrics { | ||
// status = "Enabled" | ||
// event_threshold { | ||
// minutes = 15 | ||
// } | ||
// } | ||
metricsBlocks := destinationBlock.FindNestedBlocksByType("metrics") | ||
for _, metricsBlock := range metricsBlocks { | ||
minutesAttribute := metricsBlock.GetAttribute("minutes") | ||
if minutesAttribute != nil { | ||
eventThresholdBlock := tfwrite.NewEmptyNestedBlock("event_threshold") | ||
metricsBlock.AppendNestedBlock(eventThresholdBlock) | ||
eventThresholdBlock.SetAttributeRaw("minutes", minutesAttribute.ValueAsTokens()) | ||
metricsBlock.RemoveAttribute("minutes") | ||
} | ||
} | ||
} | ||
} | ||
|
||
resource.RemoveNestedBlock(nestedBlock) | ||
} | ||
|
||
return inFile, nil | ||
} |
133 changes: 133 additions & 0 deletions
133
filter/awsv4upgrade/aws_s3_bucket_replication_configuration_test.go
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,133 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/minamijoyo/hcledit/editor" | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
) | ||
|
||
func TestAWSS3BucketReplicationConfigurationFilter(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
src string | ||
ok bool | ||
want string | ||
}{ | ||
{ | ||
name: "simple", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
replication_configuration { | ||
role = aws_iam_role.replication.arn | ||
rules { | ||
id = "foobar" | ||
status = "Enabled" | ||
filter {} | ||
delete_marker_replication_status = "Enabled" | ||
destination { | ||
bucket = aws_s3_bucket.destination.arn | ||
storage_class = "STANDARD" | ||
replication_time { | ||
status = "Enabled" | ||
minutes = 15 | ||
} | ||
metrics { | ||
status = "Enabled" | ||
minutes = 15 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
} | ||
resource "aws_s3_bucket_replication_configuration" "example" { | ||
bucket = aws_s3_bucket.example.id | ||
role = aws_iam_role.replication.arn | ||
rule { | ||
id = "foobar" | ||
status = "Enabled" | ||
filter {} | ||
destination { | ||
bucket = aws_s3_bucket.destination.arn | ||
storage_class = "STANDARD" | ||
replication_time { | ||
status = "Enabled" | ||
time { | ||
minutes = 15 | ||
} | ||
} | ||
metrics { | ||
status = "Enabled" | ||
event_threshold { | ||
minutes = 15 | ||
} | ||
} | ||
} | ||
delete_marker_replication { | ||
status = "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{&AWSS3BucketReplicationConfigurationFilter{}}} | ||
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 diff := cmp.Diff(got, tc.want); diff != "" { | ||
t.Fatalf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, tc.want, diff) | ||
} | ||
}) | ||
} | ||
} |
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