Skip to content

Commit

Permalink
Rename Create/Update function
Browse files Browse the repository at this point in the history
Add testCheckAzureRMMonitorDiagnosticsDestroy function
  • Loading branch information
Jan-Hendrik Boll authored and tombuildsstuff committed Jul 24, 2018
1 parent 6f8fb49 commit a433d5e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
6 changes: 3 additions & 3 deletions azurerm/resource_arm_monitor_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.")
Expand Down
63 changes: 54 additions & 9 deletions azurerm/resource_arm_monitor_diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -78,25 +82,66 @@ 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"]
if stateName != objectName {
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
}
}
Expand Down

0 comments on commit a433d5e

Please sign in to comment.