diff --git a/.changes/unreleased/Fixed-20230925-144243.yaml b/.changes/unreleased/Fixed-20230925-144243.yaml new file mode 100644 index 0000000..6642b8b --- /dev/null +++ b/.changes/unreleased/Fixed-20230925-144243.yaml @@ -0,0 +1,3 @@ +kind: Fixed +body: Re-added retry mechanism +time: 2023-09-25T14:42:43.710543891+02:00 diff --git a/internal/asset_folder_resource.go b/internal/asset_folder_resource.go index 893730c..cc3b25f 100644 --- a/internal/asset_folder_resource.go +++ b/internal/asset_folder_resource.go @@ -149,7 +149,7 @@ func (r *assetFolderResource) Read(ctx context.Context, req resource.ReadRequest spaceId, id := parseIdentifier(state.ID.ValueString()) content, err := r.client.GetAssetFolderWithResponse(ctx, spaceId, id) - if d := checkGetError("assetFolder", content, err); d != nil { + if d := checkGetError("assetFolder", id, content, err); d != nil { resp.Diagnostics.Append(d) return } @@ -206,7 +206,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", afResp, err); d != nil { + if d := checkGetError("assetFolder", plan.AssetFolderID.ValueInt64(), afResp, err); d != nil { resp.Diagnostics.Append(d) return } diff --git a/internal/component_group_resource.go b/internal/component_group_resource.go index b2a157d..192cf62 100644 --- a/internal/component_group_resource.go +++ b/internal/component_group_resource.go @@ -139,7 +139,7 @@ func (r *componentGroupResource) Read(ctx context.Context, req resource.ReadRequ // Get refreshed order value from HashiCups content, err := r.client.GetComponentGroupWithResponse(ctx, spaceId, groupId) - if d := checkGetError("component_group", content, err); d != nil { + if d := checkGetError("component_group", groupId, content, err); d != nil { resp.Diagnostics.Append(d) return } diff --git a/internal/component_resource.go b/internal/component_resource.go index 833cd22..adebe8a 100644 --- a/internal/component_resource.go +++ b/internal/component_resource.go @@ -392,7 +392,7 @@ func (r *componentResource) Read(ctx context.Context, req resource.ReadRequest, // Get refreshed order value from HashiCups content, err := r.client.GetComponentWithResponse(ctx, spaceId, componentId) - if d := checkGetError("component", content, err); d != nil { + if d := checkGetError("component", componentId, content, err); d != nil { resp.Diagnostics.Append(d) return } diff --git a/internal/debug.go b/internal/debug.go index f5c20a7..d1dcf03 100644 --- a/internal/debug.go +++ b/internal/debug.go @@ -7,15 +7,17 @@ import ( "os" ) -type LogTransport struct { - transport http.RoundTripper +func NewDebugTransport(innerTransport http.RoundTripper) http.RoundTripper { + return &DebugTransport{ + transport: innerTransport, + } } -var debugTransport = &LogTransport{ - transport: http.DefaultTransport, +type DebugTransport struct { + transport http.RoundTripper } -func (c *LogTransport) RoundTrip(request *http.Request) (*http.Response, error) { +func (c *DebugTransport) RoundTrip(request *http.Request) (*http.Response, error) { if os.Getenv("SB_DEBUG") != "" { logRequest(request) } diff --git a/internal/errors.go b/internal/errors.go index 7b919e2..7d30862 100644 --- a/internal/errors.go +++ b/internal/errors.go @@ -31,19 +31,19 @@ func checkCreateError(name string, response ApiResponse, err error) *diag.ErrorD return nil } -func checkGetError(name string, 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", name), - fmt.Sprintf("Could not retrieve %s, unexpected error: %s", name, err.Error())) + fmt.Sprintf("Error retrieving %s with id %d", name, id), + fmt.Sprintf("Could not retrieve %s with id %d, unexpected error: %s", name, id, err.Error())) return &d } if response.StatusCode() != http.StatusOK { d := diag.NewErrorDiagnostic( - fmt.Sprintf("Error retrieving %s", name), - fmt.Sprintf("Could not retrieve %s, status code: %d (%s)", - name, response.StatusCode(), readResponseBody(response))) + fmt.Sprintf("Error retrieving %s with id %d", name, id), + fmt.Sprintf("Could not retrieve %s with id %d, status code: %d (%s)", + name, id, response.StatusCode(), readResponseBody(response))) return &d } diff --git a/internal/provider.go b/internal/provider.go index 9df0e70..c0ed46b 100644 --- a/internal/provider.go +++ b/internal/provider.go @@ -37,7 +37,7 @@ func WithRetryableClient(retries int) OptionFunc { func WithDebugClient() OptionFunc { return func(p *storyblokProvider) { - p.httpClient.Transport = debugTransport + p.httpClient.Transport = NewDebugTransport(p.httpClient.Transport) } } @@ -70,8 +70,10 @@ func WithRecorderClient(file string, mode recorder.Mode) (OptionFunc, func() err // New is a helper function to simplify provider server and testing implementation. func New(opts ...OptionFunc) provider.Provider { + tp := http.DefaultTransport + var p = &storyblokProvider{ - httpClient: http.DefaultClient, + httpClient: &http.Client{Transport: tp}, } for _, opt := range opts { diff --git a/internal/space_role_resource.go b/internal/space_role_resource.go index 3df9dc9..148fac1 100644 --- a/internal/space_role_resource.go +++ b/internal/space_role_resource.go @@ -184,7 +184,7 @@ func (r *spaceRoleResource) Read(ctx context.Context, req resource.ReadRequest, spaceId, groupId := parseIdentifier(state.ID.ValueString()) content, err := r.client.GetSpaceRoleWithResponse(ctx, spaceId, groupId) - if d := checkGetError("space_role", content, err); d != nil { + if d := checkGetError("space_role", groupId, content, err); d != nil { resp.Diagnostics.Append(d) return } diff --git a/main.go b/main.go index f729def..2a05ef6 100644 --- a/main.go +++ b/main.go @@ -25,9 +25,9 @@ func main() { } err := providerserver.Serve(context.Background(), func() provider.Provider { - var options []internal.OptionFunc - { - internal.WithRetryableClient(10) + var options = []internal.OptionFunc{ + //We allow 10 retries of a failed request + internal.WithRetryableClient(10), } if debug {