Skip to content

Commit

Permalink
New Data Source: azurerm_monitor_diagnostic_categories
Browse files Browse the repository at this point in the history
Tests pass:

```
$ acctests azurerm TestAccDataSourceArmMonitorDiagnosticCategories_
=== RUN   TestAccDataSourceArmMonitorDiagnosticCategories_appService
--- PASS: TestAccDataSourceArmMonitorDiagnosticCategories_appService (150.71s)
=== RUN   TestAccDataSourceArmMonitorDiagnosticCategories_storageAccount
--- PASS: TestAccDataSourceArmMonitorDiagnosticCategories_storageAccount (108.97s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	261.573s
```
  • Loading branch information
tombuildsstuff committed Nov 13, 2018
1 parent 06ae887 commit 83f9ff4
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
89 changes: 89 additions & 0 deletions azurerm/data_source_monitor_diagnostic_categories.go
Original file line number Diff line number Diff line change
@@ -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
}
105 changes: 105 additions & 0 deletions azurerm/data_source_monitor_diagnostic_categories_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
<a href="/docs/providers/azurerm/d/management_group.html">azurerm_management_group</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-monitor-diagnostic-categories") %>>
<a href="/docs/providers/azurerm/d/monitor_diagnostic_categories.html">azurerm_monitor_diagnostic_categories</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-monitor-log-profile") %>>
<a href="/docs/providers/azurerm/d/monitor_log_profile.html">azurerm_monitor_log_profile</a>
</li>
Expand Down
37 changes: 37 additions & 0 deletions website/docs/d/monitor_diagnostic_categories.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 83f9ff4

Please sign in to comment.