Skip to content

Commit

Permalink
feat: add loggedin user account related entities and API enpoint
Browse files Browse the repository at this point in the history
interactions
  • Loading branch information
sachin120 committed Jan 15, 2024
1 parent f9e15e9 commit 4c3359b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
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.AuthorisationToken, 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.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
}
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions entity/account.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 4c3359b

Please sign in to comment.