From 7c5d9a6587daed92e4adfa22a330809281023ee0 Mon Sep 17 00:00:00 2001 From: Greg Poirier Date: Fri, 23 Aug 2024 08:02:38 -0400 Subject: [PATCH 1/2] Linting fixes --- internal/provider/dashboard_resource.go | 21 ++++++++++++++++----- internal/provider/notificationSettings.go | 11 ++++++++++- internal/provider/notification_schema.go | 11 ++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/internal/provider/dashboard_resource.go b/internal/provider/dashboard_resource.go index 2e532ea..2872dd0 100644 --- a/internal/provider/dashboard_resource.go +++ b/internal/provider/dashboard_resource.go @@ -3,6 +3,7 @@ package provider import ( "context" "encoding/json" + "errors" "fmt" "github.com/google/go-cmp/cmp" @@ -19,8 +20,19 @@ var ( _ resource.Resource = &dashboardResource{} _ resource.ResourceWithConfigure = &dashboardResource{} _ resource.ResourceWithImportState = &dashboardResource{} + + layoutError = errors.New("layout missing for widget id") + widgetPropertiesError = errors.New("widget properties error") ) +func newLayoutError(id string) error { + return fmt.Errorf("%w: %s", layoutError, id) +} + +func newWidgetPropertiesError(msg string, id string) error { + return fmt.Errorf("%w: %s id:%s", widgetPropertiesError, msg, id) +} + func NewDashboardResource() resource.Resource { return &dashboardResource{} } @@ -80,7 +92,7 @@ func setDashboardValuesFromCreate(dashboard *swoClient.CreateDashboardResult, pl for _, w := range dashboard.Widgets { lIdx := slices.IndexFunc(dashboard.Layout, func(l swoClient.CreateDashboardLayout) bool { return l.Id == w.Id }) if lIdx <= -1 { - return fmt.Errorf("layout missing for widget id: %s", w.Id) + return newLayoutError(w.Id) } // The layout that will give us the widget coordinates for comparison to the plan. @@ -120,7 +132,7 @@ func setDashboardValuesFromRead(dashboard *swoClient.ReadDashboardResult, state for _, w := range dashboard.Widgets { lIdx := slices.IndexFunc(dashboard.Layout, func(l swoClient.ReadDashboardLayout) bool { return l.Id == w.Id }) if lIdx <= -1 { - return fmt.Errorf("layout missing for widget id: %s", w.Id) + return newLayoutError(w.Id) } // We found the layout that will give us the widget coordinates for comparison to the plan. @@ -128,8 +140,7 @@ func setDashboardValuesFromRead(dashboard *swoClient.ReadDashboardResult, state isInState := false props, err := json.Marshal(w.Properties) if err != nil { - return fmt.Errorf("widget properties error: %s, id: %s", - err, w.Id) + return newWidgetPropertiesError(err.Error(), w.Id) } for wIdx := range state.Widgets { @@ -146,7 +157,7 @@ func setDashboardValuesFromRead(dashboard *swoClient.ReadDashboardResult, state var stateProps any err = json.Unmarshal([]byte(stateW.Properties.ValueString()), &stateProps) if err != nil { - return fmt.Errorf("widget properties error: %s, id: %s", err, w.Id) + return newWidgetPropertiesError(err.Error(), w.Id) } // The json string can be marshalled differently than what is specified in the terraform diff --git a/internal/provider/notificationSettings.go b/internal/provider/notificationSettings.go index 3e35e9f..0cebcba 100644 --- a/internal/provider/notificationSettings.go +++ b/internal/provider/notificationSettings.go @@ -2,10 +2,19 @@ package provider import ( "encoding/json" + "errors" "fmt" "log" ) +var ( + unsupportedNotificationTypeError = errors.New("unsupported notification type") +) + +func newUnsupportedNotificationTypeError(notificationType string) error { + return fmt.Errorf("%w: %s", unsupportedNotificationTypeError, notificationType) +} + type notificationSettings struct { Email *notificationSettingsEmail `tfsdk:"email"` Slack *notificationSettingsSlack `tfsdk:"slack"` @@ -261,7 +270,7 @@ func (m *notificationResourceModel) SetSettings(settings any) error { return err } } else { - return fmt.Errorf("unsupported notification type: %s", m.Type.ValueString()) + return newUnsupportedNotificationTypeError(m.Type.ValueString()) } return nil diff --git a/internal/provider/notification_schema.go b/internal/provider/notification_schema.go index 13b122c..4c414d4 100644 --- a/internal/provider/notification_schema.go +++ b/internal/provider/notification_schema.go @@ -2,6 +2,7 @@ package provider import ( "context" + "errors" "fmt" "regexp" "strings" @@ -25,6 +26,14 @@ const ( httpSchemeRegex = `^(http|https)` ) +var ( + parseError = errors.New("parser error") +) + +func newParseError(msg string) error { + return fmt.Errorf("%w: %s", parseError, msg) +} + // The main Notification Resource model that is derived from the schema. type notificationResourceModel struct { Id types.String `tfsdk:"id"` @@ -38,7 +47,7 @@ func (m *notificationResourceModel) ParseId() (id string, notificationType strin idParts := strings.Split(m.Id.ValueString(), ":") if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { - err = fmt.Errorf("expected identifier with format: id:type. got: %q", m.Id) + err = newParseError(fmt.Sprintf("expected identifier with format id:type. got: %q", m.Id)) } else { id = idParts[0] notificationType = idParts[1] From 2f3e2b09242b55c81327d9baf3f015c3639909e4 Mon Sep 17 00:00:00 2001 From: Greg Poirier Date: Fri, 23 Aug 2024 08:06:52 -0400 Subject: [PATCH 2/2] Satisfy staticcheck --- internal/provider/dashboard_resource.go | 8 ++++---- internal/provider/notificationSettings.go | 4 ++-- internal/provider/notification_schema.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/provider/dashboard_resource.go b/internal/provider/dashboard_resource.go index 2872dd0..7520b58 100644 --- a/internal/provider/dashboard_resource.go +++ b/internal/provider/dashboard_resource.go @@ -21,16 +21,16 @@ var ( _ resource.ResourceWithConfigure = &dashboardResource{} _ resource.ResourceWithImportState = &dashboardResource{} - layoutError = errors.New("layout missing for widget id") - widgetPropertiesError = errors.New("widget properties error") + errLayoutMissing = errors.New("layout missing for widget id") + errWidgetProperties = errors.New("widget properties error") ) func newLayoutError(id string) error { - return fmt.Errorf("%w: %s", layoutError, id) + return fmt.Errorf("%w: %s", errLayoutMissing, id) } func newWidgetPropertiesError(msg string, id string) error { - return fmt.Errorf("%w: %s id:%s", widgetPropertiesError, msg, id) + return fmt.Errorf("%w: %s id:%s", errWidgetProperties, msg, id) } func NewDashboardResource() resource.Resource { diff --git a/internal/provider/notificationSettings.go b/internal/provider/notificationSettings.go index 0cebcba..d4555b4 100644 --- a/internal/provider/notificationSettings.go +++ b/internal/provider/notificationSettings.go @@ -8,11 +8,11 @@ import ( ) var ( - unsupportedNotificationTypeError = errors.New("unsupported notification type") + errUnsupportedNotificationType = errors.New("unsupported notification type") ) func newUnsupportedNotificationTypeError(notificationType string) error { - return fmt.Errorf("%w: %s", unsupportedNotificationTypeError, notificationType) + return fmt.Errorf("%w: %s", errUnsupportedNotificationType, notificationType) } type notificationSettings struct { diff --git a/internal/provider/notification_schema.go b/internal/provider/notification_schema.go index 4c414d4..a69ec96 100644 --- a/internal/provider/notification_schema.go +++ b/internal/provider/notification_schema.go @@ -27,11 +27,11 @@ const ( ) var ( - parseError = errors.New("parser error") + errParse = errors.New("parser error") ) func newParseError(msg string) error { - return fmt.Errorf("%w: %s", parseError, msg) + return fmt.Errorf("%w: %s", errParse, msg) } // The main Notification Resource model that is derived from the schema.