forked from Scalingo/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
73 lines (63 loc) · 1.26 KB
/
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
package api
import "github.com/Scalingo/cli/Godeps/_workspace/src/gopkg.in/errgo.v1"
type Key struct {
ID string `json:"id"`
Name string `json:"name"`
Content string `json:"content"`
}
type KeyIndex struct {
Keys []Key `json:"keys"`
}
func KeysList() ([]Key, error) {
req := &APIRequest{
Endpoint: "/account/keys",
}
res, err := req.Do()
if err != nil {
return nil, errgo.Mask(err)
}
defer res.Body.Close()
var ki KeyIndex
err = ParseJSON(res, &ki)
if err != nil {
return nil, errgo.Mask(err)
}
return ki.Keys, nil
}
func KeysAdd(name string, content string) (*Key, error) {
req := &APIRequest{
Method: "POST",
Endpoint: "/account/keys",
Params: map[string]interface{}{
"key": map[string]interface{}{
"name": name,
"content": content,
},
},
Expected: Statuses{201},
}
res, err := req.Do()
if err != nil {
return nil, errgo.Mask(err)
}
defer res.Body.Close()
var key *Key
err = ParseJSON(res, &key)
if err != nil {
return nil, errgo.Mask(err)
}
return key, nil
}
func KeysDelete(id string) error {
req := &APIRequest{
Method: "DELETE",
Endpoint: "/account/keys/" + id,
Expected: Statuses{204},
}
res, err := req.Do()
if err != nil {
return errgo.Mask(err)
}
res.Body.Close()
return nil
}