Skip to content

Commit

Permalink
Handles 204 responses from AWX.
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-silvas committed Mar 28, 2024
1 parent bb4c509 commit bd40e8d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ terraform

develop/.env
develop/terraform-provider-*

test.go
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.3
v1.0.4
4 changes: 2 additions & 2 deletions internal/awx/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func resourceTeamCreate(ctx context.Context, d *schema.ResourceData, m interface
return resourceTeamRead(ctx, d, m)
}

func roleTeamEntitlementUpdate(m interface{}, team_id int, roles []interface{}, remove bool) error {
func roleTeamEntitlementUpdate(m interface{}, teamID int, roles []interface{}, remove bool) error {
client := m.(*awx.AWX)
awxService := client.TeamService

Expand All @@ -118,7 +118,7 @@ func roleTeamEntitlementUpdate(m interface{}, team_id int, roles []interface{},
payload["disassociate"] = true // presence of key triggers removal
}

_, err := awxService.UpdateTeamRoleEntitlement(team_id, payload, make(map[string]string))
_, err := awxService.UpdateTeamRoleEntitlement(teamID, payload, make(map[string]string))
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions tools/goawx/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ func (r *Requester) Do(ar *APIRequest, responseStruct interface{}, options ...in

response, err := r.Client.Do(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("Do.Request: %v", err)
}

//nolint:gomnd
if response.StatusCode == 400 { // Bad Request
var errorString string

errorList := map[string][]string{}
errorList := map[string]interface{}{}
if err := json.NewDecoder(response.Body).Decode(&errorList); err != nil {
return response, err
}
Expand All @@ -127,6 +127,11 @@ func (r *Requester) Do(ar *APIRequest, responseStruct interface{}, options ...in
return response, errors.New(errorString)
}

// If there is no response body, or if the response is of type `No Content` bypass the decode process.
if responseStruct == nil || response.StatusCode == 204 { //nolint:gomnd
return response, nil
}

switch responseStruct.(type) {
case *string:
return r.ReadRawResponse(response, responseStruct)
Expand Down
6 changes: 3 additions & 3 deletions tools/goawx/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ func (t *TeamService) UpdateTeam(id int, data map[string]interface{}, params map
}

// UpdateTeamRoleEntitlement updates the role entitlements for a team.
func (t *TeamService) UpdateTeamRoleEntitlement(id int, data map[string]interface{}, params map[string]string) (interface{}, error) {
func (t *TeamService) UpdateTeamRoleEntitlement(id int, data map[string]interface{}, _ map[string]string) (interface{}, error) {
result := new(interface{})
endpoint := fmt.Sprintf("%s%d/roles/", teamsAPIEndpoint, id)
payload, err := json.Marshal(data)
if err != nil {
return nil, err
return nil, fmt.Errorf("UpdateTeamRoleEntitlement:json.Marshal error in payload. %s", err)
}

resp, err := t.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
Expand All @@ -335,7 +335,7 @@ func (t *TeamService) UpdateTeamRoleEntitlement(id int, data map[string]interfac
}()
}
if err != nil {
return nil, err
return nil, fmt.Errorf("UpdateTeamRoleEntitlement:Requester.PostJSON error. %s", err)
}

if err := CheckResponse(resp); err != nil {
Expand Down

0 comments on commit bd40e8d

Please sign in to comment.