github.com/naphatkrit/deis@v1.12.3/client/controller/models/keys/keys.go (about) 1 package keys 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/deis/deis/client/controller/api" 8 "github.com/deis/deis/client/controller/client" 9 ) 10 11 // List keys on a controller. 12 func List(c *client.Client, results int) ([]api.Key, int, error) { 13 body, count, err := c.LimitedRequest("/v1/keys/", results) 14 15 if err != nil { 16 return []api.Key{}, -1, err 17 } 18 19 var keys []api.Key 20 if err = json.Unmarshal([]byte(body), &keys); err != nil { 21 return []api.Key{}, -1, err 22 } 23 24 return keys, count, nil 25 } 26 27 // New creates a new key. 28 func New(c *client.Client, id string, pubKey string) (api.Key, error) { 29 req := api.KeyCreateRequest{ID: id, Public: pubKey} 30 body, err := json.Marshal(req) 31 32 resBody, err := c.BasicRequest("POST", "/v1/keys/", body) 33 34 if err != nil { 35 return api.Key{}, err 36 } 37 38 key := api.Key{} 39 if err = json.Unmarshal([]byte(resBody), &key); err != nil { 40 return api.Key{}, err 41 } 42 43 return key, nil 44 } 45 46 // Delete a key. 47 func Delete(c *client.Client, keyID string) error { 48 u := fmt.Sprintf("/v1/keys/%s", keyID) 49 50 _, err := c.BasicRequest("DELETE", u, nil) 51 return err 52 }