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

azurerm_monitor_diagnostic_setting - remove explicitly disable log in Update #20611

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func resourceMonitorDiagnosticSetting() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: false,
Default: "AzureDiagnostics",
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"Dedicated",
"AzureDiagnostics", // Not documented in azure API, but some resource has skew. See: https://github.com/Azure/azure-rest-api-specs/issues/9281
Expand Down Expand Up @@ -413,52 +413,6 @@ func resourceMonitorDiagnosticSettingUpdate(d *pluginsdk.ResourceData, meta inte
return fmt.Errorf("at least one type of Log or Metric must be enabled")
}

if d.HasChange("enabled_log") {
oldEnabledLogs, newEnabledLogs := d.GetChange("enabled_log")

for _, oldLog := range oldEnabledLogs.(*pluginsdk.Set).List() {
logRemoved := true
oldLogMap := oldLog.(map[string]interface{})

for _, newLog := range newEnabledLogs.(*pluginsdk.Set).List() {
newLogMap := newLog.(map[string]interface{})

// check if an enabled_log has been removed from config and if so, set to disabled
if (oldLogMap["category"].(string) != "" && strings.EqualFold(oldLogMap["category"].(string), newLogMap["category"].(string))) || (oldLogMap["category_group"].(string) != "" && strings.EqualFold(oldLogMap["category_group"].(string), newLogMap["category_group"].(string))) {
logRemoved = false
break
}
}

if logRemoved {

disabledLog := diagnosticsettings.LogSettings{
Category: utils.String(oldLogMap["category"].(string)),
CategoryGroup: utils.String(oldLogMap["category_group"].(string)),
Enabled: false,
}

retentionPolicy := diagnosticsettings.RetentionPolicy{}
if v, ok := oldLogMap["retention_policy"].([]interface{}); ok {
if len(v) > 0 {

policyMap := v[0].(map[string]interface{})
if days, ok := policyMap["days"].(int); ok {
retentionPolicy.Days = int64(days)
}

if enabled, ok := policyMap["enabled"].(bool); ok {
retentionPolicy.Enabled = enabled
}
}
}
disabledLog.RetentionPolicy = &retentionPolicy

logs = append(logs, disabledLog)
}
}
}

parameters := diagnosticsettings.DiagnosticSettingsResource{
Properties: &diagnosticsettings.DiagnosticSettings{
Logs: &logs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,76 @@ func TestAccMonitorDiagnosticSetting_logAnalyticsDestinationType(t *testing.T) {
})
}

func TestAccMonitorDiagnosticSetting_enabledLogsMix(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_diagnostic_setting", "test")
r := MonitorDiagnosticSettingResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.enabledLogs(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("2"),
),
},
data.ImportStep(),
{
Config: r.enabledLogsCategoryGroupUpdated(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("1"),
),
},
data.ImportStep(),
{
Config: r.enabledLogsUpdated(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("1"),
),
},
data.ImportStep(),
{
Config: r.enabledLogsCategoryGroup(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("2"),
),
},
data.ImportStep(),
})
}

func TestAccMonitorDiagnosticSetting_enabledLogsCategoryGroup(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_diagnostic_setting", "test")
r := MonitorDiagnosticSettingResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.enabledLogsCategoryGroup(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("2"),
),
},
data.ImportStep(),
{
Config: r.enabledLogsCategoryGroupUpdated(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("1"),
),
},
data.ImportStep(),
{
Config: r.enabledLogsCategoryGroup(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("enabled_log.#").HasValue("2"),
),
},
data.ImportStep(),
})
}

func TestAccMonitorDiagnosticSetting_enabledLogs(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_diagnostic_setting", "test")
r := MonitorDiagnosticSettingResource{}
Expand Down Expand Up @@ -884,7 +954,6 @@ resource "azurerm_monitor_diagnostic_setting" "test" {
target_resource_id = azurerm_key_vault.test.id
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.test.id
eventhub_name = azurerm_eventhub.test.name
log_analytics_destination_type = "AzureDiagnostics"

enabled_log {
category = "AuditEvent"
Expand Down Expand Up @@ -968,7 +1037,6 @@ resource "azurerm_monitor_diagnostic_setting" "test" {
target_resource_id = azurerm_key_vault.test.id
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.test.id
eventhub_name = azurerm_eventhub.test.name
log_analytics_destination_type = "AzureDiagnostics"

enabled_log {
category = "AuditEvent"
Expand All @@ -992,6 +1060,163 @@ resource "azurerm_monitor_diagnostic_setting" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomIntOfLength(17))
}

func (MonitorDiagnosticSettingResource) enabledLogsCategoryGroup(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_client_config" "current" {
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_eventhub_namespace" "test" {
name = "acctest-EHN-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Basic"
}

resource "azurerm_eventhub" "test" {
name = "acctest-EH-%[1]d"
namespace_name = azurerm_eventhub_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
partition_count = 2
message_retention = 1
}

resource "azurerm_eventhub_namespace_authorization_rule" "test" {
name = "example"
namespace_name = azurerm_eventhub_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
listen = true
send = true
manage = true
}

resource "azurerm_key_vault" "test" {
name = "acctest%[3]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
}

resource "azurerm_monitor_diagnostic_setting" "test" {
name = "acctest-DS-%[1]d"
target_resource_id = azurerm_key_vault.test.id
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.test.id
eventhub_name = azurerm_eventhub.test.name

enabled_log {
category_group = "allLogs"

retention_policy {
days = 0
enabled = false
}
}

enabled_log {
category_group = "audit"

retention_policy {
days = 0
enabled = false
}
}

metric {
category = "AllMetrics"
enabled = true

retention_policy {
enabled = false
days = 7
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomIntOfLength(17))
}

func (MonitorDiagnosticSettingResource) enabledLogsCategoryGroupUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_client_config" "current" {
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_eventhub_namespace" "test" {
name = "acctest-EHN-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Basic"
}

resource "azurerm_eventhub" "test" {
name = "acctest-EH-%[1]d"
namespace_name = azurerm_eventhub_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
partition_count = 2
message_retention = 1
}

resource "azurerm_eventhub_namespace_authorization_rule" "test" {
name = "example"
namespace_name = azurerm_eventhub_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
listen = true
send = true
manage = true
}

resource "azurerm_key_vault" "test" {
name = "acctest%[3]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
}

resource "azurerm_monitor_diagnostic_setting" "test" {
name = "acctest-DS-%[1]d"
target_resource_id = azurerm_key_vault.test.id
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.test.id
eventhub_name = azurerm_eventhub.test.name

enabled_log {
category_group = "allLogs"

retention_policy {
days = 0
enabled = false
}
}

metric {
category = "AllMetrics"
enabled = true

retention_policy {
enabled = false
days = 7
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomIntOfLength(17))
}

func (MonitorDiagnosticSettingResource) logAnalyticsDestinationTypeOmit(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down