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

Linting fixes #81

Merged
merged 2 commits into from
Aug 23, 2024
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
21 changes: 16 additions & 5 deletions internal/provider/dashboard_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/google/go-cmp/cmp"
Expand All @@ -19,8 +20,19 @@ var (
_ resource.Resource = &dashboardResource{}
_ resource.ResourceWithConfigure = &dashboardResource{}
_ resource.ResourceWithImportState = &dashboardResource{}

errLayoutMissing = errors.New("layout missing for widget id")
errWidgetProperties = errors.New("widget properties error")
)

func newLayoutError(id string) error {
return fmt.Errorf("%w: %s", errLayoutMissing, id)
}

func newWidgetPropertiesError(msg string, id string) error {
return fmt.Errorf("%w: %s id:%s", errWidgetProperties, msg, id)
}

func NewDashboardResource() resource.Resource {
return &dashboardResource{}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -120,16 +132,15 @@ 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.
layout := &dashboard.Layout[lIdx]
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 {
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion internal/provider/notificationSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"log"
)

var (
errUnsupportedNotificationType = errors.New("unsupported notification type")
)

func newUnsupportedNotificationTypeError(notificationType string) error {
return fmt.Errorf("%w: %s", errUnsupportedNotificationType, notificationType)
}

type notificationSettings struct {
Email *notificationSettingsEmail `tfsdk:"email"`
Slack *notificationSettingsSlack `tfsdk:"slack"`
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion internal/provider/notification_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"errors"
"fmt"
"regexp"
"strings"
Expand All @@ -25,6 +26,14 @@ const (
httpSchemeRegex = `^(http|https)`
)

var (
errParse = errors.New("parser error")
)

func newParseError(msg string) error {
return fmt.Errorf("%w: %s", errParse, msg)
}

// The main Notification Resource model that is derived from the schema.
type notificationResourceModel struct {
Id types.String `tfsdk:"id"`
Expand All @@ -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]
Expand Down
Loading