-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9e15e9
commit c43300e
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |