diff --git a/azurerm/resource_arm_monitor_diagnostics.go b/azurerm/resource_arm_monitor_diagnostics.go index 4c7a5dd54be89..9a4dcd2943924 100644 --- a/azurerm/resource_arm_monitor_diagnostics.go +++ b/azurerm/resource_arm_monitor_diagnostics.go @@ -11,9 +11,9 @@ import ( func resourceArmMonitorDiagnostics() *schema.Resource { return &schema.Resource{ - Create: resourceArmMonitorDiagnosticsCreate, + Create: resourceArmMonitorDiagnosticsCreateUpdate, Read: resourceArmMonitorDiagnosticsRead, - Update: resourceArmMonitorDiagnosticsCreate, + Update: resourceArmMonitorDiagnosticsCreateUpdate, Delete: resourceArmMonitorDiagnosticsDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, @@ -70,7 +70,7 @@ func resourceArmMonitorDiagnostics() *schema.Resource { } } -func resourceArmMonitorDiagnosticsCreate(d *schema.ResourceData, meta interface{}) error { +func resourceArmMonitorDiagnosticsCreateUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*ArmClient).monitorDiagnosticSettingsClient ctx := meta.(*ArmClient).StopContext log.Printf("[INFO] preparing arguments for Azure ARM Diagnostic Settings.") diff --git a/azurerm/resource_arm_monitor_diagnostics_test.go b/azurerm/resource_arm_monitor_diagnostics_test.go index eba9f9a5bee24..608244b0f3218 100644 --- a/azurerm/resource_arm_monitor_diagnostics_test.go +++ b/azurerm/resource_arm_monitor_diagnostics_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) func TestAccAzureRMMonitorDiagnostics_basic(t *testing.T) { @@ -18,8 +19,9 @@ func TestAccAzureRMMonitorDiagnostics_basic(t *testing.T) { config := testAccAzureRMMonitorDiagnostics_basic(ri, location) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorDiagnosticsDestroy, Steps: []resource.TestStep{ { Config: config, @@ -41,8 +43,9 @@ func TestAccAzureRMMonitorDiagnostics_complete(t *testing.T) { config := testAccAzureRMMonitorDiagnostics_complete(ri, location) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorDiagnosticsDestroy, Steps: []resource.TestStep{ { Config: config, @@ -66,8 +69,9 @@ func TestAccAzureRMMonitorDiagnostics_update(t *testing.T) { configUpdate := testAccAzureRMMonitorDiagnostics_update(ri, location) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorDiagnosticsDestroy, Steps: []resource.TestStep{ { Config: configBasic, @@ -78,18 +82,47 @@ func TestAccAzureRMMonitorDiagnostics_update(t *testing.T) { ), }, { - Config: configUpdate, - ExpectNonEmptyPlan: true, + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorDiagnosticsExists(resourceName, objectName), + resource.TestCheckResourceAttr(resourceName, "disabled_settings.#", "0"), + resource.TestCheckResourceAttr(resourceName, "retention_days", "30"), + ), }, }, }) } +func testCheckAzureRMMonitorDiagnosticsDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*ArmClient).monitorDiagnosticSettingsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_monitor_diagnostics" { + continue + } + + diagSettingName := rs.Primary.Attributes["name"] + targetResourceId := rs.Primary.Attributes["target_resource_id"] + resp, err := conn.Get(ctx, targetResourceId, diagSettingName) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return nil + } + + return fmt.Errorf("Error while retrieving DiagnosticSettings %v", err) + } + return fmt.Errorf("DiagnosticSettings %s still exists", diagSettingName) + } + return nil +} + func testCheckAzureRMMonitorDiagnosticsExists(name, objectName string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Did not find resource %s in state", name) } stateName := rs.Primary.Attributes["name"] @@ -97,6 +130,18 @@ func testCheckAzureRMMonitorDiagnosticsExists(name, objectName string) resource. return fmt.Errorf("State inconsistent, %s does not match state name %s", stateName, objectName) } + conn := testAccProvider.Meta().(*ArmClient).monitorDiagnosticSettingsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + targetResourceId := rs.Primary.Attributes["target_resource_id"] + + resp, err := conn.Get(ctx, targetResourceId, objectName) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("DiagnosticSettings %s did not exist for resource %s", objectName, targetResourceId) + } + return fmt.Errorf("Error while retrieving DiagnosticSettings %v", err) + } + return nil } }