This repository was archived by the owner on Jun 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathssh_key.go
More file actions
75 lines (61 loc) · 1.9 KB
/
ssh_key.go
File metadata and controls
75 lines (61 loc) · 1.9 KB
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
package hetzner
import (
"fmt"
"net/http"
)
// See: https://wiki.hetzner.de/index.php/Robot_Webservice/en#SSH_keys
type SSHKeyService interface {
List() ([]*SSHKey, *http.Response, error)
Create(req *SSHKeyCreateRequest) (*SSHKey, *http.Response, error)
Get(fingerprint string) (*SSHKey, *http.Response, error)
Update(req *SSHKeyUpdateRequest) (*SSHKey, *http.Response, error)
Delete(fingerprint string) (*http.Response, error)
}
type SSHKeyServiceImpl struct {
client *Client
}
var _ SSHKeyService = &SSHKeyServiceImpl{}
func (s *SSHKeyServiceImpl) List() ([]*SSHKey, *http.Response, error) {
path := "/key"
type Data struct {
Key *SSHKey `json:"key"`
}
data := make([]Data, 0)
resp, err := s.client.Call(http.MethodGet, path, nil, &data, true)
a := make([]*SSHKey, len(data))
for i, d := range data {
a[i] = d.Key
}
return a, resp, err
}
func (s *SSHKeyServiceImpl) Create(req *SSHKeyCreateRequest) (*SSHKey, *http.Response, error) {
path := "/key"
type Data struct {
Key *SSHKey `json:"key"`
}
data := Data{}
resp, err := s.client.Call(http.MethodPost, path, req, &data, true)
return data.Key, resp, err
}
func (s *SSHKeyServiceImpl) Get(fingerprint string) (*SSHKey, *http.Response, error) {
path := fmt.Sprintf("/key/%v", fingerprint)
type Data struct {
Key *SSHKey `json:"key"`
}
data := Data{}
resp, err := s.client.Call(http.MethodGet, path, nil, &data, true)
return data.Key, resp, err
}
func (s *SSHKeyServiceImpl) Update(req *SSHKeyUpdateRequest) (*SSHKey, *http.Response, error) {
path := fmt.Sprintf("/key/%v", req.Fingerprint)
type Data struct {
Key *SSHKey `json:"key"`
}
data := Data{}
resp, err := s.client.Call(http.MethodPost, path, req, &data, true)
return data.Key, resp, err
}
func (s *SSHKeyServiceImpl) Delete(fingerprint string) (*http.Response, error) {
path := fmt.Sprintf("/key/%v", fingerprint)
return s.client.Call(http.MethodDelete, path, nil, nil, true)
}