forked from equinix/ne-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_sshkey.go
84 lines (75 loc) · 2.25 KB
/
rest_sshkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package ne
import (
"net/http"
"net/url"
"github.com/equinix/ne-go/internal/api"
)
// GetSSHPublicKeys retrieves list of available SSH public keys
func (c RestClient) GetSSHPublicKeys() ([]SSHPublicKey, error) {
path := "/ne/v1/publicKeys"
respBody := make([]api.SSHPublicKey, 0)
req := c.R().SetResult(&respBody)
if err := c.Execute(req, http.MethodGet, path); err != nil {
return nil, err
}
return mapSSHPublicKeysAPIToDomain(respBody), nil
}
// GetSSHPublicKey retrieves SSH public key with a given identifier
func (c RestClient) GetSSHPublicKey(uuid string) (*SSHPublicKey, error) {
path := "/ne/v1/publicKeys/" + url.PathEscape(uuid)
respBody := api.SSHPublicKey{}
req := c.R().SetResult(&respBody)
if err := c.Execute(req, http.MethodGet, path); err != nil {
return nil, err
}
mapped := mapSSHPublicKeyAPIToDomain(respBody)
return &mapped, nil
}
// CreateSSHPublicKey creates new SSH public key with a given details
func (c RestClient) CreateSSHPublicKey(key SSHPublicKey) (*string, error) {
path := "/ne/v1/publicKeys"
reqBody := mapSSHPublicKeyDomainToAPI(key)
req := c.R().SetBody(&reqBody)
resp, err := c.Do(http.MethodPost, path, req)
if err != nil {
return nil, err
}
uuid, err := getResourceIDFromLocationHeader(resp)
if err != nil {
return nil, err
}
return uuid, nil
}
// DeleteSSHPublicKey removes SSH Public key with given identifier
func (c RestClient) DeleteSSHPublicKey(uuid string) error {
path := "/ne/v1/publicKeys/" + url.PathEscape(uuid)
if err := c.Execute(c.R(), http.MethodDelete, path); err != nil {
return err
}
return nil
}
func mapSSHPublicKeysAPIToDomain(apiKeys []api.SSHPublicKey) []SSHPublicKey {
transformed := make([]SSHPublicKey, len(apiKeys))
for i := range apiKeys {
transformed[i] = mapSSHPublicKeyAPIToDomain(apiKeys[i])
}
return transformed
}
func mapSSHPublicKeyAPIToDomain(apiKey api.SSHPublicKey) SSHPublicKey {
return SSHPublicKey{
UUID: apiKey.UUID,
Name: apiKey.KeyName,
Value: apiKey.KeyValue,
Type: apiKey.KeyType,
ProjectID: apiKey.ProjectID,
}
}
func mapSSHPublicKeyDomainToAPI(key SSHPublicKey) api.SSHPublicKey {
return api.SSHPublicKey{
UUID: key.UUID,
KeyName: key.Name,
KeyValue: key.Value,
KeyType: key.Type,
ProjectID: key.ProjectID,
}
}