Skip to content

Commit

Permalink
azurerm_monitor_diagnostic_setting - mark retention_policy & retentio…
Browse files Browse the repository at this point in the history
…n_policy as optional (#6603)
  • Loading branch information
nyuen committed Apr 26, 2020
1 parent 6356c26 commit 691f888
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 28 deletions.
Expand Up @@ -107,7 +107,7 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource {

"retention_policy": {
Type: schema.TypeList,
Required: true,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -146,7 +146,7 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource {

"retention_policy": {
Type: schema.TypeList,
Required: true,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -384,19 +384,22 @@ func expandMonitorDiagnosticsSettingsLogs(input []interface{}) []insights.LogSet

category := v["category"].(string)
enabled := v["enabled"].(bool)

policiesRaw := v["retention_policy"].([]interface{})
policyRaw := policiesRaw[0].(map[string]interface{})
retentionDays := policyRaw["days"].(int)
retentionEnabled := policyRaw["enabled"].(bool)

output := insights.LogSettings{
Category: utils.String(category),
Enabled: utils.Bool(enabled),
RetentionPolicy: &insights.RetentionPolicy{
var retentionPolicy *insights.RetentionPolicy
if len(policiesRaw) != 0 {
policyRaw := policiesRaw[0].(map[string]interface{})
retentionDays := policyRaw["days"].(int)
retentionEnabled := policyRaw["enabled"].(bool)
retentionPolicy = &insights.RetentionPolicy{
Days: utils.Int32(int32(retentionDays)),
Enabled: utils.Bool(retentionEnabled),
},
}
}

output := insights.LogSettings{
Category: utils.String(category),
Enabled: utils.Bool(enabled),
RetentionPolicy: retentionPolicy,
}

results = append(results, output)
Expand Down Expand Up @@ -456,18 +459,20 @@ func expandMonitorDiagnosticsSettingsMetrics(input []interface{}) []insights.Met
enabled := v["enabled"].(bool)

policiesRaw := v["retention_policy"].([]interface{})
policyRaw := policiesRaw[0].(map[string]interface{})

retentionDays := policyRaw["days"].(int)
retentionEnabled := policyRaw["enabled"].(bool)

output := insights.MetricSettings{
Category: utils.String(category),
Enabled: utils.Bool(enabled),
RetentionPolicy: &insights.RetentionPolicy{
var retentionPolicy *insights.RetentionPolicy
if policiesRaw != nil {
policyRaw := policiesRaw[0].(map[string]interface{})
retentionDays := policyRaw["days"].(int)
retentionEnabled := policyRaw["enabled"].(bool)
retentionPolicy = &insights.RetentionPolicy{
Days: utils.Int32(int32(retentionDays)),
Enabled: utils.Bool(retentionEnabled),
},
}
}
output := insights.MetricSettings{
Category: utils.String(category),
Enabled: utils.Bool(enabled),
RetentionPolicy: retentionPolicy,
}

results = append(results, output)
Expand Down
Expand Up @@ -14,7 +14,6 @@ import (

func TestAccAzureRMMonitorDiagnosticSetting_eventhub(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_diagnostic_setting", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
Expand Down Expand Up @@ -98,9 +97,6 @@ func TestAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspaceDedicated(t *te
resource.TestCheckResourceAttrSet(data.ResourceName, "log_analytics_workspace_id"),
resource.TestCheckResourceAttr(data.ResourceName, "log_analytics_destination_type", "Dedicated"),
resource.TestCheckResourceAttr(data.ResourceName, "log.#", "3"),
resource.TestCheckResourceAttr(data.ResourceName, "log.3188484811.category", "ActivityRuns"),
resource.TestCheckResourceAttr(data.ResourceName, "log.595859111.category", "PipelineRuns"),
resource.TestCheckResourceAttr(data.ResourceName, "log.2542277390.category", "TriggerRuns"),
resource.TestCheckResourceAttr(data.ResourceName, "metric.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "metric.4109484471.category", "AllMetrics"),
),
Expand Down Expand Up @@ -134,6 +130,28 @@ func TestAccAzureRMMonitorDiagnosticSetting_storageAccount(t *testing.T) {
})
}

func TestAccAzureRMMonitorDiagnosticSetting_activityLog(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_diagnostic_setting", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMonitorDiagnosticSettingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMMonitorDiagnosticSetting_activityLog(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMonitorDiagnosticSettingExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_id"),
resource.TestCheckResourceAttr(data.ResourceName, "log.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "log.782743152.category", "Administrative"),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMMonitorDiagnosticSettingExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Monitor.DiagnosticSettingsClient
Expand Down Expand Up @@ -473,3 +491,44 @@ resource "azurerm_monitor_diagnostic_setting" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomIntOfLength(17))
}

func testAccAzureRMMonitorDiagnosticSetting_activityLog(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
data "azurerm_client_config" "current" {
}
data "azurerm_subscription" "current" {
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}
resource "azurerm_storage_account" "test" {
name = "acctest%[3]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_replication_type = "LRS"
account_tier = "Standard"
}
resource "azurerm_monitor_diagnostic_setting" "test" {
name = "acctest-DS-%[1]d"
target_resource_id = data.azurerm_subscription.current.id
storage_account_id = azurerm_storage_account.test.id
log {
category = "Administrative"
enabled = true
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomIntOfLength(17))
}
4 changes: 2 additions & 2 deletions website/docs/r/monitor_diagnostic_setting.html.markdown
Expand Up @@ -99,7 +99,7 @@ A `log` block supports the following:

-> **NOTE:** The Log Categories available vary depending on the Resource being used. You may wish to use [the `azurerm_monitor_diagnostic_categories` Data Source](../d/monitor_diagnostic_categories.html) to identify which categories are available for a given Resource.

* `retention_policy` - (Required) A `retention_policy` block as defined below.
* `retention_policy` - (Optional) A `retention_policy` block as defined below.

* `enabled` - (Optional) Is this Diagnostic Log enabled? Defaults to `true`.

Expand All @@ -111,7 +111,7 @@ A `metric` block supports the following:

-> **NOTE:** The Metric Categories available vary depending on the Resource being used. You may wish to use [the `azurerm_monitor_diagnostic_categories` Data Source](../d/monitor_diagnostic_categories.html) to identify which categories are available for a given Resource.

* `retention_policy` - (Required) A `retention_policy` block as defined below.
* `retention_policy` - (Optional) A `retention_policy` block as defined below.

* `enabled` - (Optional) Is this Diagnostic Metric enabled? Defaults to `true`.

Expand Down

0 comments on commit 691f888

Please sign in to comment.