Skip to content

Commit

Permalink
fix: fixed retry mechanism, added outputs to logs (#43)
Browse files Browse the repository at this point in the history
<!--

Describe in detail the changes you are proposing, and the rationale.

-->

<!--

Link all GitHub issues fixed by this PR, and add references to prior
related PRs.

-->

Fixes #42 

### NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES |
EXPERIMENTS

<!--

Write a short description of your changes. Examples:

- Fixed a bug
- Added a new feature
- Updated documentation

--> 

- fixed retry mechanism
- added outputs to logs
  • Loading branch information
demeyerthom authored Sep 25, 2023
1 parent b7e845a commit 237aa9b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20230925-144243.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Re-added retry mechanism
time: 2023-09-25T14:42:43.710543891+02:00
4 changes: 2 additions & 2 deletions internal/asset_folder_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/component_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/component_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 7 additions & 5 deletions internal/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions internal/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/space_role_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 237aa9b

Please sign in to comment.