forked from Scalingo/go-scalingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
57 lines (46 loc) · 1.04 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
package scalingo
import "gopkg.in/errgo.v1"
type KeysService interface {
KeysList() ([]Key, error)
KeysAdd(name string, content string) (*Key, error)
KeysDelete(id string) error
}
var _ KeysService = (*Client)(nil)
type Key struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Content string `json:"content"`
}
type KeyRes struct {
Key Key `json:"key"`
}
type KeysRes struct {
Keys []Key `json:"keys"`
}
func (c *Client) KeysList() ([]Key, error) {
var res KeysRes
err := c.AuthAPI().ResourceList("keys", nil, &res)
if err != nil {
return nil, errgo.Mask(err)
}
return res.Keys, nil
}
func (c *Client) KeysAdd(name string, content string) (*Key, error) {
payload := KeyRes{Key{
Name: name,
Content: content,
}}
var res KeyRes
err := c.AuthAPI().ResourceAdd("keys", payload, &res)
if err != nil {
return nil, errgo.Mask(err)
}
return &res.Key, nil
}
func (c *Client) KeysDelete(id string) error {
err := c.AuthAPI().ResourceDelete("keys", id)
if err != nil {
return errgo.Mask(err)
}
return nil
}