forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
145 lines (118 loc) · 3.49 KB
/
keys.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package godo
import "fmt"
const keysBasePath = "v2/account/keys"
// KeysService is an interface for interfacing with the keys
// endpoints of the Digital Ocean API
// See: https://developers.digitalocean.com/documentation/v2#keys
type KeysService interface {
List(*ListOptions) ([]Key, *Response, error)
GetByID(int) (*Key, *Response, error)
GetByFingerprint(string) (*Key, *Response, error)
Create(*KeyCreateRequest) (*Key, *Response, error)
DeleteByID(int) (*Response, error)
DeleteByFingerprint(string) (*Response, error)
}
// KeysServiceOp handles communication with key related method of the
// DigitalOcean API.
type KeysServiceOp struct {
client *Client
}
var _ KeysService = &KeysServiceOp{}
// Key represents a DigitalOcean Key.
type Key struct {
ID int `json:"id,float64,omitempty"`
Name string `json:"name,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
PublicKey string `json:"public_key,omitempty"`
}
type keysRoot struct {
SSHKeys []Key `json:"ssh_keys"`
Links *Links `json:"links"`
}
type keyRoot struct {
SSHKey Key `json:"ssh_key"`
}
func (s Key) String() string {
return Stringify(s)
}
// KeyCreateRequest represents a request to create a new key.
type KeyCreateRequest struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
}
// List all keys
func (s *KeysServiceOp) List(opt *ListOptions) ([]Key, *Response, error) {
path := keysBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
root := new(keysRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.SSHKeys, resp, err
}
// Performs a get given a path
func (s *KeysServiceOp) get(path string) (*Key, *Response, error) {
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
root := new(keyRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return &root.SSHKey, resp, err
}
// GetByID gets a Key by id
func (s *KeysServiceOp) GetByID(keyID int) (*Key, *Response, error) {
path := fmt.Sprintf("%s/%d", keysBasePath, keyID)
return s.get(path)
}
// GetByFingerprint gets a Key by by fingerprint
func (s *KeysServiceOp) GetByFingerprint(fingerprint string) (*Key, *Response, error) {
path := fmt.Sprintf("%s/%s", keysBasePath, fingerprint)
return s.get(path)
}
// Create a key using a KeyCreateRequest
func (s *KeysServiceOp) Create(createRequest *KeyCreateRequest) (*Key, *Response, error) {
req, err := s.client.NewRequest("POST", keysBasePath, createRequest)
if err != nil {
return nil, nil, err
}
root := new(keyRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return &root.SSHKey, resp, err
}
// Delete key using a path
func (s *KeysServiceOp) delete(path string) (*Response, error) {
req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
return resp, err
}
// DeleteByID deletes a key by its id
func (s *KeysServiceOp) DeleteByID(keyID int) (*Response, error) {
path := fmt.Sprintf("%s/%d", keysBasePath, keyID)
return s.delete(path)
}
// DeleteByFingerprint deletes a key by its fingerprint
func (s *KeysServiceOp) DeleteByFingerprint(fingerprint string) (*Response, error) {
path := fmt.Sprintf("%s/%s", keysBasePath, fingerprint)
return s.delete(path)
}