-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublickey.go
57 lines (47 loc) · 1.01 KB
/
publickey.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
package lolp
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
type PublicKey struct {
Name string `json:"name"`
Key string `json:"key"`
}
// AddPublicKey add OpenSSH public key
func (c *Client) AddPublicKey(p *PublicKey) (*PublicKey, error) {
if len(p.Name) == 0 {
return nil, fmt.Errorf("client: missing name")
}
if len(p.Key) == 0 {
return nil, fmt.Errorf("client: missing key")
}
body, err := json.Marshal(p)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] request body: %s", body)
res, err := c.HTTP("POST", "/v1/pubkeys", &RequestOptions{
Body: bytes.NewReader(body),
})
if err != nil {
return nil, err
}
var pubKey PublicKey
if err := decodeJSON(res, &pubKey); err != nil {
return nil, err
}
return &pubKey, nil
}
// DeletePublicKey delete OpenSSH public key
func (c *Client) DeletePublicKey(name string) error {
if len(name) == 0 {
return fmt.Errorf("client: missing name")
}
_, err := c.HTTP("DELETE", "/v1/pubkeys/"+name, nil)
if err != nil {
return err
}
return nil
}