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 - added LogAnalyticsDestinationType #3987

17 changes: 17 additions & 0 deletions azurerm/resource_arm_monitor_diagnostic_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func resourceArmMonitorDiagnosticSetting() *schema.Resource {
ValidateFunc: azure.ValidateResourceID,
},

"log_analytics_destination_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
ValidateFunc: validation.StringInSlice([]string{"Dedicated"}, false),
},

"log": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -234,6 +241,14 @@ func resourceArmMonitorDiagnosticSettingCreateUpdate(d *schema.ResourceData, met
valid = true
}

if v := d.Get("log_analytics_destination_type").(string); 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 {
return fmt.Errorf("Either a `eventhub_authorization_rule_id`, `log_analytics_workspace_id` or `storage_account_id` must be set")
}
Expand Down Expand Up @@ -287,6 +302,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)
}
Expand Down
101 changes: 101 additions & 0 deletions azurerm/resource_arm_monitor_diagnostic_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,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)
Expand Down Expand Up @@ -338,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" {}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/monitor_diagnostic_setting.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

* `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.
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

---

A `log` block supports the following:
Expand Down