-
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.
Merge pull request #10 from minamijoyo/add-object-lock-configuration
Add support for object_lock_configuration of aws_s3_bucket
- Loading branch information
Showing
6 changed files
with
177 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
47 changes: 47 additions & 0 deletions
47
filter/awsv4upgrade/aws_s3_bucket_object_lock_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,47 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
"github.com/minamijoyo/tfedit/tfwrite" | ||
) | ||
|
||
// AWSS3BucketObjectLockConfigurationFilter is a filter implementation for | ||
// upgrading the object_lock_configuration argument of aws_s3_bucket. | ||
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#object_lock_configuration-rule-argument | ||
type AWSS3BucketObjectLockConfigurationFilter struct{} | ||
|
||
var _ tfeditor.ResourceFilter = (*AWSS3BucketObjectLockConfigurationFilter)(nil) | ||
|
||
// NewAWSS3BucketObjectLockConfigurationFilter creates a new instance of | ||
// AWSS3BucketObjectLockConfigurationFilter. | ||
func NewAWSS3BucketObjectLockConfigurationFilter() tfeditor.ResourceFilter { | ||
return &AWSS3BucketObjectLockConfigurationFilter{} | ||
} | ||
|
||
// ResourceFilter upgrades the object_lock_configuration argument of | ||
// aws_s3_bucket. | ||
func (f *AWSS3BucketObjectLockConfigurationFilter) ResourceFilter(inFile *tfwrite.File, resource *tfwrite.Resource) (*tfwrite.File, error) { | ||
oldNestedBlock := "object_lock_configuration" | ||
newResourceType := "aws_s3_bucket_object_lock_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) | ||
|
||
objectLockBlock := nestedBlocks[0] | ||
|
||
// Move a rule block to a new resource | ||
ruleBlocks := objectLockBlock.FindNestedBlocksByType("rule") | ||
for _, ruleBlock := range ruleBlocks { | ||
newResource.AppendNestedBlock(ruleBlock) | ||
objectLockBlock.RemoveNestedBlock(ruleBlock) | ||
} | ||
|
||
return inFile, nil | ||
} |
96 changes: 96 additions & 0 deletions
96
filter/awsv4upgrade/aws_s3_bucket_object_lock_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,96 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/minamijoyo/hcledit/editor" | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
) | ||
|
||
func TestAWSS3BucketObjectLockConfigurationFilter(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
src string | ||
ok bool | ||
want string | ||
}{ | ||
{ | ||
name: "simple", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
object_lock_configuration { | ||
object_lock_enabled = "Enabled" | ||
rule { | ||
default_retention { | ||
mode = "COMPLIANCE" | ||
days = 3 | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
object_lock_configuration { | ||
object_lock_enabled = "Enabled" | ||
} | ||
} | ||
resource "aws_s3_bucket_object_lock_configuration" "example" { | ||
bucket = aws_s3_bucket.example.id | ||
rule { | ||
default_retention { | ||
mode = "COMPLIANCE" | ||
days = 3 | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
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{&AWSS3BucketObjectLockConfigurationFilter{}}} | ||
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
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