Skip to content

Commit

Permalink
azurerm_monitor_diagnostic_setting - exactly one of category or `…
Browse files Browse the repository at this point in the history
…category_group` must be specified in `enabled_log` and `log` block (#23308)

* add category category_group validation for non-empty string

* add Exactly one of category or category_group must be specified

* fix lint: change if-else to switch
  • Loading branch information
teowa authored Sep 21, 2023
1 parent 1e3e2bb commit 766cb63
Showing 2 changed files with 55 additions and 23 deletions.
72 changes: 50 additions & 22 deletions internal/services/monitor/monitor_diagnostic_setting_resource.go
Original file line number Diff line number Diff line change
@@ -116,13 +116,15 @@ func resourceMonitorDiagnosticSetting() *pluginsdk.Resource {
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"category": {
Type: pluginsdk.TypeString,
Optional: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"category_group": {
Type: pluginsdk.TypeString,
Optional: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"retention_policy": {
@@ -157,8 +159,9 @@ func resourceMonitorDiagnosticSetting() *pluginsdk.Resource {
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"category": {
Type: pluginsdk.TypeString,
Required: true,
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"enabled": {
@@ -203,13 +206,15 @@ func resourceMonitorDiagnosticSetting() *pluginsdk.Resource {
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"category": {
Type: pluginsdk.TypeString,
Optional: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"category_group": {
Type: pluginsdk.TypeString,
Optional: true,
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"enabled": {
@@ -279,7 +284,11 @@ func resourceMonitorDiagnosticSettingCreate(d *pluginsdk.ResourceData, meta inte
if !features.FourPointOhBeta() {
logsRaw, ok := d.GetOk("log")
if ok && len(logsRaw.(*pluginsdk.Set).List()) > 0 {
logs = expandMonitorDiagnosticsSettingsLogs(logsRaw.(*pluginsdk.Set).List())
expendLogs, err := expandMonitorDiagnosticsSettingsLogs(logsRaw.(*pluginsdk.Set).List())
if err != nil {
return fmt.Errorf("expanding log: %+v", err)
}
logs = *expendLogs
for _, v := range logs {
if v.Enabled {
hasEnabledLogs = true
@@ -292,7 +301,11 @@ func resourceMonitorDiagnosticSettingCreate(d *pluginsdk.ResourceData, meta inte
if enabledLogs, ok := d.GetOk("enabled_log"); ok {
enabledLogsList := enabledLogs.(*pluginsdk.Set).List()
if len(enabledLogsList) > 0 {
logs = expandMonitorDiagnosticsSettingsEnabledLogs(enabledLogsList)
expandEnabledLogs, err := expandMonitorDiagnosticsSettingsEnabledLogs(enabledLogsList)
if err != nil {
return fmt.Errorf("expanding enabled_log: %+v", err)
}
logs = *expandEnabledLogs
hasEnabledLogs = true
}
}
@@ -383,7 +396,11 @@ func resourceMonitorDiagnosticSettingUpdate(d *pluginsdk.ResourceData, meta inte
if d.HasChange("log") {
logChanged = true
logsRaw := d.Get("log").(*pluginsdk.Set).List()
logs = expandMonitorDiagnosticsSettingsLogs(logsRaw)
expandLogs, err := expandMonitorDiagnosticsSettingsLogs(logsRaw)
if err != nil {
return fmt.Errorf("expanding log: %+v", err)
}
logs = *expandLogs
for _, v := range logs {
if v.Enabled {
hasEnabledLogs = true
@@ -396,7 +413,11 @@ func resourceMonitorDiagnosticSettingUpdate(d *pluginsdk.ResourceData, meta inte
if d.HasChange("enabled_log") {
enabledLogs := d.Get("enabled_log").(*pluginsdk.Set).List()
if len(enabledLogs) > 0 {
logs = expandMonitorDiagnosticsSettingsEnabledLogs(enabledLogs)
expandEnabledLogs, err := expandMonitorDiagnosticsSettingsEnabledLogs(enabledLogs)
if err != nil {
return fmt.Errorf("expanding enabled_log: %+v", err)
}
logs = *expandEnabledLogs
hasEnabledLogs = true
}
} else if !logChanged && existing.Model != nil && existing.Model.Properties != nil && existing.Model.Properties.Logs != nil {
@@ -607,7 +628,7 @@ func monitorDiagnosticSettingDeletedRefreshFunc(ctx context.Context, client *dia
}
}

func expandMonitorDiagnosticsSettingsLogs(input []interface{}) []diagnosticsettings.LogSettings {
func expandMonitorDiagnosticsSettingsLogs(input []interface{}) (*[]diagnosticsettings.LogSettings, error) {
results := make([]diagnosticsettings.LogSettings, 0)

for _, raw := range input {
@@ -632,19 +653,22 @@ func expandMonitorDiagnosticsSettingsLogs(input []interface{}) []diagnosticsetti
Enabled: enabled,
RetentionPolicy: retentionPolicy,
}
if category != "" {
switch {
case category != "":
output.Category = utils.String(category)
} else {
case categoryGroup != "":
output.CategoryGroup = utils.String(categoryGroup)
default:
return nil, fmt.Errorf("exactly one of `category` or `category_group` must be specified")
}

results = append(results, output)
}

return results
return &results, nil
}

func expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) []diagnosticsettings.LogSettings {
func expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) (*[]diagnosticsettings.LogSettings, error) {
results := make([]diagnosticsettings.LogSettings, 0)

for _, raw := range input {
@@ -668,16 +692,20 @@ func expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) []diagnost
Enabled: true,
RetentionPolicy: retentionPolicy,
}
if category != "" {

switch {
case category != "":
output.Category = utils.String(category)
} else {
case categoryGroup != "":
output.CategoryGroup = utils.String(categoryGroup)
default:
return nil, fmt.Errorf("exactly one of `category` or `category_group` must be specified")
}

results = append(results, output)
}

return results
return &results, nil
}

func flattenMonitorDiagnosticLogs(input *[]diagnosticsettings.LogSettings) []interface{} {
6 changes: 5 additions & 1 deletion website/docs/r/monitor_diagnostic_setting.html.markdown
Original file line number Diff line number Diff line change
@@ -122,6 +122,8 @@ A `log` block supports the following:

-> **NOTE:** Not all resources have category groups available.

-> **NOTE:** Exactly one of `category` or `category_group` must be specified.

* `retention_policy` - (Optional) A `retention_policy` block as defined below.

!> **NOTE:** `retention_policy` has been deprecated in favor of `azurerm_storage_management_policy` resource - to learn more information on the deprecation [in the Azure documentation](https://aka.ms/diagnostic_settings_log_retention).
@@ -138,7 +140,9 @@ An `enabled_log` block supports the following:

* `category_group` - (Optional) The name of a Diagnostic Log Category Group for this Resource.

-> **NOTE:** Not all resources have category groups available.****
-> **NOTE:** Not all resources have category groups available.

-> **NOTE:** Exactly one of `category` or `category_group` must be specified.

* `retention_policy` - (Optional) A `retention_policy` block as defined below.

0 comments on commit 766cb63

Please sign in to comment.