-
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.
Merge pull request #1291 from janboll/feature/diagnostics-monitoring
New Resource: `azurerm_monitor_diagnostic_setting` / New Data Source: `azurerm_monitor_diagnostic_categories`
- Loading branch information
Showing
9 changed files
with
1,180 additions
and
5 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
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 setting `logs`: %+v", err) | ||
} | ||
|
||
if err := d.Set("metrics", metrics); err != nil { | ||
return fmt.Errorf("Error setting `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
Oops, something went wrong.