diff --git a/azurerm/data_source_monitor_diagnostic_categories.go b/azurerm/data_source_monitor_diagnostic_categories.go new file mode 100644 index 000000000000..8ee9b8741ac2 --- /dev/null +++ b/azurerm/data_source_monitor_diagnostic_categories.go @@ -0,0 +1,89 @@ +package azurerm + +import ( + "fmt" + "strings" + + "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights" + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +func dataSourceArmMonitorDiagnosticCategories() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmMonitorDiagnosticCategoriesRead, + Schema: map[string]*schema.Schema{ + "resource_id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: azure.ValidateResourceID, + }, + + "logs": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Computed: true, + }, + + "metrics": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Computed: true, + }, + }, + } +} + +func dataSourceArmMonitorDiagnosticCategoriesRead(d *schema.ResourceData, meta interface{}) error { + categoriesClient := meta.(*ArmClient).monitorDiagnosticSettingsCategoryClient + ctx := meta.(*ArmClient).StopContext + + actualResourceId := d.Get("resource_id").(string) + // trim off the leading `/` since the CheckExistenceByID / List methods don't expect it + resourceId := strings.TrimPrefix(actualResourceId, "/") + + // then retrieve the possible Diagnostics Categories for this Resource + categories, err := categoriesClient.List(ctx, resourceId) + if err != nil { + return fmt.Errorf("Error retrieving Diagnostics Categories for Resource %q: %+v", actualResourceId, err) + } + + if categories.Value == nil { + return fmt.Errorf("Error retrieving Diagnostics Categories for Resource %q: `categories.Value` was nil", actualResourceId) + } + + d.SetId(actualResourceId) + val := *categories.Value + + metrics := make([]string, 0) + logs := make([]string, 0) + + for _, v := range val { + if v.Name == nil { + continue + } + + if category := v.DiagnosticSettingsCategory; category != nil { + switch category.CategoryType { + case insights.Logs: + logs = append(logs, *v.Name) + case insights.Metrics: + metrics = append(metrics, *v.Name) + default: + return fmt.Errorf("Unsupported category type %q", string(category.CategoryType)) + } + } + } + + if err := d.Set("logs", logs); err != nil { + return fmt.Errorf("Error flattening `logs`: %+v", err) + } + + if err := d.Set("metrics", metrics); err != nil { + return fmt.Errorf("Error flattening `metrics`: %+v", err) + } + + return nil +} diff --git a/azurerm/data_source_monitor_diagnostic_categories_test.go b/azurerm/data_source_monitor_diagnostic_categories_test.go new file mode 100644 index 000000000000..9c89a6c1c3bc --- /dev/null +++ b/azurerm/data_source_monitor_diagnostic_categories_test.go @@ -0,0 +1,105 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceArmMonitorDiagnosticCategories_appService(t *testing.T) { + dataSourceName := "data.azurerm_monitor_diagnostic_categories.test" + ri := acctest.RandInt() + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceArmMonitorDiagnosticCategories_appService(ri, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "metrics.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "logs.#", "1"), + ), + }, + }, + }) +} + +func TestAccDataSourceArmMonitorDiagnosticCategories_storageAccount(t *testing.T) { + dataSourceName := "data.azurerm_monitor_diagnostic_categories.test" + rs := acctest.RandString(8) + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceArmMonitorDiagnosticCategories_storageAccount(rs, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "metrics.#", "2"), + resource.TestCheckResourceAttr(dataSourceName, "logs.#", "0"), + ), + }, + }, + }) +} + +func testAccDataSourceArmMonitorDiagnosticCategories_appService(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" +} + +resource "azurerm_app_service_plan" "test" { + name = "acctestASP-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + tier = "Standard" + size = "S1" + } +} + +resource "azurerm_app_service" "test" { + name = "acctestAS-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + app_service_plan_id = "${azurerm_app_service_plan.test.id}" +} + +data "azurerm_monitor_diagnostic_categories" "test" { + resource_id = "${azurerm_app_service.test.id}" +} +`, rInt, location, rInt, rInt) +} + +func testAccDataSourceArmMonitorDiagnosticCategories_storageAccount(rString, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%s" + location = "%s" +} + +resource "azurerm_storage_account" "test" { + name = "acctestsa%s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + account_tier = "Standard" + account_replication_type = "GRS" + + tags { + environment = "staging" + } +} + +data "azurerm_monitor_diagnostic_categories" "test" { + resource_id = "${azurerm_storage_account.test.id}" +} +`, rString, location, rString) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index b5222f6932d6..b2fdee7f9b9c 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -97,6 +97,7 @@ func Provider() terraform.ResourceProvider { "azurerm_logic_app_workflow": dataSourceArmLogicAppWorkflow(), "azurerm_managed_disk": dataSourceArmManagedDisk(), "azurerm_management_group": dataSourceArmManagementGroup(), + "azurerm_monitor_diagnostic_categories": dataSourceArmMonitorDiagnosticCategories(), "azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(), "azurerm_network_interface": dataSourceArmNetworkInterface(), "azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(), diff --git a/website/azurerm.erb b/website/azurerm.erb index 36da1ac9ef41..ae49a48f2f51 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -136,6 +136,10 @@ azurerm_management_group + > + azurerm_monitor_diagnostic_categories + + > azurerm_monitor_log_profile diff --git a/website/docs/d/monitor_diagnostic_categories.html.markdown b/website/docs/d/monitor_diagnostic_categories.html.markdown new file mode 100644 index 000000000000..65bccf28f761 --- /dev/null +++ b/website/docs/d/monitor_diagnostic_categories.html.markdown @@ -0,0 +1,37 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_monitor_diagnostic_categories" +sidebar_current: "docs-azurerm-datasource-monitor-diagnostic-categories" +description: |- + Gets information about an the Monitor Diagnostics Categories supported by an existing Resource. + +--- + +# Data Source: azurerm_monitor_diagnostic_categories + +Use this data source to access information about the Monitor Diagnostics Categories supported by an existing Resource. + +## Example Usage + +```hcl +data "azurerm_dns_zone" "test" { + name = "search-eventhubns" + resource_group_name = "search-service" +} + +output "dns_zone_id" { + value = "${data.azurerm_dns_zone.test.id}" +} +``` + +## Argument Reference + +* `resource_id` - (Required) The ID of an existing Resource which Monitor Diagnostics Categories should be retrieved for. + +## Attributes Reference + +* `id` - The ID of the Resource. + +* `logs` - A list of the Log Categories supported for this Resource. + +* `metrics` - A list of the Metric Categories supported for this Resource.