Skip to content

Commit

Permalink
Add SSH Key(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jan 12, 2024
1 parent f9e15e9 commit c43300e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/ssh_key.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions api/ssh_keys.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions client/ssh_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"
)

// 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()
}
51 changes: 51 additions & 0 deletions client/ssh_keys.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions entity/ssh_key.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit c43300e

Please sign in to comment.