Skip to content

Commit

Permalink
Fix tag-after-create logic
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Jan 13, 2022
1 parent 39b07ae commit 4f4350b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
9 changes: 7 additions & 2 deletions internal/service/cloudwatch/composite_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ func resourceCompositeAlarmCreate(ctx context.Context, d *schema.ResourceData, m

// Some partitions (i.e., ISO) may not support tag-on-create, attempt tag after create
if input.Tags == nil && len(tags) > 0 {
arn := d.Get("arn").(string)
err := UpdateTags(conn, arn, nil, tags)
alarm, err := FindCompositeAlarmByName(ctx, conn, name)

if err != nil {
return diag.Errorf("error reading CloudWatch Composite Alarm (%s): %s", name, err)
}

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) && (tfawserr.ErrCodeContains(err, errCodeAccessDenied) || tfawserr.ErrCodeContains(err, cloudwatch.ErrCodeInternalServiceFault)) {
Expand Down
18 changes: 13 additions & 5 deletions internal/service/cloudwatch/metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,22 @@ func resourceMetricAlarmCreate(d *schema.ResourceData, meta interface{}) error {

// Some partitions (i.e., ISO) may not support tag-on-create, attempt tag after create
if params.Tags == nil && len(tags) > 0 {
arn := d.Get("arn").(string)
err := UpdateTags(conn, arn, nil, tags)
resp, err := FindMetricAlarmByName(conn, d.Id())

if err != nil {
return fmt.Errorf("while finding metric alarm (%s): %w", d.Id(), err)
}

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) && (tfawserr.ErrCodeContains(err, errCodeAccessDenied) || tfawserr.ErrCodeContains(err, cloudwatch.ErrCodeInternalServiceFault)) {
log.Printf("[WARN] error adding tags after create for CloudWatch Metric Alarm (%s): %s", d.Id(), err)
log.Printf("[WARN] could not add tags after create for CloudWatch Metric Alarm (%s): %s", d.Id(), err)
return resourceMetricAlarmRead(d, meta)
}

if err != nil {
return fmt.Errorf("error creating CloudWatch Metric Alarm (%s) tags: %s", d.Id(), err)
return fmt.Errorf("creating CloudWatch Metric Alarm (%s) tags: %w", d.Id(), err)
}
}

Expand Down Expand Up @@ -478,7 +483,10 @@ func getPutMetricAlarmInput(d *schema.ResourceData, meta interface{}) cloudwatch
ComparisonOperator: aws.String(d.Get("comparison_operator").(string)),
EvaluationPeriods: aws.Int64(int64(d.Get("evaluation_periods").(int))),
TreatMissingData: aws.String(d.Get("treat_missing_data").(string)),
Tags: Tags(tags.IgnoreAWS()),
}

if len(tags) > 0 {
params.Tags = Tags(tags.IgnoreAWS())
}

if v := d.Get("actions_enabled"); v != nil {
Expand Down

0 comments on commit 4f4350b

Please sign in to comment.