diff --git a/api/account.go b/api/account.go new file mode 100644 index 0000000..081c56f --- /dev/null +++ b/api/account.go @@ -0,0 +1,11 @@ +package api + +import "github.com/maas/gomaasclient/entity" + +// Account is an interface for managing User Account +type Account interface { + CreateAuthorisationToken(name string) (*entity.AuthorisationToken, error) + DeleteAuthorisationToken(key string) + ListAuthorisationTokens() ([]entity.AuthorisationToken, error) + UpdateTokenName(name, token string) +} diff --git a/client/account.go b/client/account.go new file mode 100644 index 0000000..5aa9c68 --- /dev/null +++ b/client/account.go @@ -0,0 +1,55 @@ +package client + +import ( + "encoding/json" + "net/url" + + "github.com/maas/gomaasclient/entity" +) + +// Account implements api.Account +type Account struct { + APIClient APIClient +} + +func (a *Account) client() APIClient { + return a.APIClient.GetSubObject("account") +} + +// CreateAuthorisationToken creates authorisation token with provided name +func (a *Account) CreateAuthorisationToken(name string) (*entity.AuthorisationToken, error) { + qsp := url.Values{} + qsp.Set("name", name) + + authToken := new(entity.AuthorisationToken) + err := a.client().Post("create_authorisation_token", qsp, func(data []byte) error { + return json.Unmarshal(data, authToken) + }) + + return authToken, err +} + +// DeleteAuthorisationToken deletes authorisation token with provided key +func (a *Account) DeleteAuthorisationToken(key string) { + qsp := url.Values{} + qsp.Set("token_key", key) + a.client().Post("delete_authorisation_token", qsp, func(data []byte) error { return nil }) +} + +// ListAuthorisationTokens Lists authorisation tokens +func (a *Account) ListAuthorisationTokens() ([]entity.AuthorisationToken, error) { + authToken := make([]entity.AuthorisationToken, 0) + err := a.client().Get("list_authorisation_tokens", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, &authToken) + }) + + return authToken, err +} + +// UpdateTokenName updates given token with provided name +func (a *Account) UpdateTokenName(name, token string) { + qsp := url.Values{} + qsp.Set("name", name) + qsp.Set("token", token) + a.client().Post("update_token_name", qsp, func(data []byte) error { return nil }) +} diff --git a/client/client.go b/client/client.go index a252ab9..be44235 100644 --- a/client/client.go +++ b/client/client.go @@ -84,6 +84,7 @@ func constructClient(apiClient *APIClient) *Client { BCaches: &BCaches{APIClient: *apiClient}, BCacheCacheSet: &BCacheCacheSet{APIClient: *apiClient}, BCacheCacheSets: &BCacheCacheSets{APIClient: *apiClient}, + Account: &Account{APIClient: *apiClient}, } return &client @@ -144,6 +145,7 @@ type Client struct { BCaches api.BCaches BCacheCacheSet api.BCacheCacheSet BCacheCacheSets api.BCacheCacheSets + Account api.Account } // GetAPIClient returns a MAAS API client. diff --git a/entity/account.go b/entity/account.go new file mode 100644 index 0000000..b892416 --- /dev/null +++ b/entity/account.go @@ -0,0 +1,8 @@ +package entity + +type AuthorisationToken struct { + Name string `json:"name,omitempty"` + TokenKey string `json:"token_key,omitempty"` + TokenSecret string `json:"token_secret,omitempty"` + ConsumerKey string `json:"consumer_key,omitempty"` +}