forked from latitudesh/latitudesh-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_keys.go
161 lines (129 loc) · 4.42 KB
/
ssh_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package latitude
import (
"path"
)
const sshKeyBasePath = "/ssh_keys"
type SSHKeyService interface {
List(projectID string, opts *ListOptions) ([]SSHKeyData, *Response, error)
Get(sshKeyID string, projectID string, opts *GetOptions) (*SSHKeyGetResponse, *Response, error)
Create(projectID string, request *SSHKeyCreateRequest) (*SSHKey, *Response, error)
Update(sshKeyID string, projectID string, request *SSHKeyUpdateRequest) (*SSHKey, *Response, error)
Delete(sshKeyID string, projectID string) (*Response, error)
}
// SSHKey represents a Latitude SSH key
type SSHKey struct {
Data SSHKeyData `json:"data"`
Meta meta `json:"meta"`
}
type SSHKeyData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes SSHKeyAttributes `json:"attributes"`
}
type SSHKeyAttributes struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
Fingerprint string `json:"fingerprint"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type SSHKeyListResponse struct {
SSHKeys []SSHKeyData `json:"data"`
Meta meta `json:"meta"`
}
type SSHKeyGetResponse struct {
Data SSHKeyGetData `json:"data"`
Meta meta `json:"meta"`
}
type SSHKeyGetData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes SSHKeyAttributes `json:"attributes"`
}
// SSHKeyCreateRequest type used to create a Latitude SSH key
type SSHKeyCreateRequest struct {
Data SSHKeyCreateData `json:"data"`
}
func (s SSHKeyCreateRequest) String() string {
return Stringify(s)
}
type SSHKeyCreateData struct {
Type string `json:"type"`
Attributes SSHKeyCreateAttributes `json:"attributes"`
}
type SSHKeyCreateAttributes struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
}
// ProjectUpdateRequest type used to update a Latitude project
type SSHKeyUpdateRequest struct {
Data SSHKeyUpdateData `json:"data"`
}
type SSHKeyUpdateData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes SSHKeyUpdateAttributes `json:"attributes"`
}
type SSHKeyUpdateAttributes struct {
Name string `json:"name"`
}
func (p SSHKeyUpdateRequest) String() string {
return Stringify(p)
}
// SSHKeyServiceOp implements SSHKeyService
type SSHKeyServiceOp struct {
client requestDoer
}
// List returns a list of SSH Keys
func (s *SSHKeyServiceOp) List(projectID string, opts *ListOptions) (sshKeys []SSHKeyData, resp *Response, err error) {
endpointPath := path.Join(projectBasePath, projectID, sshKeyBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(SSHKeyListResponse)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
sshKeys = append(sshKeys, subset.SSHKeys...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
// Get returns an SSH key by id
func (s *SSHKeyServiceOp) Get(sshKeyID string, projectID string, opts *GetOptions) (*SSHKeyGetResponse, *Response, error) {
endpointPath := path.Join(projectBasePath, projectID, sshKeyBasePath, sshKeyID)
apiPathQuery := opts.WithQuery(endpointPath)
sshKey := new(SSHKeyGetResponse)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, sshKey)
if err != nil {
return nil, resp, err
}
return sshKey, resp, err
}
// Create creates a new SSH key
func (s *SSHKeyServiceOp) Create(projectID string, createRequest *SSHKeyCreateRequest) (*SSHKey, *Response, error) {
endpointPath := path.Join(projectBasePath, projectID, sshKeyBasePath)
sshKey := new(SSHKey)
resp, err := s.client.DoRequest("POST", endpointPath, createRequest, sshKey)
if err != nil {
return nil, resp, err
}
return sshKey, resp, err
}
// Update updates an SSH key
func (s *SSHKeyServiceOp) Update(sshKeyID string, projectID string, updateRequest *SSHKeyUpdateRequest) (*SSHKey, *Response, error) {
apiPath := path.Join(projectBasePath, projectID, sshKeyBasePath, sshKeyID)
sshKey := new(SSHKey)
resp, err := s.client.DoRequest("PATCH", apiPath, updateRequest, sshKey)
if err != nil {
return nil, resp, err
}
return sshKey, resp, err
}
// Delete deletes an SSH Key
func (s *SSHKeyServiceOp) Delete(sshKeyID string, projectID string) (*Response, error) {
apiPath := path.Join(projectBasePath, projectID, sshKeyBasePath, sshKeyID)
return s.client.DoRequest("DELETE", apiPath, nil, nil)
}