Skip to content

Commit

Permalink
Merge pull request #1291 from janboll/feature/diagnostics-monitoring
Browse files Browse the repository at this point in the history
New Resource: `azurerm_monitor_diagnostic_setting` / New Data Source: `azurerm_monitor_diagnostic_categories`
  • Loading branch information
katbyte authored Nov 15, 2018
2 parents f511ba2 + ea431b1 commit 2154108
Show file tree
Hide file tree
Showing 9 changed files with 1,180 additions and 5 deletions.
20 changes: 15 additions & 5 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ type ArmClient struct {
managementGroupsSubscriptionClient managementgroups.SubscriptionsClient

// Monitor
monitorActionGroupsClient insights.ActionGroupsClient
monitorActivityLogAlertsClient insights.ActivityLogAlertsClient
monitorAlertRulesClient insights.AlertRulesClient
monitorLogProfilesClient insights.LogProfilesClient
monitorMetricAlertsClient insights.MetricAlertsClient
monitorActionGroupsClient insights.ActionGroupsClient
monitorActivityLogAlertsClient insights.ActivityLogAlertsClient
monitorAlertRulesClient insights.AlertRulesClient
monitorDiagnosticSettingsClient insights.DiagnosticSettingsClient
monitorDiagnosticSettingsCategoryClient insights.DiagnosticSettingsCategoryClient
monitorLogProfilesClient insights.LogProfilesClient
monitorMetricAlertsClient insights.MetricAlertsClient

// MSI
userAssignedIdentitiesClient msi.UserAssignedIdentitiesClient
Expand Down Expand Up @@ -832,6 +834,14 @@ func (c *ArmClient) registerMonitorClients(endpoint, subscriptionId string, auth
autoscaleSettingsClient := insights.NewAutoscaleSettingsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&autoscaleSettingsClient.Client, auth)
c.autoscaleSettingsClient = autoscaleSettingsClient

monitoringInsightsClient := insights.NewDiagnosticSettingsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&monitoringInsightsClient.Client, auth)
c.monitorDiagnosticSettingsClient = monitoringInsightsClient

monitoringCategorySettingsClient := insights.NewDiagnosticSettingsCategoryClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&monitoringCategorySettingsClient.Client, auth)
c.monitorDiagnosticSettingsCategoryClient = monitoringCategorySettingsClient
}

func (c *ArmClient) registerNetworkingClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
Expand Down
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 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 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)
}
2 changes: 2 additions & 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 Expand Up @@ -218,6 +219,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_metric_alertrule": resourceArmMetricAlertRule(),
"azurerm_monitor_action_group": resourceArmMonitorActionGroup(),
"azurerm_monitor_activity_log_alert": resourceArmMonitorActivityLogAlert(),
"azurerm_monitor_diagnostic_setting": resourceArmMonitorDiagnosticSetting(),
"azurerm_monitor_log_profile": resourceArmMonitorLogProfile(),
"azurerm_monitor_metric_alert": resourceArmMonitorMetricAlert(),
"azurerm_mysql_configuration": resourceArmMySQLConfiguration(),
Expand Down
Loading

0 comments on commit 2154108

Please sign in to comment.