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 2 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
36 changes: 33 additions & 3 deletions aws/resource_aws_cloudwatch_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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/hashicorp/terraform/terraform" // TODO: (JP) Is this reference still correct?
)

func resourceAwsCloudWatchDashboard() *schema.Resource {
Expand Down Expand Up @@ -76,10 +77,20 @@ func resourceAwsCloudWatchDashboardRead(d *schema.ResourceData, meta interface{}
}

func resourceAwsCloudWatchDashboardPut(d *schema.ResourceData, meta interface{}) error {
var prevState *terraform.InstanceState = d.State()
var dashboardNameBefore string

dashboardBody := d.Get("dashboard_body")
dashboardName := d.Get("dashboard_name")

if prevState != nil {
dashboardNameBefore = (*d.State()).ID
}

conn := meta.(*AWSClient).cloudwatchconn
params := cloudwatch.PutDashboardInput{
DashboardBody: aws.String(d.Get("dashboard_body").(string)),
DashboardName: aws.String(d.Get("dashboard_name").(string)),
DashboardBody: aws.String(dashboardBody.(string)),
DashboardName: aws.String(dashboardName.(string)),
}

log.Printf("[DEBUG] Putting CloudWatch Dashboard: %#v", params)
Expand All @@ -88,9 +99,28 @@ func resourceAwsCloudWatchDashboardPut(d *schema.ResourceData, meta interface{})
if err != nil {
return fmt.Errorf("Putting dashboard failed: %s", err)
}
d.SetId(d.Get("dashboard_name").(string))
d.SetId(dashboardName.(string))

log.Println("[INFO] CloudWatch Dashboard put finished")

if prevState != nil && dashboardNameBefore != dashboardName {
log.Printf("[INFO] Dashboard name changed, cleaning up previous dashboard: %s", dashboardNameBefore)

newConn := meta.(*AWSClient).cloudwatchconn
params := cloudwatch.DeleteDashboardsInput{
DashboardNames: []*string{aws.String(dashboardNameBefore)},
}

if _, err := newConn.DeleteDashboards(&params); err != nil {
if isCloudWatchDashboardNotFoundErr(err) {
return nil
}
return fmt.Errorf("Error deleting CloudWatch Dashboard: %s", dashboardNameBefore)
}

log.Printf("[INFO] CloudWatch Dashboard %s deleted", dashboardNameBefore)
}

return resourceAwsCloudWatchDashboardRead(d, meta)
}

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