diff --git a/account.go b/account.go index 11eca7b..e6b13f5 100644 --- a/account.go +++ b/account.go @@ -95,6 +95,10 @@ func (c *Client) CreateAccountUserJSON(accountID string, params UserParams) (str Send(params). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 201 { // Expect Created on success errs = ProcessErrors(response.StatusCode, body) } @@ -120,6 +124,10 @@ func (c *Client) DeleteAccountUserJSON(accountID, userID string) (string, []erro apibase+"accounts/"+accountID+"/users/"+userID). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect OK on success errs = ProcessErrors(response.StatusCode, body) } diff --git a/audit_events.go b/audit_events.go index 7bc2d1a..788dec6 100644 --- a/audit_events.go +++ b/audit_events.go @@ -57,6 +57,10 @@ func (c *Client) GetAuditEventsJSON(params AuditEventsParams) (string, []error) Query(params). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else myerrors := Errors{} err := json.Unmarshal([]byte(body), &myerrors) @@ -86,6 +90,10 @@ func (c *Client) GetAuditEventJSON(id string) (string, []error) { response, body, errs := c.newRequest("GET", apibase+"/audit_events/"+id). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/backups.go b/backups.go index dbacd9c..6d601c2 100644 --- a/backups.go +++ b/backups.go @@ -64,6 +64,10 @@ func (c *Client) StartBackupForDeploymentJSON(deploymentid string) (string, []er response, body, errs := c.newRequest("POST", apibase+"deployments/"+deploymentid+"/backups"). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } @@ -145,6 +149,10 @@ func (c *Client) RestoreBackupJSON(params RestoreBackupParams) (string, []error) Send(backupparams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/composeapi.go b/composeapi.go index c003f32..3056116 100644 --- a/composeapi.go +++ b/composeapi.go @@ -19,6 +19,7 @@ package composeapi import ( "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -30,7 +31,8 @@ import ( ) const ( - apibase = "https://api.compose.io/2016-07/" + apibase = "https://api.compose.io/2016-07/" + gorequestInternalFailure = "An internal error occurred. Could not complete request" ) // Client is a structure that holds session information for the API @@ -122,6 +124,9 @@ func (c *Client) SetAPIToken(newtoken string) { //GetJSON Gets JSON string of content at an endpoint func (c *Client) getJSON(endpoint string) (string, []error) { response, body, errs := c.newRequest("GET", apibase+endpoint).End() + if response == nil { + return internalError(errs) + } if response.StatusCode != 200 { errs = ProcessErrors(response.StatusCode, body) @@ -151,3 +156,10 @@ func ProcessErrors(statuscode int, body string) []error { return errs } + +func internalError(errs []error) (string, []error) { + if len(errs) > 0 { + return "", errs + } + return "", []error{errors.New(gorequestInternalFailure)} +} diff --git a/database.go b/database.go index 294001a..27d1139 100644 --- a/database.go +++ b/database.go @@ -56,6 +56,10 @@ func (c *Client) UpdateVersionJSON(deploymentID string, version string) (string, Send(patchParams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect OK on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/deployment.go b/deployment.go index bcaf7ad..0cd313b 100644 --- a/deployment.go +++ b/deployment.go @@ -134,6 +134,10 @@ func (c *Client) CreateDeploymentJSON(params DeploymentParams) (string, []error) Send(deploymentparams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else myerrors := Errors{} err := json.Unmarshal([]byte(body), &myerrors) @@ -223,6 +227,10 @@ func (c *Client) DeprovisionDeploymentJSON(deploymentID string) (string, []error response, body, errs := c.newRequest("DELETE", apibase+"deployments/"+deploymentID). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } @@ -260,6 +268,10 @@ func (c *Client) PatchDeploymentJSON(params PatchDeploymentParams) (string, []er Send(patchParams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/scalings.go b/scalings.go index b10adcb..806e149 100644 --- a/scalings.go +++ b/scalings.go @@ -73,6 +73,10 @@ func (c *Client) SetScalingsJSON(params ScalingsParams) (string, []error) { Send(scalingsparams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/tags.go b/tags.go index 61c4bba..e2bf666 100644 --- a/tags.go +++ b/tags.go @@ -63,6 +63,10 @@ func (c *Client) updateClusterTagsJSON(clusterID, method string, tags []string) Send(clusterTags{ClusterTags: clusterTagList{Tags: tags}}). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect OK on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/team_roles.go b/team_roles.go index f59a22d..654527f 100644 --- a/team_roles.go +++ b/team_roles.go @@ -16,6 +16,7 @@ package composeapi import ( "encoding/json" + "errors" "fmt" ) @@ -48,6 +49,10 @@ func (c *Client) CreateTeamRoleJSON(deploymentID string, params TeamRoleParams) Send(updateTeamRole{TeamRole: params}). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 201 { myErrors := Errors{} err := json.Unmarshal([]byte(body), &myErrors) @@ -99,6 +104,13 @@ func (c *Client) DeleteTeamRoleJSON(deploymentID string, params TeamRoleParams) Send(updateTeamRole{TeamRole: params}). End() + if response == nil { + if len(errs) > 0 { + return errs + } + return []error{errors.New(gorequestInternalFailure)} + } + if response.StatusCode != 204 { // No response body is returned on success errs = ProcessErrors(response.StatusCode, body) } diff --git a/teams.go b/teams.go index c7f117b..10961e4 100644 --- a/teams.go +++ b/teams.go @@ -57,6 +57,10 @@ func (c *Client) CreateTeamJSON(params TeamParams) (string, []error) { Send(teamParams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 201 { // Expect Created on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } @@ -137,6 +141,10 @@ func (c *Client) DeleteTeamJSON(teamID string) (string, []error) { response, body, errs := c.newRequest("DELETE", apibase+"teams/"+teamID). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect OK on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } @@ -165,6 +173,10 @@ func (c *Client) PatchTeamJSON(teamID, teamName string) (string, []error) { Send(patchParams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect OK on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } @@ -193,6 +205,10 @@ func (c *Client) PutTeamUsersJSON(teamID string, userIDs []string) (string, []er Send(putUsers). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 200 { // Expect OK on success - assume error on anything else errs = ProcessErrors(response.StatusCode, body) } diff --git a/whitelist.go b/whitelist.go index 70da292..a0327e8 100644 --- a/whitelist.go +++ b/whitelist.go @@ -58,6 +58,10 @@ func (c *Client) createDeploymentWhitelistJSON(deploymentID string, params Deplo Send(whitelistParams). End() + if response == nil { + return internalError(errs) + } + if response.StatusCode != 202 { myerrors := Errors{} err := json.Unmarshal([]byte(body), &myerrors)