From c8b36f658c0e84fd36f7506b727e7e0622fe7702 Mon Sep 17 00:00:00 2001 From: Sachin Shingare Date: Mon, 15 Jan 2024 12:58:00 +0000 Subject: [PATCH] feat: add loggedin user account related entities and API enpoint interactions --- api/account.go | 11 +++++++++ client/account.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ client/client.go | 2 ++ entity/account.go | 9 ++++++++ 4 files changed, 81 insertions(+) create mode 100644 api/account.go create mode 100644 client/account.go create mode 100644 entity/account.go diff --git a/api/account.go b/api/account.go new file mode 100644 index 0000000..9a051b5 --- /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) error + ListAuthorisationTokens() ([]entity.AuthorisationToken, error) + UpdateTokenName(name, token string) error +} diff --git a/client/account.go b/client/account.go new file mode 100644 index 0000000..390725f --- /dev/null +++ b/client/account.go @@ -0,0 +1,59 @@ +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) error { + qsp := url.Values{} + qsp.Set("token_key", key) + err := a.client().Post("delete_authorisation_token", qsp, func(data []byte) error { return nil }) + + return err +} + +// 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) error { + qsp := url.Values{} + qsp.Set("name", name) + qsp.Set("token", token) + err := a.client().Post("update_token_name", qsp, func(data []byte) error { return nil }) + + return err +} 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..b84d72c --- /dev/null +++ b/entity/account.go @@ -0,0 +1,9 @@ +package entity + +// AuthorisationToken represents user account authorisation Token +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"` +}