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

Fixed remnant CloudWatch Dashboards on each dashboard rename #9784

Merged
merged 4 commits into from
Jan 2, 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
1 change: 1 addition & 0 deletions aws/resource_aws_cloudwatch_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func resourceAwsCloudWatchDashboard() *schema.Resource {
"dashboard_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCloudWatchDashboardName,
},
},
Expand Down
52 changes: 52 additions & 0 deletions aws/resource_aws_cloudwatch_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ func TestAccAWSCloudWatchDashboard_update(t *testing.T) {
})
}

func TestAccAWSCloudWatchDashboard_updateName(t *testing.T) {
var dashboard cloudwatch.GetDashboardOutput
rInt := acctest.RandInt()
rInt2 := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchDashboardDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchDashboardConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchDashboardExists("aws_cloudwatch_dashboard.foobar", &dashboard),
Copy link
Contributor

Choose a reason for hiding this comment

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

Likely copy-paste from the earlier iteration of this pull request, the test configuration resource name is aws_cloudwatch_dashboard.test:

https://github.com/terraform-providers/terraform-provider-aws/blob/e1fc857056e66b257520f73ca5e22dc6facaebef/aws/resource_aws_cloudwatch_dashboard_test.go#L159

Otherwise, this test causes this error:

--- FAIL: TestAccAWSCloudWatchDashboard_updateName (5.64s)
    testing.go:640: Step 0 error: Check failed: Check 1/3 error: Not found: aws_cloudwatch_dashboard.foobar

Since it is a small testing fix, will do so on merge. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gah, I was so close. Thanks!

testAccCloudWatchCheckDashboardBodyIsExpected("aws_cloudwatch_dashboard.foobar", basicWidget),
resource.TestCheckResourceAttr("aws_cloudwatch_dashboard.foobar", "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
),
},
{
Config: testAccAWSCloudWatchDashboardConfig(rInt2),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchDashboardExists("aws_cloudwatch_dashboard.foobar", &dashboard),
testAccCloudWatchCheckDashboardBodyIsExpected("aws_cloudwatch_dashboard.foobar", basicWidget),
resource.TestCheckResourceAttr("aws_cloudwatch_dashboard.foobar", "dashboard_name", testAccAWSCloudWatchDashboardName(rInt2)),
testAccCheckAWSCloudWatchDashboardDestroyPrevious(testAccAWSCloudWatchDashboardName(rInt)),
),
},
},
})
}

func testAccCheckCloudWatchDashboardExists(n string, dashboard *cloudwatch.GetDashboardOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -122,6 +152,28 @@ func testAccCheckAWSCloudWatchDashboardDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSCloudWatchDashboardDestroyPrevious(dashboardName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn

params := cloudwatch.GetDashboardInput{
DashboardName: aws.String(dashboardName),
}

_, err := conn.GetDashboard(&params)

if err == nil {
return fmt.Errorf("Dashboard still exists: %s", dashboardName)
}

if !isCloudWatchDashboardNotFoundErr(err) {
return err
}

return nil
}
}

const (
basicWidget = `{
"widgets": [{
Expand Down