-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source:
azurerm_monitor_diagnostic_categories
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
1 parent
06ae887
commit 83f9ff4
Showing
5 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
azurerm/data_source_monitor_diagnostic_categories_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
website/docs/d/monitor_diagnostic_categories.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |