-
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 acceleration_status of aws_s3_bucket
- Loading branch information
1 parent
6f9c6c8
commit 8056c03
Showing
6 changed files
with
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
"github.com/minamijoyo/tfedit/tfwrite" | ||
) | ||
|
||
// AWSS3BucketAccelerationStatusFilter is a filter implementation for upgrading | ||
// the acceleration_status argument of aws_s3_bucket. | ||
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/version-4-upgrade#acceleration_status-argument | ||
type AWSS3BucketAccelerationStatusFilter struct{} | ||
|
||
var _ tfeditor.ResourceFilter = (*AWSS3BucketAccelerationStatusFilter)(nil) | ||
|
||
// NewAWSS3BucketAccelerationStatusFilter creates a new instance of AWSS3BucketAccelerationStatusFilter. | ||
func NewAWSS3BucketAccelerationStatusFilter() tfeditor.ResourceFilter { | ||
return &AWSS3BucketAccelerationStatusFilter{} | ||
} | ||
|
||
// ResourceFilter upgrades the acceleration_status argument of aws_s3_bucket. | ||
func (f *AWSS3BucketAccelerationStatusFilter) ResourceFilter(inFile *tfwrite.File, resource *tfwrite.Resource) (*tfwrite.File, error) { | ||
oldAttribute := "acceleration_status" | ||
newResourceType := "aws_s3_bucket_accelerate_configuration" | ||
|
||
attr := resource.GetAttribute(oldAttribute) | ||
if attr == nil { | ||
return inFile, nil | ||
} | ||
|
||
resourceName := resource.Name() | ||
newResource := tfwrite.NewEmptyResource(newResourceType, resourceName) | ||
inFile.AppendResource(newResource) | ||
setBucketArgument(newResource, resource) | ||
|
||
// Map an `acceleration_status` attribute to an `status` attribute. | ||
// acceleration_status = "Enabled" => status = "Enabled" | ||
status := attr.ValueAsTokens() | ||
newResource.SetAttributeRaw("status", status) | ||
|
||
resource.RemoveAttribute(oldAttribute) | ||
|
||
return inFile, nil | ||
} |
75 changes: 75 additions & 0 deletions
75
filter/awsv4upgrade/aws_s3_bucket_acceleration_status_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,75 @@ | ||
package awsv4upgrade | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/minamijoyo/hcledit/editor" | ||
"github.com/minamijoyo/tfedit/tfeditor" | ||
) | ||
|
||
func TestAWSS3BucketAccelerationStatusFilter(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
src string | ||
ok bool | ||
want string | ||
}{ | ||
{ | ||
name: "simple", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
acceleration_status = "Enabled" | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
} | ||
resource "aws_s3_bucket_accelerate_configuration" "example" { | ||
bucket = aws_s3_bucket.example.id | ||
status = "Enabled" | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "argument not found", | ||
src: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
foo = "bar" | ||
} | ||
`, | ||
ok: true, | ||
want: ` | ||
resource "aws_s3_bucket" "example" { | ||
bucket = "tfedit-test" | ||
foo = "bar" | ||
} | ||
`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
filter := &AWSS3BucketFilter{filters: []tfeditor.ResourceFilter{&AWSS3BucketAccelerationStatusFilter{}}} | ||
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