-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
telyn
committed
Dec 3, 2018
1 parent
7bd2866
commit 2a2789e
Showing
3 changed files
with
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
package brain | ||
|
||
// APIKey represents an api_key in the brain. | ||
type APIKey struct { | ||
ID int `json:"id,omitempty"` | ||
UserID int `json:"user_id,omitempty"` | ||
Label string `json:"label,omitempty"` | ||
APIKey string `json:"api_key,omitempty"` | ||
// ID must not be set during creation. | ||
ID int `json:"id,omitempty"` | ||
UserID int `json:"user_id,omitempty"` | ||
// Label is a friendly display name for this API key | ||
Label string `json:"label,omitempty"` | ||
// API key is the actual key. To use it, it must be prepended with | ||
// 'apikey.' in the HTTP Authorization header. For example, if the api key | ||
// is xpq21, the HTTP headers should include `Authorization: Bearer apikey.xpq21` | ||
APIKey string `json:"api_key,omitempty"` | ||
// ExpiresAt should be a time or datetime in HH:MM:SS or | ||
// YYYY-MM-DDTHH:MM:SS.msZ where T is a literal T, .ms are optional | ||
// microseconds and Z is either literal Z (meaning UTC) or a timezone | ||
// specified like -600 or +1200 | ||
ExpiresAt string `json:"expires_at,omitempty"` | ||
// Privileges cannot be set at creation or update time, but are returned by | ||
// the brain when view=overview. | ||
Privileges Privileges `json:"privileges,omitempty"` | ||
} |
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,18 @@ | ||
package brain | ||
|
||
import ( | ||
"github.com/BytemarkHosting/bytemark-client/lib" | ||
"github.com/BytemarkHosting/bytemark-client/lib/brain" | ||
) | ||
|
||
// GetAPIKeys gets all API keys that you can currently see. | ||
// In general this means those for your user, but users with cluster_admin | ||
// will be able to see all API keys on the cluster | ||
func GetAPIKeys(client lib.Client) (apiKeys []brain.APIKey, err error) { | ||
r, err := client.BuildRequest("GET", lib.BrainEndpoint, "/api_keys?view=overview") | ||
if err != nil { | ||
return | ||
} | ||
_, _, err = r.MarshalAndRun(nil, &apiKeys) | ||
return | ||
} |
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,75 @@ | ||
package brain_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/BytemarkHosting/bytemark-client/lib" | ||
"github.com/BytemarkHosting/bytemark-client/lib/brain" | ||
brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" | ||
"github.com/BytemarkHosting/bytemark-client/lib/testutil" | ||
"github.com/BytemarkHosting/bytemark-client/lib/testutil/assert" | ||
) | ||
|
||
func TestGetAPIKeys(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
response []brain.APIKey | ||
statusCode int | ||
shouldErr bool | ||
}{ | ||
{ | ||
name: "empty array", | ||
}, | ||
{ | ||
name: "http 500", | ||
statusCode: 500, | ||
shouldErr: true, | ||
}, | ||
{ | ||
name: "some keys", | ||
response: []brain.APIKey{ | ||
{ | ||
ID: 6, | ||
UserID: 2152, | ||
Label: "gitlab-autoscaling", | ||
APIKey: "extremelyrandomdatahereuhhhhh7", | ||
ExpiresAt: "2019-03-21T00:24:45.0312Z", | ||
Privileges: brain.Privileges{ | ||
{ | ||
Username: "dr-gitlabotopis", | ||
APIKeyID: 6, | ||
GroupID: 2433, | ||
Level: "group_admin", | ||
}, | ||
}, | ||
}, { | ||
ID: 912, | ||
UserID: 2505, | ||
Label: "kubernetes-cloud-controller-manager", | ||
APIKey: "keykeykeykeykeynoonesgonnaguessthat", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
rts := testutil.RequestTestSpec{ | ||
Method: "GET", | ||
Endpoint: lib.BrainEndpoint, | ||
URL: "/api_keys", | ||
StatusCode: test.statusCode, | ||
Response: test.response, | ||
} | ||
rts.Run(t, test.name, true, func(client lib.Client) { | ||
keys, err := brainRequests.GetAPIKeys(client) | ||
if err != nil && !test.shouldErr { | ||
t.Errorf("Unexpected error: %v", err) | ||
} else if err == nil && test.shouldErr { | ||
t.Error("Error expected but not returned") | ||
} | ||
assert.Equal(t, "api keys", test.response, keys) | ||
}) | ||
}) | ||
} | ||
} |