-
Notifications
You must be signed in to change notification settings - Fork 7
/
sshkey.go
184 lines (159 loc) · 5.63 KB
/
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
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
package gsclient
import (
"context"
"errors"
"net/http"
"path"
)
// SSHKeyOperator provides an interface for operations on SSH keys.
type SSHKeyOperator interface {
GetSshkey(ctx context.Context, id string) (Sshkey, error)
GetSshkeyList(ctx context.Context) ([]Sshkey, error)
CreateSshkey(ctx context.Context, body SshkeyCreateRequest) (CreateResponse, error)
DeleteSshkey(ctx context.Context, id string) error
UpdateSshkey(ctx context.Context, id string, body SshkeyUpdateRequest) error
GetSshkeyEventList(ctx context.Context, id string) ([]Event, error)
}
// SshkeyList holds a list of SSH keys.
type SshkeyList struct {
// Array of SSH keys.
List map[string]SshkeyProperties `json:"sshkeys"`
}
// Sshkey represents a single SSH key.
type Sshkey struct {
// Properties of a SSH key.
Properties SshkeyProperties `json:"sshkey"`
}
// SshkeyProperties holds properties of a single SSH key.
// A SSH key can be retrieved when creating new storages and attaching them to
// servers.
type SshkeyProperties struct {
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
Name string `json:"name"`
// The UUID of an object is always unique, and refers to a specific object.
ObjectUUID string `json:"object_uuid"`
// Status indicates the status of the object.
Status string `json:"status"`
// Defines the date and time the object was initially created.
CreateTime GSTime `json:"create_time"`
// Defines the date and time of the last object change.
ChangeTime GSTime `json:"change_time"`
// The OpenSSH public key string (all key types are supported => ed25519, ecdsa, dsa, rsa, rsa1).
Sshkey string `json:"sshkey"`
// List of labels.
Labels []string `json:"labels"`
// The User-UUID of the account which created this SSH Key.
UserUUID string `json:"user_uuid"`
}
// SshkeyCreateRequest represents a request for creating a SSH key.
type SshkeyCreateRequest struct {
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
Name string `json:"name"`
// The OpenSSH public key string (all key types are supported => ed25519, ecdsa, dsa, rsa, rsa1).
Sshkey string `json:"sshkey"`
// List of labels. Optional.
Labels []string `json:"labels,omitempty"`
}
// SshkeyUpdateRequest represents a request for updating a SSH key.
type SshkeyUpdateRequest struct {
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
// Optional.
Name string `json:"name,omitempty"`
// The OpenSSH public key string (all key types are supported => ed25519, ecdsa, dsa, rsa, rsa1). Optional.
Sshkey string `json:"sshkey,omitempty"`
// List of labels. Optional.
Labels *[]string `json:"labels,omitempty"`
}
// GetSshkey gets a single SSH key object.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getSshKey
func (c *Client) GetSshkey(ctx context.Context, id string) (Sshkey, error) {
if !isValidUUID(id) {
return Sshkey{}, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiSshkeyBase, id),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response Sshkey
err := r.execute(ctx, *c, &response)
return response, err
}
// GetSshkeyList gets the list of SSH keys in the project.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getSshKeys
func (c *Client) GetSshkeyList(ctx context.Context) ([]Sshkey, error) {
r := gsRequest{
uri: apiSshkeyBase,
method: http.MethodGet,
skipCheckingRequest: true,
}
var response SshkeyList
var sshKeys []Sshkey
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
sshKeys = append(sshKeys, Sshkey{Properties: properties})
}
return sshKeys, err
}
// CreateSshkey creates a new SSH key.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/createSshKey
func (c *Client) CreateSshkey(ctx context.Context, body SshkeyCreateRequest) (CreateResponse, error) {
r := gsRequest{
uri: apiSshkeyBase,
method: "POST",
body: body,
}
var response CreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
// DeleteSshkey removes a single SSH key.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/deleteSshKey
func (c *Client) DeleteSshkey(ctx context.Context, id string) error {
if !isValidUUID(id) {
return errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiSshkeyBase, id),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}
// UpdateSshkey updates a SSH key.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/updateSshKey
func (c *Client) UpdateSshkey(ctx context.Context, id string, body SshkeyUpdateRequest) error {
if !isValidUUID(id) {
return errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiSshkeyBase, id),
method: http.MethodPatch,
body: body,
}
return r.execute(ctx, *c, nil)
}
// GetSshkeyEventList gets a SSH key's events.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getSshKeyEvents
func (c *Client) GetSshkeyEventList(ctx context.Context, id string) ([]Event, error) {
if !isValidUUID(id) {
return nil, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiSshkeyBase, id, "events"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response EventList
var sshEvents []Event
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
sshEvents = append(sshEvents, Event{Properties: properties})
}
return sshEvents, err
}