-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ability to pull organization id from a provided token (#60)
* feat: added ability to pull organization id from a provided token * chore: followed linter advice * fix: added status code guard when checking
- Loading branch information
1 parent
ba870a5
commit 766db40
Showing
4 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package aptible | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type Organization struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type HALOrganizationParent struct { | ||
Organizations []Organization `json:"organizations"` | ||
} | ||
|
||
type HALOrganizationResponse struct { | ||
Embedded HALOrganizationParent `json:"_embedded"` | ||
} | ||
|
||
// getOrganizations - get all organizations a user has access to | ||
func (c *Client) getOrganizations() ([]Organization, error) { | ||
client := &http.Client{} | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/organizations", GetAuthURL()), nil) | ||
//Handle Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.RawToken)) | ||
req.Header.Set("Accept", "application/json") | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} else if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("did not receive 200 from auth, check credentials - %d status code received", resp.StatusCode) | ||
} | ||
|
||
defer func(Body io.ReadCloser) { | ||
bodyErr := Body.Close() | ||
if bodyErr != nil { | ||
log.Fatalf("Error in body reader close of http client %s\n", err.Error()) | ||
} | ||
}(resp.Body) | ||
|
||
out := HALOrganizationResponse{} | ||
if err = json.NewDecoder(resp.Body).Decode(&out); err != nil { | ||
return nil, err | ||
} | ||
|
||
return out.Embedded.Organizations, nil | ||
} | ||
|
||
// GetOrganization get organizations by user's token. Attempts to get organizations from auth api, then | ||
// attempts to get the FIRST one. if more than one is found or none are found, error is raised | ||
func (c *Client) GetOrganization() (Organization, error) { | ||
organizations, err := c.getOrganizations() | ||
if err != nil { | ||
return Organization{}, err | ||
} | ||
if len(organizations) > 1 { | ||
return Organization{}, fmt.Errorf("multiple organizations for user, unable to determine" + | ||
" a default organization in result") | ||
} | ||
|
||
if len(organizations) == 0 { | ||
return Organization{}, fmt.Errorf("no organizations found in response") | ||
} | ||
|
||
return organizations[0], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestOrganization(t *testing.T) { | ||
client := getClient(t) | ||
|
||
resp, err := client.GetOrganization() | ||
if err != nil { | ||
t.Fatalf("Error when trying to get organization - %s", err.Error()) | ||
return | ||
} | ||
if resp.ID == "" { | ||
t.Fatal("Error when retrieving organization, id not populated") | ||
return | ||
} | ||
|
||
fmt.Println("Organization ID retrieved", resp.ID) | ||
fmt.Println("Organization Name retrieved", resp.Name) | ||
} |