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

Refactor cloudwatch event rule resource to use keyvaluetags + refactor tests #11346

Merged
merged 9 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 28 additions & 37 deletions aws/resource_aws_cloudwatch_event_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsCloudWatchEventRule() *schema.Resource {
Expand Down Expand Up @@ -61,7 +62,7 @@ func resourceAwsCloudWatchEventRule() *schema.Resource {
"role_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 1600),
ValidateFunc: validateArn,
},
"is_enabled": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -122,11 +123,7 @@ func resourceAwsCloudWatchEventRuleCreate(d *schema.ResourceData, meta interface

log.Printf("[INFO] CloudWatch Event Rule %q created", *out.RuleArn)

if err := setTagsCloudWatchEvents(conn, d, aws.StringValue(out.RuleArn)); err != nil {
return fmt.Errorf("Error creating tags for %s: %s", d.Id(), err)
}

return resourceAwsCloudWatchEventRuleUpdate(d, meta)
return resourceAwsCloudWatchEventRuleRead(d, meta)
}

func resourceAwsCloudWatchEventRuleRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -138,7 +135,7 @@ func resourceAwsCloudWatchEventRuleRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Reading CloudWatch Event Rule: %s", input)
out, err := conn.DescribeRule(&input)
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ResourceNotFoundException" {
if awsErr.Code() == events.ErrCodeResourceNotFoundException {
log.Printf("[WARN] Removing CloudWatch Event Rule %q because it's gone.", d.Id())
d.SetId("")
return nil
Expand All @@ -149,7 +146,8 @@ func resourceAwsCloudWatchEventRuleRead(d *schema.ResourceData, meta interface{}
}
log.Printf("[DEBUG] Found Event Rule: %s", out)

d.Set("arn", out.Arn)
arn := *out.Arn
d.Set("arn", arn)
d.Set("description", out.Description)
if out.EventPattern != nil {
pattern, err := structure.NormalizeJsonString(*out.EventPattern)
Expand All @@ -168,26 +166,23 @@ func resourceAwsCloudWatchEventRuleRead(d *schema.ResourceData, meta interface{}
}
log.Printf("[DEBUG] Setting boolean state: %t", boolState)
d.Set("is_enabled", boolState)
if err := saveTagsCloudWatchEvents(conn, d, aws.StringValue(out.Arn)); err != nil {

tags, err := keyvaluetags.CloudwatcheventsListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for CloudWatch Event Rule (%s): %s", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

func resourceAwsCloudWatchEventRuleUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn

if d.HasChange("is_enabled") && d.Get("is_enabled").(bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to understand how removing this code was not breaking is_enabled functionality 😅 and my guess is that when the original implementation was done years ago, the PutRule API State parameter was not properly disabling/enabling rules.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was scratching my head for a while after seeing this

log.Printf("[DEBUG] Enabling CloudWatch Event Rule %q", d.Id())
_, err := conn.EnableRule(&events.EnableRuleInput{
Name: aws.String(d.Id()),
})
if err != nil {
return err
}
log.Printf("[DEBUG] CloudWatch Event Rule (%q) enabled", d.Id())
}

input, err := buildPutRuleInputStruct(d, d.Id())
if err != nil {
return fmt.Errorf("Updating CloudWatch Event Rule failed: %s", err)
Expand Down Expand Up @@ -215,20 +210,12 @@ func resourceAwsCloudWatchEventRuleUpdate(d *schema.ResourceData, meta interface
return fmt.Errorf("Updating CloudWatch Event Rule failed: %s", err)
}

if d.HasChange("is_enabled") && !d.Get("is_enabled").(bool) {
log.Printf("[DEBUG] Disabling CloudWatch Event Rule %q", d.Id())
_, err := conn.DisableRule(&events.DisableRuleInput{
Name: aws.String(d.Id()),
})
if err != nil {
return err
}
log.Printf("[DEBUG] CloudWatch Event Rule (%q) disabled", d.Id())
}

arn := d.Get("arn").(string)
if d.HasChange("tags") {
if err := setTagsCloudWatchEvents(conn, d, d.Get("arn").(string)); err != nil {
return fmt.Errorf("Error updating tags for %s: %s", d.Id(), err)
o, n := d.GetChange("tags")

if err := keyvaluetags.CloudwatcheventsUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating CloudwWatch Event Rule (%s) tags: %s", arn, err)
}
}

Expand Down Expand Up @@ -271,16 +258,20 @@ func buildPutRuleInputStruct(d *schema.ResourceData, name string) (*events.PutRu
input.ScheduleExpression = aws.String(v.(string))
}

if v, ok := d.GetOk("tags"); ok {
input.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().CloudwatcheventsTags()
}

input.State = aws.String(getStringStateFromBoolean(d.Get("is_enabled").(bool)))

return &input, nil
}

// State is represented as (ENABLED|DISABLED) in the API
func getBooleanStateFromString(state string) (bool, error) {
if state == "ENABLED" {
if state == events.RuleStateEnabled {
return true, nil
} else if state == "DISABLED" {
} else if state == events.RuleStateDisabled {
return false, nil
}
// We don't just blindly trust AWS as they tend to return
Expand All @@ -291,9 +282,9 @@ func getBooleanStateFromString(state string) (bool, error) {
// State is represented as (ENABLED|DISABLED) in the API
func getStringStateFromBoolean(isEnabled bool) string {
if isEnabled {
return "ENABLED"
return events.RuleStateEnabled
}
return "DISABLED"
return events.RuleStateDisabled
}

func validateEventPatternValue() schema.SchemaValidateFunc {
Expand Down
Loading