Skip to content

Commit

Permalink
Add SSL Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jan 13, 2024
1 parent c43300e commit c10bfd3
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 1 deletion.
11 changes: 11 additions & 0 deletions api/ssl_key.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions api/ssl_keys.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions client/spaces.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:dupl // disable dupl check on client for now
package client

import (
Expand Down
33 changes: 33 additions & 0 deletions client/ssl_key.go
Original file line number Diff line number Diff line change
@@ -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()
}
40 changes: 40 additions & 0 deletions client/ssl_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//nolint:dupl // disable dupl check on client for now
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
}
2 changes: 1 addition & 1 deletion entity/ssh_key.go
Original file line number Diff line number Diff line change
@@ -1,8 +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"`
ID int `json:"id,omitempty"`
}
7 changes: 7 additions & 0 deletions entity/ssl_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package entity

type SSLKey struct {
Key string `json:"key,omitempty"`
ResourceURI string `json:"resource_uri,omitempty"`
ID int `json:"id,omitempty"`
}

0 comments on commit c10bfd3

Please sign in to comment.