Skip to content

Commit

Permalink
Switching to a custom ID format, to work around differences in casing…
Browse files Browse the repository at this point in the history
… etc

Adding some TODO's around the Schema
Ensuring fields are always set
  • Loading branch information
tombuildsstuff committed Jul 24, 2018
1 parent a433d5e commit 0b9a331
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 104 deletions.
24 changes: 0 additions & 24 deletions azurerm/monitor_diagnostics.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
package azurerm

import "strings"

type MonitorDiagnosticId struct {
ResourceID string
Name string
}

func parseMonitorDiagnosticId(monitorId string) MonitorDiagnosticId {
var returnId MonitorDiagnosticId
returnId.ResourceID = retrieveResourceId(monitorId)
returnId.Name = retrieveDiagnosticName(monitorId)
return returnId
}

func retrieveResourceId(diagnosticSettingId string) string {
substring := "/providers/microsoft.insights/diagnosticSettings/"
return diagnosticSettingId[0:strings.Index(diagnosticSettingId, substring)]
}

func retrieveDiagnosticName(diagnosticSettingId string) string {
substring := "/providers/microsoft.insights/diagnosticSettings/"
return diagnosticSettingId[len(substring)+strings.Index(diagnosticSettingId, substring):]
}

func getAllDiagnosticSettings(targetResourceId string, meta interface{}) (*[]interface{}, *[]interface{}, error) {
client := meta.(*ArmClient).monitorDiagnosticSettingsCategoryClient
ctx := meta.(*ArmClient).StopContext
Expand Down
166 changes: 92 additions & 74 deletions azurerm/resource_arm_monitor_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package azurerm
import (
"fmt"
"log"
"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/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

type monitorDiagnosticId struct {
resourceID string
name string
}

func resourceArmMonitorDiagnostics() *schema.Resource {
return &schema.Resource{
Create: resourceArmMonitorDiagnosticsCreateUpdate,
Expand Down Expand Up @@ -84,6 +91,7 @@ func resourceArmMonitorDiagnosticsCreateUpdate(d *schema.ResourceData, meta inte
disabledSettings := d.Get("disabled_settings").([]interface{})
retentionDays := d.Get("retention_days").(int)

// TODO: I think this wants to be a Data Source?
allMetricSettings, allLogSettings, err := getAllDiagnosticSettings(targetResourceId, meta)
if err != nil {
return err
Expand All @@ -94,10 +102,12 @@ func resourceArmMonitorDiagnosticsCreateUpdate(d *schema.ResourceData, meta inte
return fmt.Errorf("Invalid value for disabled settings provided, use one or multiple of: %q", allSettings)
}

// TODO: remove this
if len(allSettings) == len(disabledSettings) {
return fmt.Errorf("You can not disable all settings, rather delete diagnostic logging")
}

// TODO: fix the schema
diagnosticSettings := &insights.DiagnosticSettings{}
diagnosticSettings.Metrics = expandMetricsConfiguration(*allMetricSettings, disabledSettings, retentionDays)
diagnosticSettings.Logs = expandLogConfiguration(*allLogSettings, disabledSettings, retentionDays)
Expand All @@ -115,13 +125,11 @@ func resourceArmMonitorDiagnosticsCreateUpdate(d *schema.ResourceData, meta inte
diagnosticSettings.EventHubName = &eventHubName
}

_, err = client.CreateOrUpdate(
ctx,
targetResourceId,
insights.DiagnosticSettingsResource{
DiagnosticSettings: diagnosticSettings,
},
name)
resource := insights.DiagnosticSettingsResource{
DiagnosticSettings: diagnosticSettings,
}

_, err = client.CreateOrUpdate(ctx, targetResourceId, resource, name)
if err != nil {
return fmt.Errorf("Error creating Diagnostics Setting %q (Resource ID %q): %+v", name, targetResourceId, err)
}
Expand All @@ -131,10 +139,10 @@ func resourceArmMonitorDiagnosticsCreateUpdate(d *schema.ResourceData, meta inte
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read ID for Monitor Diagnostics %q", name)
return fmt.Errorf("Cannot read ID for Monitor Diagnostics %q for Resource ID %q", name, targetResourceId)
}

d.SetId(*read.ID)
d.SetId(fmt.Sprintf("%s|%s", targetResourceId, name))

return resourceArmMonitorDiagnosticsRead(d, meta)
}
Expand All @@ -143,92 +151,52 @@ func resourceArmMonitorDiagnosticsRead(d *schema.ResourceData, meta interface{})
client := meta.(*ArmClient).monitorDiagnosticSettingsClient
ctx := meta.(*ArmClient).StopContext

monitoringId := parseMonitorDiagnosticId(d.Id())
id, err := parseMonitorDiagnosticId(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, monitoringId.ResourceID, monitoringId.Name)
resp, err := client.Get(ctx, id.resourceID, id.name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] Monitor Diagnostics Setting %q was not found for Resource ID %q - removing from state!", id.name, id.resourceID)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Diagnostic Setting %s: %+v", monitoringId.Name, err)
}

d.Set("name", *resp.Name)

// ID of base resource is not returned by API, so we have to guess here
monitoringId = parseMonitorDiagnosticId(d.Id())
d.Set("target_resource_id", monitoringId.ResourceID)

if resp.StorageAccountID != nil {
d.Set("storage_account_id", *resp.StorageAccountID)
}

if resp.EventHubName != nil {
d.Set("event_hub_name", *resp.EventHubName)
}

if resp.EventHubAuthorizationRuleID != nil {
d.Set("event_hub_authorization_rule_id", *resp.EventHubAuthorizationRuleID)
return fmt.Errorf("Error retrieving Monitor Diagnostics Setting %q for Resource ID %q: %+v", id.name, id.resourceID, err)
}

if resp.WorkspaceID != nil {
d.Set("workspace_id", *resp.WorkspaceID)
}
d.Set("name", id.name)
d.Set("target_resource_id", id.resourceID)
d.Set("storage_account_id", resp.StorageAccountID)
d.Set("event_hub_name", resp.EventHubName)
d.Set("event_hub_authorization_rule_id", resp.EventHubAuthorizationRuleID)
d.Set("workspace_id", resp.WorkspaceID)

// TODO: handle crashes/set errors here
d.Set("disabled_settings", flattenDisabledSettings(*resp.Metrics, *resp.Logs))
d.Set("retention_days", flattenRetentionDays(*resp.Metrics, *resp.Logs))

return nil
}

func flattenDisabledSettings(metricSettings []insights.MetricSettings, logSettings []insights.LogSettings) []interface{} {
disabledSettings := make([]interface{}, 0)

for _, setting := range metricSettings {
category := *setting.Category
if !*setting.Enabled {
disabledSettings = append(disabledSettings, category)
}
}

for _, setting := range logSettings {
category := *setting.Category
if !*setting.Enabled {
disabledSettings = append(disabledSettings, category)
}
}
return disabledSettings
}

func flattenRetentionDays(metricSettings []insights.MetricSettings, logSettings []insights.LogSettings) int32 {
returnSetting := int32(0)

for _, setting := range metricSettings {
if *setting.Enabled {
returnSetting = *setting.RetentionPolicy.Days
}
}

for _, setting := range logSettings {
if *setting.Enabled {
returnSetting = *setting.RetentionPolicy.Days
}
}

return returnSetting
}

func resourceArmMonitorDiagnosticsDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).monitorDiagnosticSettingsClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
targetResoureId := d.Get("target_resource_id").(string)
id, err := parseMonitorDiagnosticId(d.Id())
if err != nil {
return err
}

_, err := client.Delete(ctx, targetResoureId, name)
resp, err := client.Delete(ctx, id.resourceID, id.name)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("Error deleting Monitor Diagnostics Setting %q for Resource Id %q: %+v", id.name, id.resourceID, err)
}
}

return err
return nil
}

func expandMetricsConfiguration(allMetricSettings, disabledSettings []interface{}, retentionDays int) *[]insights.MetricSettings {
Expand Down Expand Up @@ -282,3 +250,53 @@ func expandLogConfiguration(allLogSettings, disabledSettings []interface{}, rete
}
return &returnLogSettings
}

func flattenDisabledSettings(metricSettings []insights.MetricSettings, logSettings []insights.LogSettings) []interface{} {
disabledSettings := make([]interface{}, 0)

for _, setting := range metricSettings {
category := *setting.Category
if !*setting.Enabled {
disabledSettings = append(disabledSettings, category)
}
}

for _, setting := range logSettings {
category := *setting.Category
if !*setting.Enabled {
disabledSettings = append(disabledSettings, category)
}
}
return disabledSettings
}

func flattenRetentionDays(metricSettings []insights.MetricSettings, logSettings []insights.LogSettings) int32 {
returnSetting := int32(0)

for _, setting := range metricSettings {
if *setting.Enabled {
returnSetting = *setting.RetentionPolicy.Days
}
}

for _, setting := range logSettings {
if *setting.Enabled {
returnSetting = *setting.RetentionPolicy.Days
}
}

return returnSetting
}

func parseMonitorDiagnosticId(monitorId string) (*monitorDiagnosticId, error) {
v := strings.Split(monitorId, "|")
if len(v) != 2 {
return nil, fmt.Errorf("Expected the Monitor Diagnostics ID to be in the format `{resourceId}|{name}` but got %d segments", len(v))
}

identifier := monitorDiagnosticId{
resourceID: v[0],
name: v[1],
}
return &identifier, nil
}
12 changes: 6 additions & 6 deletions azurerm/resource_arm_monitor_diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func testCheckAzureRMMonitorDiagnosticsExists(name, objectName string) resource.
}
}

func testAccAzureRMMonitorDiagnostics_basic(randomInt int, location string) string {
func testAccAzureRMMonitorDiagnostics_basic(rInt int, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down Expand Up @@ -179,10 +179,10 @@ func testAccAzureRMMonitorDiagnostics_basic(randomInt int, location string) stri
target_resource_id = "${azurerm_key_vault.test_vault.id}"
storage_account_id = "${azurerm_storage_account.test_storage_logs.id}"
disabled_settings = ["AuditEvent"]
}`, randomInt, location, randomInt, randomInt, randomInt)
}`, rInt, location, rInt, rInt, rInt)
}

func testAccAzureRMMonitorDiagnostics_complete(randomInt int, location string) string {
func testAccAzureRMMonitorDiagnostics_complete(rInt int, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down Expand Up @@ -224,10 +224,10 @@ func testAccAzureRMMonitorDiagnostics_complete(randomInt int, location string) s
storage_account_id = "${azurerm_storage_account.test_storage_logs.id}"
workspace_id = "${azurerm_log_analytics_workspace.analytics.id}"
retention_days = 90
}`, randomInt, location, randomInt, randomInt, randomInt, randomInt)
}`, rInt, location, rInt, rInt, rInt, rInt)
}

func testAccAzureRMMonitorDiagnostics_update(randomInt int, location string) string {
func testAccAzureRMMonitorDiagnostics_update(rInt int, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down Expand Up @@ -260,5 +260,5 @@ func testAccAzureRMMonitorDiagnostics_update(randomInt int, location string) str
target_resource_id = "${azurerm_key_vault.test_vault.id}"
storage_account_id = "${azurerm_storage_account.test_storage_logs.id}"
retention_days = 30
}`, randomInt, location, randomInt, randomInt, randomInt)
}`, rInt, location, rInt, rInt, rInt)
}

0 comments on commit 0b9a331

Please sign in to comment.