Skip to content

Commit

Permalink
Merge pull request #26745 from hashicorp/b-iso-partition-disable-api-…
Browse files Browse the repository at this point in the history
…stop

ec2/iso: Fix disableAPIStop bug for instances
  • Loading branch information
YakDriver authored Sep 9, 2022
2 parents 4bdb205 + 763d180 commit 506ba9e
Show file tree
Hide file tree
Showing 42 changed files with 153 additions and 130 deletions.
3 changes: 3 additions & 0 deletions .changelog/26745.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_instance: Prevents errors in ISO regions when not using DisableApiStop attribute
```
8 changes: 4 additions & 4 deletions internal/service/cloudwatch/composite_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func resourceCompositeAlarmCreate(ctx context.Context, d *schema.ResourceData, m
_, err := conn.PutCompositeAlarmWithContext(ctx, &input)

// Some partitions (i.e., ISO) may not support tag-on-create
if input.Tags != nil && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if input.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed creating CloudWatch Composite Alarm (%s) with tags: %s. Trying create without tags.", name, err)
input.Tags = nil

Expand All @@ -129,7 +129,7 @@ func resourceCompositeAlarmCreate(ctx context.Context, d *schema.ResourceData, m
err = UpdateTags(conn, aws.StringValue(alarm.AlarmArn), nil, tags)

// If default tags only, log and continue. Otherwise, error.
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed adding tags after create for CloudWatch Composite Alarm (%s): %s", d.Id(), err)
return resourceCompositeAlarmRead(ctx, d, meta)
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func resourceCompositeAlarmRead(ctx context.Context, d *schema.ResourceData, met
tags, err := ListTags(conn, aws.StringValue(alarm.AlarmArn))

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed listing tags for CloudWatch Composite Alarm (%s): %s", d.Id(), err)
return nil
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func resourceCompositeAlarmUpdate(ctx context.Context, d *schema.ResourceData, m
err := UpdateTags(conn, arn, o, n)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed updating tags for CloudWatch Composite Alarm (%s): %s", d.Id(), err)
return resourceCompositeAlarmRead(ctx, d, meta)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/cloudwatch/metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func resourceMetricAlarmCreate(d *schema.ResourceData, meta interface{}) error {
_, err = conn.PutMetricAlarm(&params)

// Some partitions (i.e., ISO) may not support tag-on-create
if params.Tags != nil && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if params.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed creating CloudWatch Metric Alarm (%s) with tags: %s. Trying create without tags.", d.Get("alarm_name").(string), err)
params.Tags = nil

Expand Down Expand Up @@ -336,7 +336,7 @@ func resourceMetricAlarmCreate(d *schema.ResourceData, meta interface{}) error {
err = UpdateTags(conn, aws.StringValue(resp.AlarmArn), nil, tags)

// If default tags only, log and continue. Otherwise, error.
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed adding tags after create for CloudWatch Metric Alarm (%s): %s", d.Id(), err)
return resourceMetricAlarmRead(d, meta)
}
Expand Down Expand Up @@ -431,7 +431,7 @@ func resourceMetricAlarmRead(d *schema.ResourceData, meta interface{}) error {
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed listing tags for CloudWatch Metric Alarm (%s): %s", d.Id(), err)
return nil
}
Expand Down Expand Up @@ -466,7 +466,7 @@ func resourceMetricAlarmUpdate(d *schema.ResourceData, meta interface{}) error {
err := UpdateTags(conn, arn, o, n)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed updating tags for CloudWatch Metric Alarm (%s): %s", d.Id(), err)
return resourceMetricAlarmRead(d, meta)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/service/cloudwatch/metric_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func resourceMetricStreamCreate(ctx context.Context, d *schema.ResourceData, met
output, err := conn.PutMetricStreamWithContext(ctx, &params)

// Some partitions (i.e., ISO) may not support tag-on-create
if params.Tags != nil && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if params.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed creating CloudWatch Metric Stream (%s) with tags: %s. Trying create without tags.", name, err)
params.Tags = nil

Expand All @@ -222,7 +222,7 @@ func resourceMetricStreamCreate(ctx context.Context, d *schema.ResourceData, met
err := UpdateTags(conn, aws.StringValue(output.Arn), nil, tags)

// If default tags only, log and continue. Otherwise, error.
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed adding tags after create for CloudWatch Metric Stream (%s): %s", d.Id(), err)
return resourceMetricStreamRead(ctx, d, meta)
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func resourceMetricStreamRead(ctx context.Context, d *schema.ResourceData, meta
tags, err := ListTags(conn, aws.StringValue(output.Arn))

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed listing tags for CloudWatch Metric Stream (%s): %s", d.Id(), err)
return nil
}
Expand Down
22 changes: 16 additions & 6 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,6 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
CapacityReservationSpecification: instanceOpts.CapacityReservationSpecification,
CpuOptions: instanceOpts.CpuOptions,
CreditSpecification: instanceOpts.CreditSpecification,
DisableApiStop: instanceOpts.DisableAPIStop,
DisableApiTermination: instanceOpts.DisableAPITermination,
EbsOptimized: instanceOpts.EBSOptimized,
EnclaveOptions: instanceOpts.EnclaveOptions,
Expand Down Expand Up @@ -847,6 +846,10 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
UserData: instanceOpts.UserData64,
}

if instanceOpts.DisableAPIStop != nil {
input.DisableApiStop = instanceOpts.DisableAPIStop
}

log.Printf("[DEBUG] Creating EC2 Instance: %s", input)
outputRaw, err := tfresource.RetryWhen(propagationTimeout,
func() (interface{}, error) {
Expand Down Expand Up @@ -1199,10 +1202,12 @@ func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
Attribute: aws.String(ec2.InstanceAttributeNameDisableApiStop),
InstanceId: aws.String(d.Id()),
})
if err != nil {
if err != nil && !verify.ErrorISOUnsupported(meta.(*conns.AWSClient).Partition, err) {
return fmt.Errorf("reading EC2 Instance (%s) attribute: %w ", d.Id(), err)
}
d.Set("disable_api_stop", attr.DisableApiStop.Value)
if !verify.ErrorISOUnsupported(meta.(*conns.AWSClient).Partition, err) {
d.Set("disable_api_stop", attr.DisableApiStop.Value)
}
}
{
if isSnowballEdgeInstance(d.Id()) {
Expand Down Expand Up @@ -1822,8 +1827,10 @@ func resourceInstanceDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] attempting to terminate EC2 Instance (%s) despite error disabling API termination: %s", d.Id(), err)
}

if err := disableInstanceAPIStop(conn, d.Id(), d.Get("disable_api_stop").(bool)); err != nil {
log.Printf("[WARN] attempting to terminate EC2 Instance (%s) despite error disabling API stop: %s", d.Id(), err)
if v, ok := d.GetOk("disable_api_stop"); ok {
if err := disableInstanceAPIStop(conn, d.Id(), v.(bool)); err != nil {
log.Printf("[WARN] attempting to terminate EC2 Instance (%s) despite error disabling API stop: %s", d.Id(), err)
}
}

if err := terminateInstance(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
Expand Down Expand Up @@ -2543,13 +2550,16 @@ func buildInstanceOpts(d *schema.ResourceData, meta interface{}) (*instanceOpts,
conn := meta.(*conns.AWSClient).EC2Conn

opts := &instanceOpts{
DisableAPIStop: aws.Bool(d.Get("disable_api_stop").(bool)),
DisableAPITermination: aws.Bool(d.Get("disable_api_termination").(bool)),
EBSOptimized: aws.Bool(d.Get("ebs_optimized").(bool)),
EnclaveOptions: expandEnclaveOptions(d.Get("enclave_options").([]interface{})),
MetadataOptions: expandInstanceMetadataOptions(d.Get("metadata_options").([]interface{})),
}

if v, ok := d.GetOk("disable_api_stop"); ok {
opts.DisableAPIStop = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("ami"); ok {
opts.ImageID = aws.String(v.(string))
}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/ecr/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func resourceRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
out, err := conn.CreateRepository(&input)

// Some partitions (i.e., ISO) may not support tag-on-create
if input.Tags != nil && meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if input.Tags != nil && meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed creating ECR Repository (%s) with tags: %s. Trying create without tags.", d.Get("name").(string), err)
input.Tags = nil

Expand All @@ -163,7 +163,7 @@ func resourceRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
err := UpdateTags(conn, aws.StringValue(repository.RepositoryArn), nil, tags)

// If default tags only, log and continue. Otherwise, error.
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed adding tags after create for ECR Repository (%s): %s", d.Id(), err)
return resourceRepositoryRead(d, meta)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func resourceRepositoryRead(d *schema.ResourceData, meta interface{}) error {
tags, err := ListTags(conn, arn)

// Some partitions (i.e., ISO) may not support tagging, giving error
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed listing tags for ECR Repository (%s): %s", d.Id(), err)
return nil
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func resourceRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
err := UpdateTags(conn, arn, o, n)

// Some partitions may not support tagging, giving error
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed updating tags for ECR Repository (%s): %s", d.Id(), err)
return resourceRepositoryRead(d, meta)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/ecr/repository_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func dataSourceRepositoryRead(d *schema.ResourceData, meta interface{}) error {
tags, err := ListTags(conn, arn)

// Some partitions (i.e., ISO) may not support tagging, giving error
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed listing tags for ECR Repository (%s): %s", d.Id(), err)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/service/ecs/capacity_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func resourceCapacityProviderCreate(d *schema.ResourceData, meta interface{}) er
output, err := conn.CreateCapacityProvider(&input)

// Some partitions (i.e., ISO) may not support tag-on-create
if input.Tags != nil && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if input.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] ECS tagging failed creating Capacity Provider (%s) with tags: %s. Trying create without tags.", name, err)
input.Tags = nil

Expand All @@ -143,7 +143,7 @@ func resourceCapacityProviderCreate(d *schema.ResourceData, meta interface{}) er
if input.Tags == nil && len(tags) > 0 {
err := UpdateTags(conn, d.Id(), nil, tags)

if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
// If default tags only, log and continue. Otherwise, error.
log.Printf("[WARN] ECS tagging failed adding tags after create for Capacity Provider (%s): %s", d.Id(), err)
return resourceCapacityProviderRead(d, meta)
Expand Down Expand Up @@ -239,7 +239,7 @@ func resourceCapacityProviderUpdate(d *schema.ResourceData, meta interface{}) er
err := UpdateTags(conn, d.Id(), o, n)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] ECS tagging failed updating tags for Capacity Provider (%s): %s", d.Id(), err)
return resourceCapacityProviderRead(d, meta)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/service/ecs/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func resourceClusterCreate(d *schema.ResourceData, meta interface{}) error {
out, err := retryClusterCreate(conn, input)

// Some partitions (i.e., ISO) may not support tag-on-create
if input.Tags != nil && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if input.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] ECS tagging failed creating Cluster (%s) with tags: %s. Trying create without tags.", clusterName, err)
input.Tags = nil

Expand All @@ -226,7 +226,7 @@ func resourceClusterCreate(d *schema.ResourceData, meta interface{}) error {
if input.Tags == nil && len(tags) > 0 {
err := UpdateTags(conn, d.Id(), nil, tags)

if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
// If default tags only, log and continue. Otherwise, error.
log.Printf("[WARN] ECS tagging failed adding tags after create for Cluster (%s): %s", d.Id(), err)
return resourceClusterRead(d, meta)
Expand Down Expand Up @@ -366,7 +366,7 @@ func resourceClusterUpdate(d *schema.ResourceData, meta interface{}) error {
err := UpdateTags(conn, d.Id(), o, n)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] ECS tagging failed updating tags for Cluster (%s): %s", d.Id(), err)
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/ecs/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func FindCapacityProviderByARN(conn *ecs.ECS, arn string) (*ecs.CapacityProvider
output, err := conn.DescribeCapacityProviders(input)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] ECS tagging failed describing Capacity Provider (%s) with tags: %s; retrying without tags", arn, err)

input.Include = nil
Expand Down Expand Up @@ -61,15 +61,15 @@ func FindClusterByNameOrARN(ctx context.Context, conn *ecs.ECS, nameOrARN string
output, err := conn.DescribeClustersWithContext(ctx, input)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed describing ECS Cluster (%s) including tags: %s; retrying without tags", nameOrARN, err)

input.Include = aws.StringSlice([]string{ecs.ClusterFieldConfigurations, ecs.ClusterFieldSettings})
output, err = conn.DescribeClustersWithContext(ctx, input)
}

// Some partitions (i.e., ISO) may not support describe including configuration, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed describing ECS Cluster (%s) including configuration: %s; retrying without configuration", nameOrARN, err)

input.Include = aws.StringSlice([]string{ecs.ClusterFieldSettings})
Expand Down Expand Up @@ -162,7 +162,7 @@ func FindServiceByIDWaitForActive(ctx context.Context, conn *ecs.ECS, id, cluste
func FindService(ctx context.Context, conn *ecs.ECS, input *ecs.DescribeServicesInput) (*ecs.Service, error) {
output, err := conn.DescribeServices(input)

if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) && input.Include != nil {
if verify.ErrorISOUnsupported(conn.PartitionID, err) && input.Include != nil {
id := aws.StringValueSlice(input.Services)[0]
log.Printf("[WARN] failed describing ECS Service (%s) with tags: %s; retrying without tags", id, err)

Expand Down
6 changes: 3 additions & 3 deletions internal/service/ecs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func resourceServiceCreate(d *schema.ResourceData, meta interface{}) error {
output, err := serviceCreateWithRetry(conn, input)

// Some partitions (i.e., ISO) may not support tag-on-create
if input.Tags != nil && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if input.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed creating ECS Service (%s) with tags: %s. Trying create without tags.", d.Get("name").(string), err)
input.Tags = nil

Expand Down Expand Up @@ -569,7 +569,7 @@ func resourceServiceCreate(d *schema.ResourceData, meta interface{}) error {
err := UpdateTags(conn, d.Id(), nil, tags)

// If default tags only, log and continue. Otherwise, error.
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if v, ok := d.GetOk("tags"); (!ok || len(v.(map[string]interface{})) == 0) && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed adding tags after create for ECS Service (%s): %s", d.Id(), err)
return resourceServiceRead(d, meta)
}
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func resourceServiceUpdate(d *schema.ResourceData, meta interface{}) error {
err := UpdateTags(conn, d.Id(), o, n)

// Some partitions (i.e., ISO) may not support tagging, giving error
if verify.CheckISOErrorTagsUnsupported(conn.PartitionID, err) {
if verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed updating tags for ECS Service (%s): %s", d.Id(), err)
return resourceServiceRead(d, meta)
}
Expand Down
Loading

0 comments on commit 506ba9e

Please sign in to comment.