From 0f6a7f8ef2444d358c0b91617ca51624252b9407 Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Sat, 13 Jan 2024 10:38:37 +0200 Subject: [PATCH] Add SSL Keys --- api/ssl_key.go | 11 +++++++++++ api/ssl_keys.go | 11 +++++++++++ client/client.go | 4 ++++ client/ssl_key.go | 33 +++++++++++++++++++++++++++++++++ client/ssl_keys.go | 39 +++++++++++++++++++++++++++++++++++++++ entity/ssl_key.go | 7 +++++++ 6 files changed, 105 insertions(+) create mode 100644 api/ssl_key.go create mode 100644 api/ssl_keys.go create mode 100644 client/ssl_key.go create mode 100644 client/ssl_keys.go create mode 100644 entity/ssl_key.go diff --git a/api/ssl_key.go b/api/ssl_key.go new file mode 100644 index 0000000..0889cbf --- /dev/null +++ b/api/ssl_key.go @@ -0,0 +1,11 @@ +package api + +import ( + "github.com/maas/gomaasclient/entity" +) + +// SSLKey is an interface defining API behaviour for SSLKey objects +type SSLKey interface { + Get(id int) (*entity.SSLKey, error) + Delete(id int) error +} diff --git a/api/ssl_keys.go b/api/ssl_keys.go new file mode 100644 index 0000000..367af7b --- /dev/null +++ b/api/ssl_keys.go @@ -0,0 +1,11 @@ +package api + +import ( + "github.com/maas/gomaasclient/entity" +) + +// SSLKeys is an interface for listing and creating SSLKey objects +type SSLKeys interface { + Get() ([]entity.SSLKey, error) + Create(key string) (*entity.SSLKey, error) +} diff --git a/client/client.go b/client/client.go index cffd5a4..287e435 100644 --- a/client/client.go +++ b/client/client.go @@ -86,6 +86,8 @@ func constructClient(apiClient *APIClient) *Client { BCacheCacheSets: &BCacheCacheSets{APIClient: *apiClient}, SSHKey: &SSHKey{APIClient: *apiClient}, SSHKeys: &SSHKeys{APIClient: *apiClient}, + SSLKey: &SSLKey{APIClient: *apiClient}, + SSLKeys: &SSLKeys{APIClient: *apiClient}, } return &client @@ -148,6 +150,8 @@ type Client struct { BCacheCacheSets api.BCacheCacheSets SSHKey api.SSHKey SSHKeys api.SSHKeys + SSLKey api.SSLKey + SSLKeys api.SSLKeys } // GetAPIClient returns a MAAS API client. diff --git a/client/ssl_key.go b/client/ssl_key.go new file mode 100644 index 0000000..a57e94c --- /dev/null +++ b/client/ssl_key.go @@ -0,0 +1,33 @@ +package client + +import ( + "encoding/json" + "fmt" + "net/url" + + "github.com/maas/gomaasclient/entity" +) + +// SSLKey implements api.SSLKey +type SSLKey struct { + APIClient APIClient +} + +func (s *SSLKey) client(id int) APIClient { + return s.APIClient.GetSubObject("account/prefs/sslkeys").GetSubObject(fmt.Sprintf("%v", id)) +} + +// Get fetches a given SSLKey +func (s *SSLKey) Get(id int) (*entity.SSLKey, error) { + sslKey := new(entity.SSLKey) + err := s.client(id).Get("", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, sslKey) + }) + + return sslKey, err +} + +// Delete deletes a given SSLKey +func (s *SSLKey) Delete(id int) error { + return s.client(id).Delete() +} diff --git a/client/ssl_keys.go b/client/ssl_keys.go new file mode 100644 index 0000000..5ff2600 --- /dev/null +++ b/client/ssl_keys.go @@ -0,0 +1,39 @@ +package client + +import ( + "encoding/json" + "net/url" + + "github.com/maas/gomaasclient/entity" +) + +// SSLKeys implements api.SSLKeys +type SSLKeys struct { + APIClient APIClient +} + +func (s *SSLKeys) client() APIClient { + return s.APIClient.GetSubObject("account/prefs/sslkeys") +} + +// Get fetches a list of SSLKey objects +func (s *SSLKeys) Get() ([]entity.SSLKey, error) { + sslKeys := make([]entity.SSLKey, 0) + err := s.client().Get("", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, &sslKeys) + }) + + return sslKeys, err +} + +// Create creates a new SSLKey +func (s *SSLKeys) Create(key string) (*entity.SSLKey, error) { + sslKey := new(entity.SSLKey) + qsp := url.Values{} + qsp.Set("key", key) + err := s.client().Post("", qsp, func(data []byte) error { + return json.Unmarshal(data, sslKey) + }) + + return sslKey, err +} diff --git a/entity/ssl_key.go b/entity/ssl_key.go new file mode 100644 index 0000000..f901e54 --- /dev/null +++ b/entity/ssl_key.go @@ -0,0 +1,7 @@ +package entity + +type SSLKey struct { + ID int `json:"id,omitempty"` + Key string `json:"key,omitempty"` + ResourceURI string `json:"resource_uri,omitempty"` +}