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

Feature/fix removed resource error handling for aws cloudwatch event permission resource #9044

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
15 changes: 14 additions & 1 deletion aws/resource_aws_cloudwatch_event_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func resourceAwsCloudWatchEventPermissionRead(d *schema.ResourceData, meta inter
}

if debo.Policy == nil {
return resource.RetryableError(fmt.Errorf("CloudWatch Events permission %q not found", d.Id()))
return resource.RetryableError(&resource.NotFoundError{
Message: fmt.Sprintf("CloudWatch Events permission %q not found"+
"in given results from DescribeEventBus", d.Id()),
LastResponse: debo,
LastRequest: input,
})
}

err = json.Unmarshal([]byte(*debo.Policy), &policyDoc)
Expand Down Expand Up @@ -167,6 +172,11 @@ func resourceAwsCloudWatchEventPermissionUpdate(d *schema.ResourceData, meta int

log.Printf("[DEBUG] Update CloudWatch Events permission: %s", input)
_, err := conn.PutPermission(&input)
if isAWSErr(err, events.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] CloudWatch Events permission %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Updating CloudWatch Events permission '%s' failed: %s", d.Id(), err.Error())
}
Expand All @@ -182,6 +192,9 @@ func resourceAwsCloudWatchEventPermissionDelete(d *schema.ResourceData, meta int

log.Printf("[DEBUG] Delete CloudWatch Events permission: %s", input)
_, err := conn.RemovePermission(&input)
if isAWSErr(err, events.ErrCodeResourceNotFoundException, "") {
return nil
}
if err != nil {
return fmt.Errorf("Deleting CloudWatch Events permission '%s' failed: %s", d.Id(), err.Error())
}
Expand Down
42 changes: 42 additions & 0 deletions aws/resource_aws_cloudwatch_event_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,48 @@ func TestAccAWSCloudWatchEventPermission_Multiple(t *testing.T) {
})
}

func TestAccAWSCloudWatchEventPermission_Disappears(t *testing.T) {
resourceName := "aws_cloudwatch_event_permission.test1"
principal := "111111111111"
statementID := acctest.RandomWithPrefix(t.Name())[:64]

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudWatchEventPermissionDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsCloudWatchEventPermissionResourceConfigBasic(principal, statementID),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventPermissionExists(resourceName),
testAccCheckCloudWatchEventPermissionDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckCloudWatchEventPermissionDisappears(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No resource ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
input := events.RemovePermissionInput{
StatementId: aws.String(rs.Primary.ID),
}
_, err := conn.RemovePermission(&input)
return err
}
}

func testAccCheckCloudWatchEventPermissionExists(pr string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
Expand Down