From 3a4d843b0e4b1a5df0348cb2140173908df04643 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Thu, 1 Aug 2019 14:35:55 +1000 Subject: [PATCH 1/9] Added LogAnalyticsDestinationType --- .../resource_arm_monitor_diagnostic_setting.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting.go b/azurerm/resource_arm_monitor_diagnostic_setting.go index ba1a98812452..1d96bcfd06de 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting.go @@ -73,6 +73,19 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource { ValidateFunc: azure.ValidateResourceID, }, + "log_analytics_destination_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + v := val.(string) + if v != "Dedicated" { + errs = append(errs, fmt.Errorf("%q must be 'Dedicated'", key, v)) + } + return + }, + }, + "log": { Type: schema.TypeSet, Optional: true, @@ -234,6 +247,11 @@ func resourceArmMonitorDiagnosticSettingCreateUpdate(d *schema.ResourceData, met valid = true } + logAnalyticsDestinationType := d.Get("log_analytics_destination_type").(string) + if logAnalyticsDestinationType != "" { + properties.DiagnosticSettings.LogAnalyticsDestinationType = utils.String(logAnalyticsDestinationType) + } + if !valid { return fmt.Errorf("Either a `eventhub_authorization_rule_id`, `log_analytics_workspace_id` or `storage_account_id` must be set") } From b1bfac682e69685c809ef2d9d2c31511ecbff6e8 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Thu, 1 Aug 2019 15:02:01 +1000 Subject: [PATCH 2/9] Updated the docs --- azurerm/resource_arm_monitor_diagnostic_setting.go | 2 +- website/docs/r/monitor_diagnostic_setting.html.markdown | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting.go b/azurerm/resource_arm_monitor_diagnostic_setting.go index 1d96bcfd06de..7af0e14b8661 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting.go @@ -76,7 +76,7 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource { "log_analytics_destination_type": { Type: schema.TypeString, Optional: true, - ForceNew: true, + ForceNew: false, ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { v := val.(string) if v != "Dedicated" { diff --git a/website/docs/r/monitor_diagnostic_setting.html.markdown b/website/docs/r/monitor_diagnostic_setting.html.markdown index a5dd600012dd..5dae6b9c0d22 100644 --- a/website/docs/r/monitor_diagnostic_setting.html.markdown +++ b/website/docs/r/monitor_diagnostic_setting.html.markdown @@ -87,6 +87,10 @@ The following arguments are supported: -> **NOTE:** One of `eventhub_authorization_rule_id`, `log_analytics_workspace_id` and `storage_account_id` must be specified. +* `` - (Optional) When set to 'Dedicated' logs sent to a Log Analytics workspace will go into resource specific tables, instead of the legacy AzureDiagnostics table. + +-> ***NOTE:** This setting will only have an effect if a `log_analytics_workspace_id` is provided, and the resource is available for resource-specific logs. As of July 2019, this only includes Azure Data Factory. Please [see the documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/diagnostic-logs-stream-log-store#azure-diagnostics-vs-resource-specific) for more information. + --- A `log` block supports the following: From 04f5f4ce455feb4445bd431ec3314470972eaf94 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Thu, 1 Aug 2019 15:03:01 +1000 Subject: [PATCH 3/9] Added the attribute to the doc --- website/docs/r/monitor_diagnostic_setting.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/monitor_diagnostic_setting.html.markdown b/website/docs/r/monitor_diagnostic_setting.html.markdown index 5dae6b9c0d22..6eedd28058ea 100644 --- a/website/docs/r/monitor_diagnostic_setting.html.markdown +++ b/website/docs/r/monitor_diagnostic_setting.html.markdown @@ -87,7 +87,7 @@ The following arguments are supported: -> **NOTE:** One of `eventhub_authorization_rule_id`, `log_analytics_workspace_id` and `storage_account_id` must be specified. -* `` - (Optional) When set to 'Dedicated' logs sent to a Log Analytics workspace will go into resource specific tables, instead of the legacy AzureDiagnostics table. +* `log_analytics_destination_type` - (Optional) When set to 'Dedicated' logs sent to a Log Analytics workspace will go into resource specific tables, instead of the legacy AzureDiagnostics table. -> ***NOTE:** This setting will only have an effect if a `log_analytics_workspace_id` is provided, and the resource is available for resource-specific logs. As of July 2019, this only includes Azure Data Factory. Please [see the documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/diagnostic-logs-stream-log-store#azure-diagnostics-vs-resource-specific) for more information. From 2cbf507176dafe26ec73ac4f2b6391858de06086 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Thu, 1 Aug 2019 15:52:22 +1000 Subject: [PATCH 4/9] Added the value to the error string --- azurerm/resource_arm_monitor_diagnostic_setting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting.go b/azurerm/resource_arm_monitor_diagnostic_setting.go index 7af0e14b8661..b3b116d1d2e9 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting.go @@ -80,7 +80,7 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource { ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { v := val.(string) if v != "Dedicated" { - errs = append(errs, fmt.Errorf("%q must be 'Dedicated'", key, v)) + errs = append(errs, fmt.Errorf("%q must be 'Dedicated', got %q", key, v)) } return }, From 2512f3bdc3ee7dafe1f957a683d2cffae8363245 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Fri, 2 Aug 2019 09:22:42 +1000 Subject: [PATCH 5/9] Added property read and simplified validation --- ...resource_arm_monitor_diagnostic_setting.go | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting.go b/azurerm/resource_arm_monitor_diagnostic_setting.go index b3b116d1d2e9..0bb13ea12072 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting.go @@ -74,16 +74,10 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource { }, "log_analytics_destination_type": { - Type: schema.TypeString, - Optional: true, - ForceNew: false, - ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { - v := val.(string) - if v != "Dedicated" { - errs = append(errs, fmt.Errorf("%q must be 'Dedicated', got %q", key, v)) - } - return - }, + Type: schema.TypeString, + Optional: true, + ForceNew: false, + ValidateFunc: validation.StringInSlice([]string{"Dedicated"}, false), }, "log": { @@ -247,9 +241,8 @@ func resourceArmMonitorDiagnosticSettingCreateUpdate(d *schema.ResourceData, met valid = true } - logAnalyticsDestinationType := d.Get("log_analytics_destination_type").(string) - if logAnalyticsDestinationType != "" { - properties.DiagnosticSettings.LogAnalyticsDestinationType = utils.String(logAnalyticsDestinationType) + if v := d.Get("log_analytics_destination_type").(string); v != "" { + properties.DiagnosticSettings.LogAnalyticsDestinationType = &v } if !valid { @@ -305,6 +298,8 @@ func resourceArmMonitorDiagnosticSettingRead(d *schema.ResourceData, meta interf d.Set("log_analytics_workspace_id", resp.WorkspaceID) d.Set("storage_account_id", resp.StorageAccountID) + d.Set("log_analytics_destination_type", resp.LogAnalyticsDestinationType) + if err := d.Set("log", flattenMonitorDiagnosticLogs(resp.Logs)); err != nil { return fmt.Errorf("Error setting `log`: %+v", err) } From 975fdca028353545b95ad41ff5652d28d886f784 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Fri, 2 Aug 2019 09:33:04 +1000 Subject: [PATCH 6/9] Added check that the log analytics workspace id is provided --- azurerm/resource_arm_monitor_diagnostic_setting.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting.go b/azurerm/resource_arm_monitor_diagnostic_setting.go index 0bb13ea12072..fb44b65d9128 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting.go @@ -242,7 +242,11 @@ func resourceArmMonitorDiagnosticSettingCreateUpdate(d *schema.ResourceData, met } if v := d.Get("log_analytics_destination_type").(string); v != "" { - properties.DiagnosticSettings.LogAnalyticsDestinationType = &v + if workspaceId != "" { + properties.DiagnosticSettings.LogAnalyticsDestinationType = &v + } else { + return fmt.Errorf("`log_analytics_workspace_id` must be set for `log_analytics_destination_type` to be used") + } } if !valid { From f27ac8b32964ba4264aa624430dd1e6312d08354 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Fri, 2 Aug 2019 09:49:30 +1000 Subject: [PATCH 7/9] Added log_analytics_destination_type to the logAnalyticsWorkspace test --- azurerm/resource_arm_monitor_diagnostic_setting_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting_test.go b/azurerm/resource_arm_monitor_diagnostic_setting_test.go index 33a694fc8d46..4d2d03c70320 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting_test.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting_test.go @@ -86,6 +86,7 @@ func TestAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspace(t *testing.T) Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorDiagnosticSettingExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "log_analytics_workspace_id"), + resource.TestCheckResourceAttr(resourceName, "log_analytics_destination_type", "Dedicated"), resource.TestCheckResourceAttr(resourceName, "log.#", "1"), resource.TestCheckResourceAttr(resourceName, "log.782743152.category", "AuditEvent"), resource.TestCheckResourceAttr(resourceName, "metric.#", "1"), @@ -318,6 +319,8 @@ resource "azurerm_monitor_diagnostic_setting" "test" { target_resource_id = "${azurerm_key_vault.test.id}" log_analytics_workspace_id = "${azurerm_log_analytics_workspace.test.id}" + log_analytics_destination_type = "Dedicated" + log { category = "AuditEvent" enabled = false From 31f2d3c2201841422a910fa4d568b21cf3c85356 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Tue, 6 Aug 2019 11:23:23 +1000 Subject: [PATCH 8/9] Moved test changes to new test TestAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspaceDedicated. --- ...rce_arm_monitor_diagnostic_setting_test.go | 104 +++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_monitor_diagnostic_setting_test.go b/azurerm/resource_arm_monitor_diagnostic_setting_test.go index 4d2d03c70320..2ccd6a14a6a5 100644 --- a/azurerm/resource_arm_monitor_diagnostic_setting_test.go +++ b/azurerm/resource_arm_monitor_diagnostic_setting_test.go @@ -86,7 +86,6 @@ func TestAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspace(t *testing.T) Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorDiagnosticSettingExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "log_analytics_workspace_id"), - resource.TestCheckResourceAttr(resourceName, "log_analytics_destination_type", "Dedicated"), resource.TestCheckResourceAttr(resourceName, "log.#", "1"), resource.TestCheckResourceAttr(resourceName, "log.782743152.category", "AuditEvent"), resource.TestCheckResourceAttr(resourceName, "metric.#", "1"), @@ -102,6 +101,39 @@ func TestAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspace(t *testing.T) }) } +func TestAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspaceDedicated(t *testing.T) { + resourceName := "azurerm_monitor_diagnostic_setting.test" + ri := acctest.RandIntRange(10000, 99999) + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorDiagnosticSettingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspaceDedicated(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorDiagnosticSettingExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "log_analytics_workspace_id"), + resource.TestCheckResourceAttr(resourceName, "log_analytics_destination_type", "Dedicated"), + resource.TestCheckResourceAttr(resourceName, "log.#", "3"), + resource.TestCheckResourceAttr(resourceName, "log.3188484811.category", "ActivityRuns"), + resource.TestCheckResourceAttr(resourceName, "log.595859111.category", "PipelineRuns"), + resource.TestCheckResourceAttr(resourceName, "log.2542277390.category", "TriggerRuns"), + resource.TestCheckResourceAttr(resourceName, "metric.#", "1"), + resource.TestCheckResourceAttr(resourceName, "metric.4109484471.category", "AllMetrics"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAzureRMMonitorDiagnosticSetting_storageAccount(t *testing.T) { resourceName := "azurerm_monitor_diagnostic_setting.test" ri := acctest.RandIntRange(10000, 99999) @@ -319,8 +351,6 @@ resource "azurerm_monitor_diagnostic_setting" "test" { target_resource_id = "${azurerm_key_vault.test.id}" log_analytics_workspace_id = "${azurerm_log_analytics_workspace.test.id}" - log_analytics_destination_type = "Dedicated" - log { category = "AuditEvent" enabled = false @@ -341,6 +371,74 @@ resource "azurerm_monitor_diagnostic_setting" "test" { `, rInt, location, rInt, rInt, rInt) } +func testAccAzureRMMonitorDiagnosticSetting_logAnalyticsWorkspaceDedicated(rInt int, location string) string { + return fmt.Sprintf(` +data "azurerm_client_config" "current" {} + +resource "azurerm_resource_group" "test" { + name = "acctest%d" + location = "%s" +} + +resource "azurerm_log_analytics_workspace" "test" { + name = "acctestlaw%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "PerGB2018" + retention_in_days = 30 +} + +resource "azurerm_data_factory" "test" { + name = "acctestdf%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +resource "azurerm_monitor_diagnostic_setting" "test" { + name = "acctestds%d" + target_resource_id = "${azurerm_data_factory.test.id}" + log_analytics_workspace_id = "${azurerm_log_analytics_workspace.test.id}" + + log_analytics_destination_type = "Dedicated" + + log { + category = "ActivityRuns" + + retention_policy { + enabled = false + } + } + + log { + category = "PipelineRuns" + enabled = false + + retention_policy { + enabled = false + } + } + + log { + category = "TriggerRuns" + enabled = false + + retention_policy { + enabled = false + } + } + + metric { + category = "AllMetrics" + enabled = false + + retention_policy { + enabled = false + } + } +} +`, rInt, location, rInt, rInt, rInt) +} + func testAccAzureRMMonitorDiagnosticSetting_storageAccount(rInt int, location string) string { return fmt.Sprintf(` data "azurerm_client_config" "current" {} From c8eff5395308507f85e9c00c4ce03ef1a8f49049 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 9 Aug 2019 10:51:45 +0100 Subject: [PATCH 9/9] r/monitor_diagnostic_setting: fixing the formatting of the note --- website/docs/r/monitor_diagnostic_setting.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/monitor_diagnostic_setting.html.markdown b/website/docs/r/monitor_diagnostic_setting.html.markdown index 6eedd28058ea..99db90eacbd6 100644 --- a/website/docs/r/monitor_diagnostic_setting.html.markdown +++ b/website/docs/r/monitor_diagnostic_setting.html.markdown @@ -89,7 +89,7 @@ The following arguments are supported: * `log_analytics_destination_type` - (Optional) When set to 'Dedicated' logs sent to a Log Analytics workspace will go into resource specific tables, instead of the legacy AzureDiagnostics table. --> ***NOTE:** This setting will only have an effect if a `log_analytics_workspace_id` is provided, and the resource is available for resource-specific logs. As of July 2019, this only includes Azure Data Factory. Please [see the documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/diagnostic-logs-stream-log-store#azure-diagnostics-vs-resource-specific) for more information. +-> **NOTE:** This setting will only have an effect if a `log_analytics_workspace_id` is provided, and the resource is available for resource-specific logs. As of July 2019, this only includes Azure Data Factory. Please [see the documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/diagnostic-logs-stream-log-store#azure-diagnostics-vs-resource-specific) for more information. ---