From 6298ba43897565794d1afc791aa6ff28e66b0141 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Fri, 15 Sep 2023 17:49:21 +0800 Subject: [PATCH 1/3] add category category_group validation for non-empty string --- .../monitor_diagnostic_setting_resource.go | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/internal/services/monitor/monitor_diagnostic_setting_resource.go b/internal/services/monitor/monitor_diagnostic_setting_resource.go index 5be8bae6c9d8..fe8b2a8381a5 100644 --- a/internal/services/monitor/monitor_diagnostic_setting_resource.go +++ b/internal/services/monitor/monitor_diagnostic_setting_resource.go @@ -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": { @@ -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": { From be84c40dba8ceb68ecf67bf28ca1a7b8de6804a0 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:29:11 +0800 Subject: [PATCH 2/3] add Exactly one of category or category_group must be specified --- .../monitor_diagnostic_setting_resource.go | 40 ++++++++++++++----- .../monitor_diagnostic_setting.html.markdown | 6 ++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/internal/services/monitor/monitor_diagnostic_setting_resource.go b/internal/services/monitor/monitor_diagnostic_setting_resource.go index fe8b2a8381a5..45c9a4baea6b 100644 --- a/internal/services/monitor/monitor_diagnostic_setting_resource.go +++ b/internal/services/monitor/monitor_diagnostic_setting_resource.go @@ -282,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 @@ -295,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 } } @@ -386,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 @@ -399,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 { @@ -610,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 { @@ -637,17 +653,19 @@ func expandMonitorDiagnosticsSettingsLogs(input []interface{}) []diagnosticsetti } if category != "" { output.Category = utils.String(category) - } else { + } else if categoryGroup != "" { 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 expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) []diagnosticsettings.LogSettings { +func expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) (*[]diagnosticsettings.LogSettings, error) { results := make([]diagnosticsettings.LogSettings, 0) for _, raw := range input { @@ -673,14 +691,16 @@ func expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) []diagnost } if category != "" { output.Category = utils.String(category) - } else { + } else if categoryGroup != "" { 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{} { diff --git a/website/docs/r/monitor_diagnostic_setting.html.markdown b/website/docs/r/monitor_diagnostic_setting.html.markdown index 493de7b692a6..fda95d42f76a 100644 --- a/website/docs/r/monitor_diagnostic_setting.html.markdown +++ b/website/docs/r/monitor_diagnostic_setting.html.markdown @@ -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`. @@ -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. From 9f39ed1097681180b420bd0197324eb86ddbfc0f Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:56:44 +0000 Subject: [PATCH 3/3] fix lint: change if-else to switch --- .../monitor_diagnostic_setting_resource.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/services/monitor/monitor_diagnostic_setting_resource.go b/internal/services/monitor/monitor_diagnostic_setting_resource.go index 45c9a4baea6b..c5f60808ec60 100644 --- a/internal/services/monitor/monitor_diagnostic_setting_resource.go +++ b/internal/services/monitor/monitor_diagnostic_setting_resource.go @@ -651,11 +651,12 @@ func expandMonitorDiagnosticsSettingsLogs(input []interface{}) (*[]diagnosticset Enabled: enabled, RetentionPolicy: retentionPolicy, } - if category != "" { + switch { + case category != "": output.Category = utils.String(category) - } else if categoryGroup != "" { + case categoryGroup != "": output.CategoryGroup = utils.String(categoryGroup) - } else { + default: return nil, fmt.Errorf("exactly one of `category` or `category_group` must be specified") } @@ -689,11 +690,13 @@ func expandMonitorDiagnosticsSettingsEnabledLogs(input []interface{}) (*[]diagno Enabled: true, RetentionPolicy: retentionPolicy, } - if category != "" { + + switch { + case category != "": output.Category = utils.String(category) - } else if categoryGroup != "" { + case categoryGroup != "": output.CategoryGroup = utils.String(categoryGroup) - } else { + default: return nil, fmt.Errorf("exactly one of `category` or `category_group` must be specified") }