Skip to content

Commit

Permalink
feat: added ability to pull organization id from a provided token (#60)
Browse files Browse the repository at this point in the history
* 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
madhuravius authored Oct 26, 2022
1 parent ba870a5 commit 766db40
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
7 changes: 5 additions & 2 deletions aptible/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

type Client struct {
Client *deploy.DeployAPIV1
Token runtime.ClientAuthInfoWriter
Client *deploy.DeployAPIV1
Token runtime.ClientAuthInfoWriter
RawToken string
}

// sets up client and gets auth token used for API requests
Expand All @@ -36,11 +37,13 @@ func SetUpClient() (*Client, error) {
if err != nil {
return nil, err
}

bearerTokenAuth := httptransport.BearerToken(token)

c := Client{}
c.Client = client
c.Token = bearerTokenAuth
c.RawToken = token
return &c, nil
}

Expand Down
74 changes: 74 additions & 0 deletions aptible/organizations.go
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
}
9 changes: 7 additions & 2 deletions aptible/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ func loginWithUsernameAndPassword(authUrl, user, password string) (string, error
return output["access_token"].(string), nil
}

// GetToken - gets a token (or error) for use from username password, an environment variable or filesystem
func GetToken() (string, error) {
func GetAuthURL() string {
// Gets auth server
auth := os.Getenv("APTIBLE_AUTH_ROOT_URL")
if auth == "" {
auth = "https://auth.aptible.com"
}
return auth
}

// GetToken - gets a token (or error) for use from username password, an environment variable or filesystem
func GetToken() (string, error) {
auth := GetAuthURL()

// use auth-server to get tokens
user := os.Getenv("APTIBLE_USERNAME")
Expand Down
26 changes: 26 additions & 0 deletions test/integration/organizations_test.go
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)
}

0 comments on commit 766db40

Please sign in to comment.