-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkeys.go
More file actions
62 lines (50 loc) · 1.62 KB
/
keys.go
File metadata and controls
62 lines (50 loc) · 1.62 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
// Package keys provides methods for managing a user's ssh keys.
package keys
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List lists a user's ssh keys.
func List(c *drycc.Client, results int) (api.Keys, int, error) {
body, count, reqErr := c.LimitedRequest("/v2/keys/", results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Key{}, -1, reqErr
}
var keys []api.Key
if err := json.Unmarshal([]byte(body), &keys); err != nil {
return []api.Key{}, -1, err
}
return keys, count, reqErr
}
// New adds a new ssh key for the user. This is used for authenting with the git
// remote for the builder. This key must be unique to the current user, or the error
// drycc.ErrDuplicateKey will be returned.
func New(c *drycc.Client, id string, pubKey string) (api.Key, error) {
req := api.KeyCreateRequest{ID: id, Public: pubKey}
body, err := json.Marshal(req)
if err != nil {
return api.Key{}, err
}
res, reqErr := c.Request("POST", "/v2/keys/", body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Key{}, reqErr
}
defer res.Body.Close()
key := api.Key{}
if err = json.NewDecoder(res.Body).Decode(&key); err != nil {
return api.Key{}, err
}
return key, reqErr
}
// Delete removes a user's ssh key. The key ID will be the key comment, usually the email or user@hostname
// of the user. The exact keyID can be retrieved with List()
func Delete(c *drycc.Client, keyID string) error {
u := fmt.Sprintf("/v2/keys/%s", keyID)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}