From e4beb78e42fc397cc84b0c13b412e5a189783585 Mon Sep 17 00:00:00 2001 From: Michael van Tellingen Date: Mon, 13 May 2024 13:49:16 +0200 Subject: [PATCH] refactor: move utils to `internal.utils` prepare for re-organizing the code-base a bit --- internal/asset_folder_model.go | 4 +++- internal/asset_folder_resource.go | 14 ++++++++------ internal/component_group_model.go | 4 +++- internal/component_group_model_test.go | 4 +++- internal/component_group_resource.go | 16 +++++++++------- internal/component_model.go | 4 +++- internal/component_resource.go | 9 +++++---- internal/space_role_model.go | 5 ++++- internal/space_role_resource.go | 22 ++++++++++++---------- internal/utils.go | 18 ------------------ internal/utils/client.go | 11 +++++++++++ internal/{ => utils}/errors.go | 10 +++++----- internal/utils/identifier.go | 15 +++++++++++++++ 13 files changed, 81 insertions(+), 55 deletions(-) create mode 100644 internal/utils/client.go rename internal/{ => utils}/errors.go (90%) create mode 100644 internal/utils/identifier.go diff --git a/internal/asset_folder_model.go b/internal/asset_folder_model.go index bdb54f4..57c288e 100644 --- a/internal/asset_folder_model.go +++ b/internal/asset_folder_model.go @@ -5,6 +5,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // assetFolderResourceModel maps the resource schema data. @@ -37,7 +39,7 @@ func (m *assetFolderResourceModel) fromRemote(spaceID int64, f *sbmgmt.AssetFold if f == nil { return fmt.Errorf("asset folder is nil") } - m.ID = types.StringValue(createIdentifier(spaceID, f.Id)) + m.ID = types.StringValue(utils.CreateIdentifier(spaceID, f.Id)) m.AssetFolderID = types.Int64Value(f.Id) m.SpaceID = types.Int64Value(spaceID) m.Name = types.StringValue(f.Name) diff --git a/internal/asset_folder_resource.go b/internal/asset_folder_resource.go index cc3b25f..7fd749e 100644 --- a/internal/asset_folder_resource.go +++ b/internal/asset_folder_resource.go @@ -3,17 +3,19 @@ package internal import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "net/http" "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // Ensure the implementation satisfies the expected interfaces. @@ -81,7 +83,7 @@ func (r *assetFolderResource) Configure(_ context.Context, req resource.Configur return } - r.client = getClient(req.ProviderData) + r.client = utils.GetClient(req.ProviderData) } // Create creates the resource and sets the initial Terraform state. @@ -146,10 +148,10 @@ func (r *assetFolderResource) Read(ctx context.Context, req resource.ReadRequest return } - spaceId, id := parseIdentifier(state.ID.ValueString()) + spaceId, id := utils.ParseIdentifier(state.ID.ValueString()) content, err := r.client.GetAssetFolderWithResponse(ctx, spaceId, id) - if d := checkGetError("assetFolder", id, content, err); d != nil { + if d := utils.CheckGetError("assetFolder", id, content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -206,7 +208,7 @@ func (r *assetFolderResource) Update(ctx context.Context, req resource.UpdateReq } afResp, err := r.client.GetAssetFolderWithResponse(ctx, spaceID, plan.AssetFolderID.ValueInt64()) - if d := checkGetError("assetFolder", plan.AssetFolderID.ValueInt64(), afResp, err); d != nil { + if d := utils.CheckGetError("assetFolder", plan.AssetFolderID.ValueInt64(), afResp, err); d != nil { resp.Diagnostics.Append(d) return } @@ -239,7 +241,7 @@ func (r *assetFolderResource) Delete(ctx context.Context, req resource.DeleteReq return } - spaceId, assetFolderId := parseIdentifier(state.ID.ValueString()) + spaceId, assetFolderId := utils.ParseIdentifier(state.ID.ValueString()) content, err := r.client.DeleteAssetFolderWithResponse(ctx, spaceId, assetFolderId) if err != nil { resp.Diagnostics.AddError( diff --git a/internal/component_group_model.go b/internal/component_group_model.go index 1e014b1..ade8060 100644 --- a/internal/component_group_model.go +++ b/internal/component_group_model.go @@ -5,6 +5,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // componentGroupResourceModel maps the resource schema data. @@ -37,7 +39,7 @@ func (m *componentGroupResourceModel) fromRemote(spaceID int64, c *sbmgmt.Compon if c == nil { return fmt.Errorf("component-group is nil") } - m.ID = types.StringValue(createIdentifier(spaceID, c.Id)) + m.ID = types.StringValue(utils.CreateIdentifier(spaceID, c.Id)) m.GroupID = types.Int64Value(c.Id) m.Name = types.StringValue(c.Name) m.UUID = types.StringValue(c.Uuid.String()) diff --git a/internal/component_group_model_test.go b/internal/component_group_model_test.go index 271d3a9..987a103 100644 --- a/internal/component_group_model_test.go +++ b/internal/component_group_model_test.go @@ -7,6 +7,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/labd/storyblok-go-sdk/sbmgmt" "github.com/stretchr/testify/assert" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) func TestComponentGroupResourceModel_ToRemoteInput(t *testing.T) { @@ -42,7 +44,7 @@ func TestComponentGroupResourceModel_FromRemote(t *testing.T) { assert.NoError(t, err, "Error occurred during conversion") expectedModel := &componentGroupResourceModel{ - ID: types.StringValue(createIdentifier(spaceID, groupID)), + ID: types.StringValue(utils.CreateIdentifier(spaceID, groupID)), GroupID: types.Int64Value(groupID), UUID: types.StringValue("ebd1af2e-875f-47e5-8886-4d3baea94d99"), Name: types.StringValue(name), diff --git a/internal/component_group_resource.go b/internal/component_group_resource.go index 192cf62..85a4224 100644 --- a/internal/component_group_resource.go +++ b/internal/component_group_resource.go @@ -12,6 +12,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // Ensure the implementation satisfies the expected interfaces. @@ -82,7 +84,7 @@ func (r *componentGroupResource) Configure(_ context.Context, req resource.Confi return } - r.client = getClient(req.ProviderData) + r.client = utils.GetClient(req.ProviderData) } // Create creates the resource and sets the initial Terraform state. @@ -100,7 +102,7 @@ func (r *componentGroupResource) Create(ctx context.Context, req resource.Create spaceID := plan.SpaceID.ValueInt64() content, err := r.client.CreateComponentGroupWithResponse(ctx, spaceID, input) - if d := checkCreateError("component_group", content, err); d != nil { + if d := utils.CheckCreateError("component_group", content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -135,11 +137,11 @@ func (r *componentGroupResource) Read(ctx context.Context, req resource.ReadRequ return } - spaceId, groupId := parseIdentifier(state.ID.ValueString()) + spaceId, groupId := utils.ParseIdentifier(state.ID.ValueString()) // Get refreshed order value from HashiCups content, err := r.client.GetComponentGroupWithResponse(ctx, spaceId, groupId) - if d := checkGetError("component_group", groupId, content, err); d != nil { + if d := utils.CheckGetError("component_group", groupId, content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -178,7 +180,7 @@ func (r *componentGroupResource) Update(ctx context.Context, req resource.Update spaceID := plan.SpaceID.ValueInt64() content, err := r.client.UpdateComponentGroupWithResponse(ctx, spaceID, plan.GroupID.ValueInt64(), input) - if d := checkUpdateError("component_group", content, err); d != nil { + if d := utils.CheckUpdateError("component_group", content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -212,9 +214,9 @@ func (r *componentGroupResource) Delete(ctx context.Context, req resource.Delete return } - spaceId, groupId := parseIdentifier(state.ID.ValueString()) + spaceId, groupId := utils.ParseIdentifier(state.ID.ValueString()) content, err := r.client.DeleteComponentGroupWithResponse(ctx, spaceId, groupId) - if d := checkDeleteError("component_group", content, err); d != nil { + if d := utils.CheckDeleteError("component_group", content, err); d != nil { resp.Diagnostics.Append(d) return } diff --git a/internal/component_model.go b/internal/component_model.go index 876ed28..f88a730 100644 --- a/internal/component_model.go +++ b/internal/component_model.go @@ -5,6 +5,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // componentResourceModel maps the resource schema data. @@ -186,7 +188,7 @@ func (m *componentResourceModel) fromRemote(spaceID int64, c *sbmgmt.Component) if c == nil { return fmt.Errorf("component is nil") } - m.ID = types.StringValue(createIdentifier(spaceID, c.Id)) + m.ID = types.StringValue(utils.CreateIdentifier(spaceID, c.Id)) m.ComponentID = types.Int64Value(c.Id) m.CreatedAt = types.StringValue(c.CreatedAt.String()) m.IsRoot = types.BoolPointerValue(c.IsRoot) diff --git a/internal/component_resource.go b/internal/component_resource.go index adebe8a..b0b1075 100644 --- a/internal/component_resource.go +++ b/internal/component_resource.go @@ -20,6 +20,7 @@ import ( "github.com/labd/storyblok-go-sdk/sbmgmt" "github.com/labd/terraform-provider-storyblok/internal/customvalidators" + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // Ensure the implementation satisfies the expected interfaces. @@ -323,7 +324,7 @@ func (r *componentResource) Configure(_ context.Context, req resource.ConfigureR return } - r.client = getClient(req.ProviderData) + r.client = utils.GetClient(req.ProviderData) } // Create creates the resource and sets the initial Terraform state. @@ -388,11 +389,11 @@ func (r *componentResource) Read(ctx context.Context, req resource.ReadRequest, return } - spaceId, componentId := parseIdentifier(state.ID.ValueString()) + spaceId, componentId := utils.ParseIdentifier(state.ID.ValueString()) // Get refreshed order value from HashiCups content, err := r.client.GetComponentWithResponse(ctx, spaceId, componentId) - if d := checkGetError("component", componentId, content, err); d != nil { + if d := utils.CheckGetError("component", componentId, content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -477,7 +478,7 @@ func (r *componentResource) Delete(ctx context.Context, req resource.DeleteReque return } - spaceId, componentId := parseIdentifier(state.ID.ValueString()) + spaceId, componentId := utils.ParseIdentifier(state.ID.ValueString()) content, err := r.client.DeleteComponentWithResponse(ctx, spaceId, componentId) if err != nil { resp.Diagnostics.AddError( diff --git a/internal/space_role_model.go b/internal/space_role_model.go index 5502797..65d7f7b 100644 --- a/internal/space_role_model.go +++ b/internal/space_role_model.go @@ -2,8 +2,11 @@ package internal import ( "fmt" + "github.com/hashicorp/terraform-plugin-framework/types" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // spaceRoleResourceModel maps the resource schema data. @@ -68,7 +71,7 @@ func (m *spaceRoleResourceModel) fromRemote(spaceId int64, c *sbmgmt.SpaceRole) if c == nil { return fmt.Errorf("space role is nil") } - m.ID = types.StringValue(createIdentifier(spaceId, int64(c.Id))) + m.ID = types.StringValue(utils.CreateIdentifier(spaceId, int64(c.Id))) m.RoleID = types.Int64Value(int64(c.Id)) return nil } diff --git a/internal/space_role_resource.go b/internal/space_role_resource.go index 148fac1..07d0e00 100644 --- a/internal/space_role_resource.go +++ b/internal/space_role_resource.go @@ -2,16 +2,18 @@ package internal import ( "context" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" - "github.com/hashicorp/terraform-plugin-framework/types" "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/labd/storyblok-go-sdk/sbmgmt" + + "github.com/labd/terraform-provider-storyblok/internal/utils" ) // Ensure the implementation satisfies the expected interfaces. @@ -128,7 +130,7 @@ func (r *spaceRoleResource) Configure(_ context.Context, req resource.ConfigureR return } - r.client = getClient(req.ProviderData) + r.client = utils.GetClient(req.ProviderData) } // Create creates the resource and sets the initial Terraform state. @@ -146,7 +148,7 @@ func (r *spaceRoleResource) Create(ctx context.Context, req resource.CreateReque spaceID := plan.SpaceID.ValueInt64() content, err := r.client.CreateSpaceRoleWithResponse(ctx, spaceID, input) - if d := checkCreateError("space_role", content, err); d != nil { + if d := utils.CheckCreateError("space_role", content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -181,10 +183,10 @@ func (r *spaceRoleResource) Read(ctx context.Context, req resource.ReadRequest, return } - spaceId, groupId := parseIdentifier(state.ID.ValueString()) + spaceId, groupId := utils.ParseIdentifier(state.ID.ValueString()) content, err := r.client.GetSpaceRoleWithResponse(ctx, spaceId, groupId) - if d := checkGetError("space_role", groupId, content, err); d != nil { + if d := utils.CheckGetError("space_role", groupId, content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -223,7 +225,7 @@ func (r *spaceRoleResource) Update(ctx context.Context, req resource.UpdateReque spaceID := plan.SpaceID.ValueInt64() content, err := r.client.UpdateSpaceRoleWithResponse(ctx, spaceID, plan.RoleID.ValueInt64(), input) - if d := checkUpdateError("space_role", content, err); d != nil { + if d := utils.CheckUpdateError("space_role", content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -257,9 +259,9 @@ func (r *spaceRoleResource) Delete(ctx context.Context, req resource.DeleteReque return } - spaceId, spaceRoleId := parseIdentifier(state.ID.ValueString()) + spaceId, spaceRoleId := utils.ParseIdentifier(state.ID.ValueString()) content, err := r.client.DeleteSpaceRoleWithResponse(ctx, spaceId, spaceRoleId) - if d := checkDeleteError("space_role", content, err); d != nil { + if d := utils.CheckDeleteError("space_role", content, err); d != nil { resp.Diagnostics.Append(d) return } diff --git a/internal/utils.go b/internal/utils.go index e26213d..45eba4e 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -2,7 +2,6 @@ package internal import ( "bytes" - "fmt" "net/http" "text/template" @@ -14,23 +13,6 @@ import ( orderedmap "github.com/wk8/go-ordered-map/v2" ) -func createIdentifier(spaceId int64, id int64) string { - return fmt.Sprintf("%d/%d", spaceId, id) -} - -func parseIdentifier(identifier string) (spaceId int64, id int64) { - _, _ = fmt.Sscanf(identifier, "%d/%d", &spaceId, &id) - return -} - -func getClient(data any) sbmgmt.ClientWithResponsesInterface { - c, ok := data.(sbmgmt.ClientWithResponsesInterface) - if !ok { - panic("invalid client type") - } - return c -} - func sortComponentFields(input map[string]sbmgmt.FieldInput) *orderedmap.OrderedMap[string, sbmgmt.FieldInput] { type Pair struct { Key string diff --git a/internal/utils/client.go b/internal/utils/client.go new file mode 100644 index 0000000..5d51154 --- /dev/null +++ b/internal/utils/client.go @@ -0,0 +1,11 @@ +package utils + +import "github.com/labd/storyblok-go-sdk/sbmgmt" + +func GetClient(data any) sbmgmt.ClientWithResponsesInterface { + c, ok := data.(sbmgmt.ClientWithResponsesInterface) + if !ok { + panic("invalid client type") + } + return c +} diff --git a/internal/errors.go b/internal/utils/errors.go similarity index 90% rename from internal/errors.go rename to internal/utils/errors.go index 7d30862..511c458 100644 --- a/internal/errors.go +++ b/internal/utils/errors.go @@ -1,4 +1,4 @@ -package internal +package utils import ( "fmt" @@ -12,7 +12,7 @@ type ApiResponse interface { StatusCode() int } -func checkCreateError(name string, response ApiResponse, err error) *diag.ErrorDiagnostic { +func CheckCreateError(name string, response ApiResponse, err error) *diag.ErrorDiagnostic { if err != nil { d := diag.NewErrorDiagnostic( fmt.Sprintf("Error creating %s", name), @@ -31,7 +31,7 @@ func checkCreateError(name string, response ApiResponse, err error) *diag.ErrorD return nil } -func checkGetError(name string, id int64, response ApiResponse, err error) *diag.ErrorDiagnostic { +func CheckGetError(name string, id int64, response ApiResponse, err error) *diag.ErrorDiagnostic { if err != nil { d := diag.NewErrorDiagnostic( fmt.Sprintf("Error retrieving %s with id %d", name, id), @@ -50,7 +50,7 @@ func checkGetError(name string, id int64, response ApiResponse, err error) *diag return nil } -func checkUpdateError(name string, response ApiResponse, err error) *diag.ErrorDiagnostic { +func CheckUpdateError(name string, response ApiResponse, err error) *diag.ErrorDiagnostic { if err != nil { d := diag.NewErrorDiagnostic( fmt.Sprintf("Error updating %s", name), @@ -69,7 +69,7 @@ func checkUpdateError(name string, response ApiResponse, err error) *diag.ErrorD return nil } -func checkDeleteError(name string, response ApiResponse, err error) *diag.ErrorDiagnostic { +func CheckDeleteError(name string, response ApiResponse, err error) *diag.ErrorDiagnostic { if err != nil { d := diag.NewErrorDiagnostic( fmt.Sprintf("Error deleting %s", name), diff --git a/internal/utils/identifier.go b/internal/utils/identifier.go new file mode 100644 index 0000000..e9bd324 --- /dev/null +++ b/internal/utils/identifier.go @@ -0,0 +1,15 @@ +package utils + +import ( + "fmt" +) + +// CreateIdentifier creates a composite identifier from a space ID and an ID. +func CreateIdentifier(spaceId int64, id int64) string { + return fmt.Sprintf("%d/%d", spaceId, id) +} + +func ParseIdentifier(identifier string) (spaceId int64, id int64) { + _, _ = fmt.Sscanf(identifier, "%d/%d", &spaceId, &id) + return +}