Skip to content

Commit

Permalink
Handle 404s of GetRole and 4XX on DeleteRole.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Feb 28, 2023
1 parent a868933 commit d7f9e9e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ func (r *RoleInterfaceProvider) Read(ctx context.Context, req resource.ReadReque
return
}

if authressSdkRole == nil {
resp.Diagnostics.AddError(
"Authress Role exists in the Terraform plan but does not exist in Authress:",
GetErrorWrapper("Either recreate the role in the Authress Management Portal or remove it from your state file. Role ID:" + currentAuthressRoleResource.RoleID.ValueString()),
)
return
}

// Set refreshed currentAuthressRoleResource
currentAuthressRoleResource = MapSdkRoleToTerraform(authressSdkRole)
diags = resp.State.Set(ctx, &currentAuthressRoleResource)
Expand Down
10 changes: 5 additions & 5 deletions src/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ func NewClient(customDomain string, accessKey string, version string) (*Client,
return &c, nil
}

func (c *Client) doRequest(req *http.Request) ([]byte, error) {
func (c *Client) doRequest(req *http.Request) ([]byte, int, error) {
req.Header.Set("Authorization", "Bearer " + c.AccessKey)
req.Header.Set("User-Agent", "Terraform SDK " + c.Version)

res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
return nil, 0, err
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
return nil, res.StatusCode, err
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
return nil, res.StatusCode, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
}

return body, err
return body, res.StatusCode, err
}
17 changes: 12 additions & 5 deletions src/sdk/rolesApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c *Client) GetRoles() ([]Role, error) {
return nil, err
}

body, err := c.doRequest(req)
body, _, err := c.doRequest(req)
if err != nil {
return nil, err
}
Expand All @@ -33,7 +33,10 @@ func (c *Client) GetRole(roleID string) (*Role, error) {
return nil, err
}

body, err := c.doRequest(req)
body, status, err := c.doRequest(req)
if status == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
Expand All @@ -58,7 +61,7 @@ func (c *Client) CreateRole(role Role) (*Role, error) {
return nil, err
}

body, err := c.doRequest(req)
body, _, err := c.doRequest(req)
if err != nil {
return nil, err
}
Expand All @@ -83,7 +86,7 @@ func (c *Client) UpdateRole(roleID string, role Role) (*Role, error) {
return nil, err
}

body, err := c.doRequest(req)
body, _, err := c.doRequest(req)
if err != nil {
return nil, err
}
Expand All @@ -104,7 +107,11 @@ func (c *Client) DeleteRole(roleID string) error {
return err
}

_, err = c.doRequest(req)
_, status, err := c.doRequest(req)

if status < http.StatusInternalServerError {
return nil
}

if err != nil {
return err
Expand Down

0 comments on commit d7f9e9e

Please sign in to comment.