-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypairs.go
327 lines (257 loc) · 8.58 KB
/
keypairs.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package edgecloud
import (
"context"
"fmt"
"net/http"
)
const (
keypairsBasePathV1 = "/v1/keypairs"
keypairsBasePathV2 = "/v2/keypairs"
)
const (
keypairsShare = "share"
)
// KeyPairsService is an interface for creating and managing SSH keys with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/keypairs
type KeyPairsService interface {
List(context.Context) ([]KeyPair, *Response, error)
ListV2(context.Context) ([]KeyPairV2, *Response, error)
Get(context.Context, string) (*KeyPair, *Response, error)
GetV2(context.Context, string) (*KeyPairV2, *Response, error)
Create(context.Context, *KeyPairCreateRequest) (*TaskResponse, *Response, error)
CreateV2(context.Context, *KeyPairCreateRequestV2) (*KeyPairV2, *Response, error)
Delete(context.Context, string) (*TaskResponse, *Response, error)
DeleteV2(context.Context, string) (*Response, error)
Share(context.Context, string, *KeyPairShareRequest) (*KeyPair, *Response, error)
}
// KeyPairsServiceOp handles communication with Key Pairs (SSH keys) methods of the EdgecenterCloud API.
type KeyPairsServiceOp struct {
client *Client
}
var _ KeyPairsService = &KeyPairsServiceOp{}
// KeyPair represents an EdgecenterCloud Key Pair.
type KeyPair struct {
SSHKeyID string `json:"sshkey_id"`
PublicKey string `json:"public_key"`
PrivateKey string `json:"private_key"`
Fingerprint string `json:"fingerprint"`
SSHKeyName string `json:"sshkey_name"`
State string `json:"state"`
SharedInProject bool `json:"shared_in_project"`
CreatedAt string `json:"created_at"`
ProjectID int `json:"project_id"`
}
// KeyPairV2 represents an EdgecenterCloud Key Pair.
type KeyPairV2 struct {
Fingerprint string `json:"fingerprint"`
PublicKey string `json:"public_key"`
SSHKeyID string `json:"sshkey_id"`
SSHKeyName string `json:"sshkey_name"`
SharedInProject bool `json:"shared_in_project"`
}
// KeyPairCreateRequest represents a request to create a Key Pair.
type KeyPairCreateRequest struct {
SSHKeyName string `json:"sshkey_name" required:"true"`
PublicKey string `json:"public_key"`
SharedInProject bool `json:"shared_in_project"`
}
// KeyPairCreateRequestV2 represents a request to create a Key Pair.
type KeyPairCreateRequestV2 struct {
ProjectID int `json:"project_id" required:"true"`
SSHKeyName string `json:"sshkey_name" required:"true"`
PublicKey string `json:"public_key,omitempty"`
SharedInProject bool `json:"shared_in_project,omitempty"`
}
// KeyPairShareRequest represents a request to share a Key Pair.
type KeyPairShareRequest struct {
SharedInProject bool `json:"shared_in_project" required:"true"`
}
// keyPairsRoot represents a KeyPair root.
type keyPairsRoot struct {
Count int
KeyPair []KeyPair `json:"results"`
}
// keyPairsRoot represents a KeyPair root.
type keyPairsRootV2 struct {
Count int
KeyPair []KeyPairV2 `json:"results"`
}
// KeyPairsListOptionsV2 specifies the optional query parameters to List method.
type KeyPairsListOptionsV2 struct {
ProjectID int `url:"project_id,omitempty" validate:"omitempty"`
UserID int `url:"user_id,omitempty" validate:"omitempty"`
}
// List get KeyPairs.
func (s *KeyPairsServiceOp) List(ctx context.Context) ([]KeyPair, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(keypairsBasePathV1)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(keyPairsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.KeyPair, resp, err
}
// ListV2 get KeyPairs.
func (s *KeyPairsServiceOp) ListV2(ctx context.Context) ([]KeyPairV2, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
opts := KeyPairsListOptionsV2{ProjectID: s.client.Project}
path, err := addOptions(keypairsBasePathV2, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(keyPairsRootV2)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.KeyPair, resp, err
}
// Get individual Key Pair.
func (s *KeyPairsServiceOp) Get(ctx context.Context, keypairID string) (*KeyPair, *Response, error) {
if resp, err := isValidUUID(keypairID, "keypairID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s", s.client.addProjectRegionPath(keypairsBasePathV1), keypairID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
keyPair := new(KeyPair)
resp, err := s.client.Do(ctx, req, keyPair)
if err != nil {
return nil, resp, err
}
return keyPair, resp, err
}
// GetV2 individual Key Pair.
func (s *KeyPairsServiceOp) GetV2(ctx context.Context, keypairID string) (*KeyPairV2, *Response, error) {
if resp, err := isValidUUID(keypairID, "keypairID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s", keypairsBasePathV2, keypairID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
keyPair := new(KeyPairV2)
resp, err := s.client.Do(ctx, req, keyPair)
if err != nil {
return nil, resp, err
}
return keyPair, resp, err
}
// Create a Key Pair.
func (s *KeyPairsServiceOp) Create(ctx context.Context, reqBody *KeyPairCreateRequest) (*TaskResponse, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(keypairsBasePathV1)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
func (s *KeyPairsServiceOp) CreateV2(ctx context.Context, reqBody *KeyPairCreateRequestV2) (*KeyPairV2, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
req, err := s.client.NewRequest(ctx, http.MethodPost, keypairsBasePathV2, reqBody)
if err != nil {
return nil, nil, err
}
keyPair := new(KeyPairV2)
resp, err := s.client.Do(ctx, req, keyPair)
if err != nil {
return nil, resp, err
}
return keyPair, resp, err
}
// Delete the Key Pair.
func (s *KeyPairsServiceOp) Delete(ctx context.Context, keypairID string) (*TaskResponse, *Response, error) {
if resp, err := isValidUUID(keypairID, "keypairID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s", s.client.addProjectRegionPath(keypairsBasePathV1), keypairID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
// DeleteV2 the Key Pair.
func (s *KeyPairsServiceOp) DeleteV2(ctx context.Context, keypairID string) (*Response, error) {
if resp, err := isValidUUID(keypairID, "keypairID"); err != nil {
return resp, err
}
if resp, err := s.client.Validate(); err != nil {
return resp, err
}
path := fmt.Sprintf("%s/%s", keypairsBasePathV2, keypairID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Share a Key Pair to view for all users in project.
func (s *KeyPairsServiceOp) Share(ctx context.Context, keypairID string, reqBody *KeyPairShareRequest) (*KeyPair, *Response, error) {
if resp, err := isValidUUID(keypairID, "keypairID"); err != nil {
return nil, resp, err
}
if reqBody == nil {
return nil, nil, NewArgError("shareRequest", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s/%s", s.client.addProjectRegionPath(keypairsBasePathV1), keypairID, keypairsShare)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, reqBody)
if err != nil {
return nil, nil, err
}
keyPair := new(KeyPair)
resp, err := s.client.Do(ctx, req, keyPair)
if err != nil {
return nil, resp, err
}
return keyPair, resp, err
}