Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_monitor_diagnostic_setting - exactly one of category or category_group must be specified in enabled_log and log block #23308

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 50 additions & 22 deletions internal/services/monitor/monitor_diagnostic_setting_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -202,13 +205,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": {
Expand Down Expand Up @@ -277,7 +282,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
Expand All @@ -290,7 +299,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
}
}
Expand Down Expand Up @@ -381,7 +394,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
Expand All @@ -394,7 +411,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 {
Expand Down Expand Up @@ -605,7 +626,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 {
Expand All @@ -630,19 +651,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 {
Expand All @@ -666,16 +690,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{} {
Expand Down
6 changes: 5 additions & 1 deletion website/docs/r/monitor_diagnostic_setting.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

* `enabled` - (Optional) Is this Diagnostic Log enabled? Defaults to `true`.
Expand All @@ -136,7 +138,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.

Expand Down
Loading