Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add loggedin user account related entities and API endpoint interactions #52

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/account.go
Original file line number Diff line number Diff line change
@@ -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.AuthorisationTokenListItem, error)
UpdateTokenName(name, token string) error
}
59 changes: 59 additions & 0 deletions client/account.go
Original file line number Diff line number Diff line change
@@ -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.AuthorisationTokenListItem, error) {
authToken := make([]entity.AuthorisationTokenListItem, 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
}
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func constructClient(apiClient *APIClient) *Client {
SSHKeys: &SSHKeys{APIClient: *apiClient},
SSLKey: &SSLKey{APIClient: *apiClient},
SSLKeys: &SSLKeys{APIClient: *apiClient},
Account: &Account{APIClient: *apiClient},
}

return &client
Expand Down Expand Up @@ -152,6 +153,7 @@ type Client struct {
SSHKeys api.SSHKeys
SSLKey api.SSLKey
SSLKeys api.SSLKeys
Account api.Account
}

// GetAPIClient returns a MAAS API client.
Expand Down
15 changes: 15 additions & 0 deletions entity/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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"`
}

// AuthorisationTokenListItem represents user account authorisation token in list API
type AuthorisationTokenListItem struct {
Name string `json:"name,omitempty"`
Token string `json:"token,omitempty"`
}