Skip to content

Commit

Permalink
additional acctests; reduce logging
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Jan 14, 2021
1 parent f7a9453 commit 757ee46
Show file tree
Hide file tree
Showing 4 changed files with 549 additions and 186 deletions.
6 changes: 4 additions & 2 deletions aws/internal/service/cloudwatch/finder/finder.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package finder

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)

func CompositeAlarmByName(conn *cloudwatch.CloudWatch, name string) (*cloudwatch.CompositeAlarm, error) {
func CompositeAlarmByName(ctx context.Context, conn *cloudwatch.CloudWatch, name string) (*cloudwatch.CompositeAlarm, error) {
input := cloudwatch.DescribeAlarmsInput{
AlarmNames: aws.StringSlice([]string{name}),
AlarmTypes: aws.StringSlice([]string{cloudwatch.AlarmTypeCompositeAlarm}),
}

output, err := conn.DescribeAlarms(&input)
output, err := conn.DescribeAlarmsWithContext(ctx, &input)
if err != nil {
return nil, err
}
Expand Down
64 changes: 25 additions & 39 deletions aws/resource_aws_cloudwatch_composite_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func resourceAwsCloudWatchCompositeAlarm() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},
"alarm_actions": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -86,48 +87,46 @@ func resourceAwsCloudWatchCompositeAlarm() *schema.Resource {
}
}

func resourceAwsCloudWatchCompositeAlarmCreate(
ctx context.Context,
d *schema.ResourceData,
meta interface{},
) diag.Diagnostics {
func resourceAwsCloudWatchCompositeAlarmCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).cloudwatchconn
name := d.Get("alarm_name").(string)

input := expandAwsCloudWatchPutCompositeAlarmInput(d)

_, err := conn.PutCompositeAlarmWithContext(ctx, &input)
if err != nil {
return diag.Errorf("error creating composite alarm: %s", err)
return diag.Errorf("error creating CloudWatch Composite Alarm (%s): %s", name, err)
}

log.Printf("[INFO] Created Composite Alarm %s.", name)
d.SetId(name)

return resourceAwsCloudWatchCompositeAlarmRead(ctx, d, meta)
}

func resourceAwsCloudWatchCompositeAlarmRead(
ctx context.Context,
d *schema.ResourceData,
meta interface{},
) diag.Diagnostics {
func resourceAwsCloudWatchCompositeAlarmRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).cloudwatchconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig
name := d.Id()

alarm, err := finder.CompositeAlarmByName(conn, name)
alarm, err := finder.CompositeAlarmByName(ctx, conn, name)
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, cloudwatch.ErrCodeResourceNotFound) {
log.Printf("[WARN] CloudWatch Composite Alarm %s not found, removing from state", name)
d.SetId("")
return nil
}

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

if alarm == nil {
if !d.IsNewResource() {
log.Printf("[WARN] CloudWatch Composite alarm %s not found, removing from state", name)
d.SetId("")
return nil
if d.IsNewResource() {
return diag.Errorf("error reading CloudWatch Composite Alarm (%s): not found", name)
}

return diag.Errorf("error reading composite alarm (%s): alarm not filtered", name)
log.Printf("[WARN] CloudWatch Composite Alarm %s not found, removing from state", name)
d.SetId("")
return nil
}

d.Set("actions_enabled", alarm.ActionsEnabled)
Expand Down Expand Up @@ -161,20 +160,15 @@ func resourceAwsCloudWatchCompositeAlarmRead(
return nil
}

func resourceAwsCloudWatchCompositeAlarmUpdate(
ctx context.Context,
d *schema.ResourceData,
meta interface{},
) diag.Diagnostics {
func resourceAwsCloudWatchCompositeAlarmUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).cloudwatchconn
name := d.Id()

log.Printf("[INFO] Updating Composite Alarm %s...", name)

input := expandAwsCloudWatchPutCompositeAlarmInput(d)

_, err := conn.PutCompositeAlarmWithContext(ctx, &input)
if err != nil {
return diag.Errorf("error creating composite alarm: %s", err)
return diag.Errorf("error updating CloudWatch Composite Alarm (%s): %s", name, err)
}

arn := d.Get("arn").(string)
Expand All @@ -189,16 +183,10 @@ func resourceAwsCloudWatchCompositeAlarmUpdate(
return resourceAwsCloudWatchCompositeAlarmRead(ctx, d, meta)
}

func resourceAwsCloudWatchCompositeAlarmDelete(
ctx context.Context,
d *schema.ResourceData,
meta interface{},
) diag.Diagnostics {
func resourceAwsCloudWatchCompositeAlarmDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).cloudwatchconn
name := d.Id()

log.Printf("[INFO] Deleting Composite Alarm %s...", name)

input := cloudwatch.DeleteAlarmsInput{
AlarmNames: aws.StringSlice([]string{name}),
}
Expand All @@ -208,17 +196,15 @@ func resourceAwsCloudWatchCompositeAlarmDelete(
if tfawserr.ErrCodeEquals(err, cloudwatch.ErrCodeResourceNotFound) {
return nil
}
return diag.Errorf("error deleting composite alarm: %s", err)
return diag.Errorf("error deleting CloudWatch Composite Alarm (%s): %s", name, err)
}

return nil
}

func expandAwsCloudWatchPutCompositeAlarmInput(d *schema.ResourceData) cloudwatch.PutCompositeAlarmInput {
out := cloudwatch.PutCompositeAlarmInput{}

if v, ok := d.GetOk("actions_enabled"); ok {
out.ActionsEnabled = aws.Bool(v.(bool))
out := cloudwatch.PutCompositeAlarmInput{
ActionsEnabled: aws.Bool(d.Get("actions_enabled").(bool)),
}

if v, ok := d.GetOk("alarm_actions"); ok {
Expand Down
Loading

0 comments on commit 757ee46

Please sign in to comment.