Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech debt: Migrate (more) s3control resources to AWS SDK for Go v2 #33416

Merged
merged 33 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6da9a69
r/aws_s3_access_point(_policy): Migrate to AWS SDK for Go v2.
ewbankkit Sep 9, 2023
d912890
(d|r)/aws_s3_account_public_access_block: Migrate to AWS SDK for Go v2.
ewbankkit Sep 10, 2023
2e1703d
r/aws_s3control_multi_region_access_point(_policy): Migrate to AWS SD…
ewbankkit Sep 10, 2023
c24e039
Remove 'connForMRAP'.
ewbankkit Sep 10, 2023
2f9860e
Add 'names.S3ControlEndpointID'.
ewbankkit Sep 10, 2023
2598d82
r/aws_s3control_storage_lens_configuration: Use 'names.S3ControlEndpo…
ewbankkit Sep 10, 2023
be33f6a
Move AsyncOperation.RequestStatus constants.
ewbankkit Sep 10, 2023
cebbaae
s3control: Migrate Access Point, Account Public Access Block and MRAP…
ewbankkit Sep 10, 2023
a0eef42
Acceptance test output:
ewbankkit Sep 10, 2023
eef4563
Acceptance test output:
ewbankkit Sep 11, 2023
81c02ac
Revert "(d|r)/aws_s3_account_public_access_block: Migrate to AWS SDK …
ewbankkit Sep 11, 2023
6e526d1
Acceptance test output:
ewbankkit Sep 11, 2023
129c051
Fix typos.
ewbankkit Sep 11, 2023
d0dd209
s3control: Generate AWS SDK for Go v2 tagging code.
ewbankkit Sep 11, 2023
120dff9
Merge 'RetryWhenAWSErrCodeEqualsV2' into 'RetryWhenAWSErrCodeEquals'.
ewbankkit Sep 11, 2023
56f3ad5
Merge 'RetryWhenAWSErrMessageContainsV2' into 'RetryWhenAWSErrMessage…
ewbankkit Sep 11, 2023
968dbfc
r/aws_s3control_bucket(_lifecycle_configuration|_policy): Migrate to …
ewbankkit Sep 11, 2023
661aaaf
r/aws_s3control_bucket(_lifecycle_configuration|_policy): Migrate acc…
ewbankkit Sep 11, 2023
9244740
Acceptance test output:
ewbankkit Sep 11, 2023
056b9c7
Merge branch 'main' into td-migrate-s3control-to-aws-sdk-v2
ewbankkit Sep 11, 2023
c1d5f8b
r/aws_s3control_object_lambda_access_point: Migrate to AWS SDK for Go…
ewbankkit Sep 11, 2023
7f6f479
r/aws_s3control_object_lambda_access_point: Migrate acceptance tests …
ewbankkit Sep 11, 2023
43b93f7
r/aws_s3control_object_lambda_access_point_policy: Migrate to AWS SDK…
ewbankkit Sep 11, 2023
619b33f
r/aws_s3control_object_lambda_access_point_policy: Migrate acceptance…
ewbankkit Sep 11, 2023
0cfcbf8
Start to add AWS partition and region IDs to 'names'.
ewbankkit Sep 12, 2023
a9a9904
Use 'names.USWest2RegionID'.
ewbankkit Sep 12, 2023
5b2709c
Use 'names.USGovCloudPartitionID'.
ewbankkit Sep 12, 2023
2886253
s3control: Add and use 'errCodeNoSuchConfiguration'.
ewbankkit Sep 12, 2023
84c24ab
r/aws_s3control_storage_lens_configuration: Use 'tfawserr.ErrHTTPStat…
ewbankkit Sep 12, 2023
7b805c5
Acceptance test output:
ewbankkit Sep 12, 2023
553d913
s3control: Migrate sweepers to AWS SDK for Go v2.
ewbankkit Sep 12, 2023
54a8645
r/aws_s3control_multi_region_access_point: Add 'details.region.bucket…
ewbankkit Sep 12, 2023
86f2350
Acceptance test output:
ewbankkit Sep 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changelog/33416.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:enhancement
resource/aws_s3control_multi_region_access_point: Add `details.region.bucket_account_id` argument to support [cross-account Multi-Region Access Points](https://docs.aws.amazon.com/AmazonS3/latest/userguide/multi-region-access-point-buckets.html)
```

```release-note:enhancement
resource/aws_s3control_multi_region_access_point: Add `details.region.region` attribute
```

```release-note:enhancement
data-source/aws_s3control_multi_region_access_point: Add `details.region.bucket_account_id` attribute
```
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func cleanupNotificationRuleTargets(ctx context.Context, conn *codestarnotificat
TargetAddress: aws.String(target["address"].(string)),
}

_, err := tfresource.RetryWhenAWSErrMessageContainsV2(ctx, targetSubscriptionTimeout, func() (interface{}, error) {
_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, targetSubscriptionTimeout, func() (interface{}, error) {
return conn.DeleteTarget(ctx, input)
}, "ValidationException", notificationRuleErrorSubscribed)

Expand Down
4 changes: 2 additions & 2 deletions internal/service/ec2/vpc_.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa
}

// "UnsupportedOperation: The operation AllocateIpamPoolCidr is not supported. Account 123456789012 is not monitored by IPAM ipam-07b079e3392782a55."
outputRaw, err := tfresource.RetryWhenAWSErrMessageContainsV2(ctx, ec2PropagationTimeout, func() (interface{}, error) {
outputRaw, err := tfresource.RetryWhenAWSErrMessageContains(ctx, ec2PropagationTimeout, func() (interface{}, error) {
return conn.CreateVpc(ctx, input)
}, errCodeUnsupportedOperation, "is not monitored by IPAM")

Expand Down Expand Up @@ -450,7 +450,7 @@ func resourceVPCDelete(ctx context.Context, d *schema.ResourceData, meta interfa
}

log.Printf("[INFO] Deleting EC2 VPC: %s", d.Id())
_, err := tfresource.RetryWhenAWSErrCodeEqualsV2(ctx, vpcDeletedTimeout, func() (interface{}, error) {
_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, vpcDeletedTimeout, func() (interface{}, error) {
return conn.DeleteVpc(ctx, input)
}, errCodeDependencyViolation)

Expand Down
74 changes: 35 additions & 39 deletions internal/service/s3control/access_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/s3control"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/s3control"
"github.com/aws/aws-sdk-go-v2/service/s3control/types"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -156,14 +157,13 @@ func resourceAccessPoint() *schema.Resource {
}

func resourceAccessPointCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3ControlConn(ctx)
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

accountID := meta.(*conns.AWSClient).AccountID
if v, ok := d.GetOk("account_id"); ok {
accountID = v.(string)
}
name := d.Get("name").(string)

input := &s3control.CreateAccessPointInput{
AccountId: aws.String(accountID),
Bucket: aws.String(d.Get("bucket").(string)),
Expand All @@ -182,20 +182,18 @@ func resourceAccessPointCreate(ctx context.Context, d *schema.ResourceData, meta
input.VpcConfiguration = expandVPCConfiguration(v.([]interface{})[0].(map[string]interface{}))
}

output, err := conn.CreateAccessPointWithContext(ctx, input)
output, err := conn.CreateAccessPoint(ctx, input)

if err != nil {
return diag.Errorf("creating S3 Access Point (%s): %s", name, err)
}

resourceID, err := AccessPointCreateResourceID(aws.StringValue(output.AccessPointArn))

resourceID, err := AccessPointCreateResourceID(aws.ToString(output.AccessPointArn))
if err != nil {
return diag.FromErr(err)
}

accountID, name, err = AccessPointParseResourceID(resourceID)

if err != nil {
return diag.FromErr(err)
}
Expand All @@ -205,7 +203,7 @@ func resourceAccessPointCreate(ctx context.Context, d *schema.ResourceData, meta
if v, ok := d.GetOk("policy"); ok && v.(string) != "" && v.(string) != "{}" {
policy, err := structure.NormalizeJsonString(v.(string))
if err != nil {
return diag.Errorf("policy (%s) is invalid JSON: %s", v.(string), err)
return diag.FromErr(err)
}

input := &s3control.PutAccessPointPolicyInput{
Expand All @@ -214,7 +212,7 @@ func resourceAccessPointCreate(ctx context.Context, d *schema.ResourceData, meta
Policy: aws.String(policy),
}

_, err = conn.PutAccessPointPolicyWithContext(ctx, input)
_, err = conn.PutAccessPointPolicy(ctx, input)

if err != nil {
return diag.Errorf("creating S3 Access Point (%s) policy: %s", d.Id(), err)
Expand All @@ -225,17 +223,14 @@ func resourceAccessPointCreate(ctx context.Context, d *schema.ResourceData, meta
}

func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3ControlConn(ctx)
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

accountID, name, err := AccessPointParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
}

s3OnOutposts := arn.IsARN(name)

output, err := FindAccessPointByTwoPartKey(ctx, conn, accountID, name)
output, err := findAccessPointByTwoPartKey(ctx, conn, accountID, name)

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] S3 Access Point (%s) not found, removing from state", d.Id())
Expand All @@ -247,9 +242,10 @@ func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta i
return diag.Errorf("reading S3 Access Point (%s): %s", d.Id(), err)
}

s3OnOutposts := arn.IsARN(name)

if s3OnOutposts {
accessPointARN, err := arn.Parse(name)

if err != nil {
return diag.FromErr(err)
}
Expand All @@ -262,8 +258,8 @@ func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta i
AccountID: accessPointARN.AccountID,
Resource: strings.Replace(
accessPointARN.Resource,
fmt.Sprintf("accesspoint/%s", aws.StringValue(output.Name)),
fmt.Sprintf("bucket/%s", aws.StringValue(output.Bucket)),
fmt.Sprintf("accesspoint/%s", aws.ToString(output.Name)),
fmt.Sprintf("bucket/%s", aws.ToString(output.Bucket)),
1,
),
}
Expand All @@ -277,7 +273,7 @@ func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta i
Service: "s3",
Region: meta.(*conns.AWSClient).Region,
AccountID: accountID,
Resource: fmt.Sprintf("accesspoint/%s", aws.StringValue(output.Name)),
Resource: fmt.Sprintf("accesspoint/%s", aws.ToString(output.Name)),
}

d.Set("arn", accessPointARN.String())
Expand All @@ -287,8 +283,8 @@ func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("account_id", accountID)
d.Set("alias", output.Alias)
d.Set("bucket_account_id", output.BucketAccountId)
d.Set("domain_name", meta.(*conns.AWSClient).RegionalHostname(fmt.Sprintf("%s-%s.s3-accesspoint", aws.StringValue(output.Name), accountID)))
d.Set("endpoints", aws.StringValueMap(output.Endpoints))
d.Set("domain_name", meta.(*conns.AWSClient).RegionalHostname(fmt.Sprintf("%s-%s.s3-accesspoint", aws.ToString(output.Name), accountID)))
d.Set("endpoints", output.Endpoints)
d.Set("name", output.Name)
d.Set("network_origin", output.NetworkOrigin)
if output.PublicAccessBlockConfiguration != nil {
Expand All @@ -306,7 +302,7 @@ func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("vpc_configuration", nil)
}

policy, status, err := FindAccessPointPolicyAndStatusByTwoPartKey(ctx, conn, accountID, name)
policy, status, err := findAccessPointPolicyAndStatusByTwoPartKey(ctx, conn, accountID, name)

if err == nil && policy != "" {
if s3OnOutposts {
Expand All @@ -332,10 +328,9 @@ func resourceAccessPointRead(ctx context.Context, d *schema.ResourceData, meta i
}

func resourceAccessPointUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3ControlConn(ctx)
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

accountID, name, err := AccessPointParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
}
Expand All @@ -344,7 +339,7 @@ func resourceAccessPointUpdate(ctx context.Context, d *schema.ResourceData, meta
if v, ok := d.GetOk("policy"); ok && v.(string) != "" && v.(string) != "{}" {
policy, err := structure.NormalizeJsonString(v.(string))
if err != nil {
return diag.Errorf("policy (%s) is invalid JSON: %s", v.(string), err)
return diag.FromErr(err)
}

input := &s3control.PutAccessPointPolicyInput{
Expand All @@ -353,16 +348,18 @@ func resourceAccessPointUpdate(ctx context.Context, d *schema.ResourceData, meta
Policy: aws.String(policy),
}

_, err = conn.PutAccessPointPolicyWithContext(ctx, input)
_, err = conn.PutAccessPointPolicy(ctx, input)

if err != nil {
return diag.Errorf("updating S3 Access Point (%s) policy: %s", d.Id(), err)
}
} else {
_, err := conn.DeleteAccessPointPolicyWithContext(ctx, &s3control.DeleteAccessPointPolicyInput{
input := &s3control.DeleteAccessPointPolicyInput{
AccountId: aws.String(accountID),
Name: aws.String(name),
})
}

_, err := conn.DeleteAccessPointPolicy(ctx, input)

if err != nil {
return diag.Errorf("deleting S3 Access Point (%s) policy: %s", d.Id(), err)
Expand All @@ -374,16 +371,15 @@ func resourceAccessPointUpdate(ctx context.Context, d *schema.ResourceData, meta
}

func resourceAccessPointDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3ControlConn(ctx)
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

accountID, name, err := AccessPointParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] Deleting S3 Access Point: %s", d.Id())
_, err = conn.DeleteAccessPointWithContext(ctx, &s3control.DeleteAccessPointInput{
_, err = conn.DeleteAccessPoint(ctx, &s3control.DeleteAccessPointInput{
AccountId: aws.String(accountID),
Name: aws.String(name),
})
Expand All @@ -399,13 +395,13 @@ func resourceAccessPointDelete(ctx context.Context, d *schema.ResourceData, meta
return nil
}

func FindAccessPointByTwoPartKey(ctx context.Context, conn *s3control.S3Control, accountID string, name string) (*s3control.GetAccessPointOutput, error) {
func findAccessPointByTwoPartKey(ctx context.Context, conn *s3control.Client, accountID, name string) (*s3control.GetAccessPointOutput, error) {
input := &s3control.GetAccessPointInput{
AccountId: aws.String(accountID),
Name: aws.String(name),
}

output, err := conn.GetAccessPointWithContext(ctx, input)
output, err := conn.GetAccessPoint(ctx, input)

if tfawserr.ErrCodeEquals(err, errCodeNoSuchAccessPoint) {
return nil, &retry.NotFoundError{
Expand Down Expand Up @@ -468,12 +464,12 @@ func AccessPointParseResourceID(id string) (string, string, error) {
return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected account-id%[2]saccess-point-name", id, accessPointResourceIDSeparator)
}

func expandVPCConfiguration(tfMap map[string]interface{}) *s3control.VpcConfiguration {
func expandVPCConfiguration(tfMap map[string]interface{}) *types.VpcConfiguration {
if tfMap == nil {
return nil
}

apiObject := &s3control.VpcConfiguration{}
apiObject := &types.VpcConfiguration{}

if v, ok := tfMap["vpc_id"].(string); ok {
apiObject.VpcId = aws.String(v)
Expand All @@ -482,15 +478,15 @@ func expandVPCConfiguration(tfMap map[string]interface{}) *s3control.VpcConfigur
return apiObject
}

func flattenVPCConfiguration(apiObject *s3control.VpcConfiguration) map[string]interface{} {
func flattenVPCConfiguration(apiObject *types.VpcConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.VpcId; v != nil {
tfMap["vpc_id"] = aws.StringValue(v)
tfMap["vpc_id"] = aws.ToString(v)
}

return tfMap
Expand Down
Loading
Loading