Skip to content

Commit

Permalink
fix: handle 404 when reading user roles
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Apr 29, 2024
1 parent ad50c39 commit b4936a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
22 changes: 6 additions & 16 deletions internal/provider/resource_application_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,8 @@ func (r *ResourceApplicationUser) Read(ctx context.Context, req resource.ReadReq
}

var httpResp *client.GetUserRoleInAppResponse
err := retry.RetryContext(ctx, defaultApplicationUserReadTimeout, func() *retry.RetryError {
var err error
httpResp, err = r.client.GetUserRoleInAppWithResponse(ctx, r.orgId, data.AppID.ValueString(), data.UserID.ValueString())
if err != nil {
return retry.NonRetryableError(err)
}

if httpResp.StatusCode() == 404 {
return retry.RetryableError(fmt.Errorf("waiting for application to be ready, status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
}

if httpResp.StatusCode() != 200 {
return retry.NonRetryableError(fmt.Errorf("unable to read resource application user, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
}

return nil
})
httpResp, err := r.client.GetUserRoleInAppWithResponse(ctx, r.orgId, data.AppID.ValueString(), data.UserID.ValueString())
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to read resource application user, got error: %s", err))
return
Expand All @@ -203,6 +188,11 @@ func (r *ResourceApplicationUser) Read(ctx context.Context, req resource.ReadReq
return
}

if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to get application user, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
return
}

data.Role = types.StringValue(httpResp.JSON200.Role)

// Save updated data into Terraform state
Expand Down
26 changes: 7 additions & 19 deletions internal/provider/resource_environment_type_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,35 +174,23 @@ func (r *ResourceEnvironmentTypeUser) Read(ctx context.Context, req resource.Rea
return
}

var httpResp *client.GetUserRoleInEnvTypeResponse
err := retry.RetryContext(ctx, defaultEnvironmentTypeUserReadTimeout, func() *retry.RetryError {
var err error
httpResp, err = r.client.GetUserRoleInEnvTypeWithResponse(ctx, r.orgId, data.EnvTypeID.ValueString(), data.UserID.ValueString())
if err != nil {
return retry.NonRetryableError(err)
}

if httpResp.StatusCode() == 404 {
return retry.RetryableError(fmt.Errorf("waiting for application to be ready, status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
}

if httpResp.StatusCode() != 200 {
return retry.NonRetryableError(fmt.Errorf("unable to read resource environment type user, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
}

return nil
})
httpResp, err := r.client.GetUserRoleInEnvTypeWithResponse(ctx, r.orgId, data.EnvTypeID.ValueString(), data.UserID.ValueString())
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to read resource environment type user, got error: %s", err))
return
}

if httpResp.StatusCode() == 404 {
resp.Diagnostics.AddWarning("Application user not found", fmt.Sprintf("The application user (%s) was deleted outside Terraform", data.ID.ValueString()))
resp.Diagnostics.AddWarning("Environment Type user not found", fmt.Sprintf("The environment type user (%s) was deleted outside Terraform", data.ID.ValueString()))
resp.State.RemoveResource(ctx)
return
}

if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to get environment type user, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
return
}

data.Role = types.StringValue(httpResp.JSON200.Role)

// Save updated data into Terraform state
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ func (r *ResourceUser) Read(ctx context.Context, req resource.ReadRequest, resp
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to read user, got error: %s", err))
return
}
if httpResp.StatusCode() == 404 {
resp.Diagnostics.AddWarning("User not found", fmt.Sprintf("The user (%s) was deleted outside Terraform", data.ID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to read user, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
return
Expand Down

0 comments on commit b4936a6

Please sign in to comment.