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 2 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
65 changes: 45 additions & 20 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 expandMonitorDiagnosticsSettingsLogs(input []interface{}) []diagnosticsettings.LogSettings {
func expandMonitorDiagnosticsSettingsLogs(input []interface{}) (*[]diagnosticsettings.LogSettings, error) {
results := make([]diagnosticsettings.LogSettings, 0)

for _, raw := range input {
Expand All @@ -632,17 +653,19 @@
}
if category != "" {
output.Category = utils.String(category)
} else {
} else if categoryGroup != "" {

Check failure on line 656 in internal/services/monitor/monitor_diagnostic_setting_resource.go

View workflow job for this annotation

GitHub Actions / golint

ifElseChain: rewrite if-else to switch statement (gocritic)
output.CategoryGroup = utils.String(categoryGroup)
} else {
return nil, fmt.Errorf("exactly one of `category` or `category_group` must be specified")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the feature ExactlyOneOf so this validation can go in the schema and happen earlier in the lifecycle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have more than one log block, so we cannot use ExactlyOneOf.

}

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 @@ -668,14 +691,16 @@
}
if category != "" {
output.Category = utils.String(category)
} else {
} else if categoryGroup != "" {

Check failure on line 694 in internal/services/monitor/monitor_diagnostic_setting_resource.go

View workflow job for this annotation

GitHub Actions / golint

ifElseChain: rewrite if-else to switch statement (gocritic)
output.CategoryGroup = utils.String(categoryGroup)
} else {
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