Skip to content

Commit

Permalink
refactor: move utils to internal.utils
Browse files Browse the repository at this point in the history
prepare for re-organizing the code-base a bit
  • Loading branch information
mvantellingen committed May 13, 2024
1 parent fd6c011 commit e4beb78
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 55 deletions.
4 changes: 3 additions & 1 deletion internal/asset_folder_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions internal/asset_folder_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion internal/component_group_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion internal/component_group_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 9 additions & 7 deletions internal/component_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion internal/component_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions internal/component_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion internal/space_role_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
22 changes: 12 additions & 10 deletions internal/space_role_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 0 additions & 18 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import (
"bytes"
"fmt"
"net/http"
"text/template"

Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions internal/utils/client.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit e4beb78

Please sign in to comment.