diff --git a/api/ssh_key.go b/api/ssh_key.go new file mode 100644 index 0000000..4b8321e --- /dev/null +++ b/api/ssh_key.go @@ -0,0 +1,11 @@ +package api + +import ( + "github.com/maas/gomaasclient/entity" +) + +// SSHKey is an interface defining API behaviour for SSHKey objects +type SSHKey interface { + Get(id int) (*entity.SSHKey, error) + Delete(id int) error +} diff --git a/api/ssh_keys.go b/api/ssh_keys.go new file mode 100644 index 0000000..de44589 --- /dev/null +++ b/api/ssh_keys.go @@ -0,0 +1,12 @@ +package api + +import ( + "github.com/maas/gomaasclient/entity" +) + +// SSHKeys is an interface for listing and creating SSHKey objects +type SSHKeys interface { + Get() ([]entity.SSHKey, error) + Create(key string) (*entity.SSHKey, error) + Import(keysource string) ([]entity.SSHKey, error) +} diff --git a/client/client.go b/client/client.go index a252ab9..cffd5a4 100644 --- a/client/client.go +++ b/client/client.go @@ -84,6 +84,8 @@ func constructClient(apiClient *APIClient) *Client { BCaches: &BCaches{APIClient: *apiClient}, BCacheCacheSet: &BCacheCacheSet{APIClient: *apiClient}, BCacheCacheSets: &BCacheCacheSets{APIClient: *apiClient}, + SSHKey: &SSHKey{APIClient: *apiClient}, + SSHKeys: &SSHKeys{APIClient: *apiClient}, } return &client @@ -144,6 +146,8 @@ type Client struct { BCaches api.BCaches BCacheCacheSet api.BCacheCacheSet BCacheCacheSets api.BCacheCacheSets + SSHKey api.SSHKey + SSHKeys api.SSHKeys } // GetAPIClient returns a MAAS API client. diff --git a/client/ssh_key.go b/client/ssh_key.go new file mode 100644 index 0000000..6f8b387 --- /dev/null +++ b/client/ssh_key.go @@ -0,0 +1,33 @@ +package client + +import ( + "encoding/json" + "fmt" + "net/url" + + "github.com/maas/gomaasclient/entity" +) + +// SSHKey implements api.SSHKey +type SSHKey struct { + APIClient APIClient +} + +func (s *SSHKey) client(id int) APIClient { + return s.APIClient.GetSubObject("account/prefs/sshkeys").GetSubObject(fmt.Sprintf("%v", id)) +} + +// Get fetches a given SSHKey +func (s *SSHKey) Get(id int) (*entity.SSHKey, error) { + sshKey := new(entity.SSHKey) + err := s.client(id).Get("", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, sshKey) + }) + + return sshKey, err +} + +// Delete deletes a given SSHKey +func (s *SSHKey) Delete(id int) error { + return s.client(id).Delete() +} diff --git a/client/ssh_keys.go b/client/ssh_keys.go new file mode 100644 index 0000000..c9ac3d4 --- /dev/null +++ b/client/ssh_keys.go @@ -0,0 +1,51 @@ +package client + +import ( + "encoding/json" + "net/url" + + "github.com/maas/gomaasclient/entity" +) + +// SSHKeys implements api.SSHKeys +type SSHKeys struct { + APIClient APIClient +} + +func (s *SSHKeys) client() APIClient { + return s.APIClient.GetSubObject("account/prefs/sshkeys") +} + +// Get fetches a list of SSHKey objects +func (s *SSHKeys) Get() ([]entity.SSHKey, error) { + sshKeys := make([]entity.SSHKey, 0) + err := s.client().Get("", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, &sshKeys) + }) + + return sshKeys, err +} + +// Create creates a new SSHKey +func (s *SSHKeys) Create(key string) (*entity.SSHKey, error) { + sshKey := new(entity.SSHKey) + qsp := url.Values{} + qsp.Set("key", key) + err := s.client().Post("", qsp, func(data []byte) error { + return json.Unmarshal(data, sshKey) + }) + + return sshKey, err +} + +// Import creates a new SSHKey +func (s *SSHKeys) Import(keysource string) ([]entity.SSHKey, error) { + sshKeys := make([]entity.SSHKey, 0) + qsp := url.Values{} + qsp.Set("keysource", keysource) + err := s.client().Post("import", qsp, func(data []byte) error { + return json.Unmarshal(data, &sshKeys) + }) + + return sshKeys, err +} diff --git a/entity/ssh_key.go b/entity/ssh_key.go new file mode 100644 index 0000000..e3f967f --- /dev/null +++ b/entity/ssh_key.go @@ -0,0 +1,8 @@ +package entity + +type SSHKey struct { + ID int `json:"id,omitempty"` + Key string `json:"key,omitempty"` + Keysource string `json:"keysource,omitempty"` + ResourceURI string `json:"resource_uri,omitempty"` +}