Skip to content

Commit

Permalink
Merge pull request hashicorp#35502 from hashicorp/td-vpc-idempotency
Browse files Browse the repository at this point in the history
EC2: Make additional (mainly `Create`) API calls idempotent
  • Loading branch information
ewbankkit authored Jan 26, 2024
2 parents 4daf73d + ebbd679 commit f49fa3d
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 47 deletions.
4 changes: 2 additions & 2 deletions internal/service/ec2/ec2_capacity_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -124,6 +125,7 @@ func resourceCapacityReservationCreate(ctx context.Context, d *schema.ResourceDa

input := &ec2.CreateCapacityReservationInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
ClientToken: aws.String(id.UniqueId()),
EndDateType: aws.String(d.Get("end_date_type").(string)),
InstanceCount: aws.Int64(int64(d.Get("instance_count").(int))),
InstancePlatform: aws.String(d.Get("instance_platform").(string)),
Expand Down Expand Up @@ -161,7 +163,6 @@ func resourceCapacityReservationCreate(ctx context.Context, d *schema.ResourceDa
input.Tenancy = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating EC2 Capacity Reservation: %s", input)
output, err := conn.CreateCapacityReservationWithContext(ctx, input)

if err != nil {
Expand Down Expand Up @@ -234,7 +235,6 @@ func resourceCapacityReservationUpdate(ctx context.Context, d *schema.ResourceDa
input.EndDate = aws.Time(v)
}

log.Printf("[DEBUG] Updating EC2 Capacity Reservation: %s", input)
_, err := conn.ModifyCapacityReservationWithContext(ctx, input)

if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/service/ec2/ec2_fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -706,6 +707,7 @@ func resourceFleetCreate(ctx context.Context, d *schema.ResourceData, meta inter

fleetType := d.Get("type").(string)
input := &ec2.CreateFleetInput{
ClientToken: aws.String(id.UniqueId()),
LaunchTemplateConfigs: expandFleetLaunchTemplateConfigRequests(d.Get("launch_template_config").([]interface{})),
TargetCapacitySpecification: expandTargetCapacitySpecificationRequest(d.Get("target_capacity_specification").([]interface{})[0].(map[string]interface{})),
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeFleet),
Expand Down Expand Up @@ -901,7 +903,7 @@ func resourceFleetDelete(ctx context.Context, d *schema.ResourceData, meta inter
})

if err == nil && output != nil {
err = DeleteFleetsError(output.UnsuccessfulFleetDeletions)
err = deleteFleetsError(output.UnsuccessfulFleetDeletions)
}

if tfawserr.ErrCodeEquals(err, errCodeInvalidFleetIdNotFound) {
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,7 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, meta in
instanceCreditSpecification := expandInstanceCreditSpecificationRequest(v.([]interface{})[0].(map[string]interface{}))
instanceCreditSpecification.InstanceId = aws.String(d.Id())
input := &ec2.ModifyInstanceCreditSpecificationInput{
ClientToken: aws.String(id.UniqueId()),
InstanceCreditSpecifications: []*ec2.InstanceCreditSpecificationRequest{instanceCreditSpecification},
}

Expand Down
6 changes: 3 additions & 3 deletions internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,19 @@ func CancelSpotFleetRequestsError(apiObjects []*ec2.CancelSpotFleetRequestsError
return errors.Join(errs...)
}

func DeleteFleetError(apiObject *ec2.DeleteFleetErrorItem) error {
func deleteFleetError(apiObject *ec2.DeleteFleetErrorItem) error {
if apiObject == nil || apiObject.Error == nil {
return nil
}

return awserr.New(aws.StringValue(apiObject.Error.Code), aws.StringValue(apiObject.Error.Message), nil)
}

func DeleteFleetsError(apiObjects []*ec2.DeleteFleetErrorItem) error {
func deleteFleetsError(apiObjects []*ec2.DeleteFleetErrorItem) error {
var errs []error

for _, apiObject := range apiObjects {
if err := DeleteFleetError(apiObject); err != nil {
if err := deleteFleetError(apiObject); err != nil {
errs = append(errs, fmt.Errorf("%s: %w", aws.StringValue(apiObject.FleetId), err))
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/service/ec2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/service/ec2/verifiedaccess_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2/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/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -191,6 +192,7 @@ func resourceVerifiedAccessEndpointCreate(ctx context.Context, d *schema.Resourc
input := &ec2.CreateVerifiedAccessEndpointInput{
ApplicationDomain: aws.String(d.Get("application_domain").(string)),
AttachmentType: types.VerifiedAccessEndpointAttachmentType(d.Get("attachment_type").(string)),
ClientToken: aws.String(id.UniqueId()),
DomainCertificateArn: aws.String(d.Get("domain_certificate_arn").(string)),
EndpointDomainPrefix: aws.String(d.Get("endpoint_domain_prefix").(string)),
EndpointType: types.VerifiedAccessEndpointType(d.Get("endpoint_type").(string)),
Expand Down Expand Up @@ -291,6 +293,7 @@ func resourceVerifiedAccessEndpointUpdate(ctx context.Context, d *schema.Resourc

if d.HasChangesExcept("policy_document", "tags", "tags_all") {
input := &ec2.ModifyVerifiedAccessEndpointInput{
ClientToken: aws.String(id.UniqueId()),
VerifiedAccessEndpointId: aws.String(d.Id()),
}

Expand Down Expand Up @@ -349,6 +352,7 @@ func resourceVerifiedAccessEndpointDelete(ctx context.Context, d *schema.Resourc

log.Printf("[INFO] Deleting Verified Access Endpoint: %s", d.Id())
_, err := conn.DeleteVerifiedAccessEndpoint(ctx, &ec2.DeleteVerifiedAccessEndpointInput{
ClientToken: aws.String(id.UniqueId()),
VerifiedAccessEndpointId: aws.String(d.Id()),
})

Expand Down
4 changes: 4 additions & 0 deletions internal/service/ec2/verifiedaccess_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2/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/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
Expand Down Expand Up @@ -85,6 +86,7 @@ func resourceVerifiedAccessGroupCreate(ctx context.Context, d *schema.ResourceDa
conn := meta.(*conns.AWSClient).EC2Client(ctx)

input := &ec2.CreateVerifiedAccessGroupInput{
ClientToken: aws.String(id.UniqueId()),
TagSpecifications: getTagSpecificationsInV2(ctx, types.ResourceTypeVerifiedAccessGroup),
VerifiedAccessInstanceId: aws.String(d.Get("verifiedaccess_instance_id").(string)),
}
Expand Down Expand Up @@ -152,6 +154,7 @@ func resourceVerifiedAccessGroupUpdate(ctx context.Context, d *schema.ResourceDa

if d.HasChangesExcept("policy_document", "tags", "tags_all") {
input := &ec2.ModifyVerifiedAccessGroupInput{
ClientToken: aws.String(id.UniqueId()),
VerifiedAccessGroupId: aws.String(d.Id()),
}

Expand Down Expand Up @@ -193,6 +196,7 @@ func resourceVerifiedAccessGroupDelete(ctx context.Context, d *schema.ResourceDa

log.Printf("[INFO] Deleting Verified Access Group: %s", d.Id())
_, err := conn.DeleteVerifiedAccessGroup(ctx, &ec2.DeleteVerifiedAccessGroupInput{
ClientToken: aws.String(id.UniqueId()),
VerifiedAccessGroupId: aws.String(d.Id()),
})

Expand Down
4 changes: 4 additions & 0 deletions internal/service/ec2/verifiedaccess_trust_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -145,6 +146,7 @@ func resourceVerifiedAccessTrustProviderCreate(ctx context.Context, d *schema.Re
conn := meta.(*conns.AWSClient).EC2Client(ctx)

input := &ec2.CreateVerifiedAccessTrustProviderInput{
ClientToken: aws.String(id.UniqueId()),
PolicyReferenceName: aws.String(d.Get("policy_reference_name").(string)),
TagSpecifications: getTagSpecificationsInV2(ctx, types.ResourceTypeVerifiedAccessTrustProvider),
TrustProviderType: types.TrustProviderType(d.Get("trust_provider_type").(string)),
Expand Down Expand Up @@ -228,6 +230,7 @@ func resourceVerifiedAccessTrustProviderUpdate(ctx context.Context, d *schema.Re

if d.HasChangesExcept("tags", "tags_all") {
input := &ec2.ModifyVerifiedAccessTrustProviderInput{
ClientToken: aws.String(id.UniqueId()),
VerifiedAccessTrustProviderId: aws.String(d.Id()),
}

Expand Down Expand Up @@ -257,6 +260,7 @@ func resourceVerifiedAccessTrustProviderDelete(ctx context.Context, d *schema.Re

log.Printf("[INFO] Deleting Verified Access Trust Provider: %s", d.Id())
_, err := conn.DeleteVerifiedAccessTrustProvider(ctx, &ec2.DeleteVerifiedAccessTrustProviderInput{
ClientToken: aws.String(id.UniqueId()),
VerifiedAccessTrustProviderId: aws.String(d.Id()),
})

Expand Down
4 changes: 3 additions & 1 deletion internal/service/ec2/vpc_endpoint_connection_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
Expand All @@ -19,7 +20,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

// @SDKResource("aws_vpc_endpoint_connection_notification")
// @SDKResource("aws_vpc_endpoint_connection_notification", name="VPC Endpoint Connection Notification")
func ResourceVPCEndpointConnectionNotification() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceVPCEndpointConnectionNotificationCreate,
Expand Down Expand Up @@ -72,6 +73,7 @@ func resourceVPCEndpointConnectionNotificationCreate(ctx context.Context, d *sch
conn := meta.(*conns.AWSClient).EC2Conn(ctx)

input := &ec2.CreateVpcEndpointConnectionNotificationInput{
ClientToken: aws.String(id.UniqueId()),
ConnectionEvents: flex.ExpandStringSet(d.Get("connection_events").(*schema.Set)),
ConnectionNotificationArn: aws.String(d.Get("connection_notification_arn").(string)),
}
Expand Down
2 changes: 2 additions & 0 deletions internal/service/ec2/vpc_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -155,6 +156,7 @@ func resourceVPCEndpointServiceCreate(ctx context.Context, d *schema.ResourceDat

input := &ec2.CreateVpcEndpointServiceConfigurationInput{
AcceptanceRequired: aws.Bool(d.Get("acceptance_required").(bool)),
ClientToken: aws.String(id.UniqueId()),
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeVpcEndpointService),
}

Expand Down
21 changes: 7 additions & 14 deletions internal/service/ec2/vpc_managed_prefix_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -102,31 +103,23 @@ func resourceManagedPrefixListCreate(ctx context.Context, d *schema.ResourceData

conn := meta.(*conns.AWSClient).EC2Conn(ctx)

name := d.Get("name").(string)
input := &ec2.CreateManagedPrefixListInput{
AddressFamily: aws.String(d.Get("address_family").(string)),
ClientToken: aws.String(id.UniqueId()),
MaxEntries: aws.Int64(int64(d.Get("max_entries").(int))),
PrefixListName: aws.String(name),
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypePrefixList),
}

if v, ok := d.GetOk("address_family"); ok {
input.AddressFamily = aws.String(v.(string))
}

if v, ok := d.GetOk("entry"); ok && v.(*schema.Set).Len() > 0 {
input.Entries = expandAddPrefixListEntries(v.(*schema.Set).List())
}

if v, ok := d.GetOk("max_entries"); ok {
input.MaxEntries = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("name"); ok {
input.PrefixListName = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating EC2 Managed Prefix List: %s", input)
output, err := conn.CreateManagedPrefixListWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating EC2 Managed Prefix List: %s", err)
return sdkdiag.AppendErrorf(diags, "creating EC2 Managed Prefix List (%s): %s", name, err)
}

d.SetId(aws.StringValue(output.PrefixList.PrefixListId))
Expand Down
3 changes: 2 additions & 1 deletion internal/service/ec2/vpc_network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -161,11 +162,11 @@ func resourceNetworkACLCreate(ctx context.Context, d *schema.ResourceData, meta
conn := meta.(*conns.AWSClient).EC2Conn(ctx)

input := &ec2.CreateNetworkAclInput{
ClientToken: aws.String(id.UniqueId()),
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeNetworkAcl),
VpcId: aws.String(d.Get("vpc_id").(string)),
}

log.Printf("[DEBUG] Creating EC2 Network ACL: %s", input)
output, err := conn.CreateNetworkAclWithContext(ctx, input)

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/service/ec2/vpc_network_insights_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -95,6 +96,7 @@ func resourceNetworkInsightsPathCreate(ctx context.Context, d *schema.ResourceDa
conn := meta.(*conns.AWSClient).EC2Conn(ctx)

input := &ec2.CreateNetworkInsightsPathInput{
ClientToken: aws.String(id.UniqueId()),
Destination: aws.String(d.Get("destination").(string)),
Protocol: aws.String(d.Get("protocol").(string)),
Source: aws.String(d.Get("source").(string)),
Expand Down
4 changes: 3 additions & 1 deletion internal/service/ec2/vpc_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
Expand Down Expand Up @@ -168,8 +169,9 @@ func resourceRouteTableCreate(ctx context.Context, d *schema.ResourceData, meta
conn := meta.(*conns.AWSClient).EC2Conn(ctx)

input := &ec2.CreateRouteTableInput{
VpcId: aws.String(d.Get("vpc_id").(string)),
ClientToken: aws.String(id.UniqueId()),
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeRouteTable),
VpcId: aws.String(d.Get("vpc_id").(string)),
}

output, err := conn.CreateRouteTableWithContext(ctx, input)
Expand Down
2 changes: 2 additions & 0 deletions internal/service/ec2/vpc_traffic_mirror_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -68,6 +69,7 @@ func resourceTrafficMirrorFilterCreate(ctx context.Context, d *schema.ResourceDa
conn := meta.(*conns.AWSClient).EC2Conn(ctx)

input := &ec2.CreateTrafficMirrorFilterInput{
ClientToken: aws.String(id.UniqueId()),
TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeTrafficMirrorFilter),
}

Expand Down
Loading

0 comments on commit f49fa3d

Please sign in to comment.