| 1 |
package cloudapi |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
|
| 6 |
"github.com/joyent/gocommon/client" |
| 7 |
"github.com/joyent/gocommon/errors" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Key struct { |
| 12 |
Name string |
| 13 |
Fingerprint string |
| 14 |
Key string |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
type CreateKeyOpts struct { |
| 27 |
Name string `json:"name"` |
| 28 |
Key string `json:"key"` |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
func (c *Client) ListKeys() ([]Key, error) { |
| 34 |
var resp []Key |
| 35 |
req := request{ |
| 36 |
method: client.GET, |
| 37 |
url: apiKeys, |
| 38 |
resp: &resp, |
| 39 |
} |
| 40 |
if _, err := c.sendRequest(req); err != nil { |
| 41 |
return nil, errors.Newf(err, "failed to get list of keys") |
| 42 |
} |
| 43 |
return resp, nil |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func (c *Client) GetKey(keyName string) (*Key, error) { |
| 49 |
var resp Key |
| 50 |
req := request{ |
| 51 |
method: client.GET, |
| 52 |
url: makeURL(apiKeys, keyName), |
| 53 |
resp: &resp, |
| 54 |
} |
| 55 |
if _, err := c.sendRequest(req); err != nil { |
| 56 |
return nil, errors.Newf(err, "failed to get key with name: %s", keyName) |
| 57 |
} |
| 58 |
return &resp, nil |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func (c *Client) CreateKey(opts CreateKeyOpts) (*Key, error) { |
| 64 |
var resp Key |
| 65 |
req := request{ |
| 66 |
method: client.POST, |
| 67 |
url: apiKeys, |
| 68 |
reqValue: opts, |
| 69 |
resp: &resp, |
| 70 |
expectedStatus: http.StatusCreated, |
| 71 |
} |
| 72 |
if _, err := c.sendRequest(req); err != nil { |
| 73 |
return nil, errors.Newf(err, "failed to create key with name: %s", opts.Name) |
| 74 |
} |
| 75 |
return &resp, nil |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
func (c *Client) DeleteKey(keyName string) error { |
| 81 |
req := request{ |
| 82 |
method: client.DELETE, |
| 83 |
url: makeURL(apiKeys, keyName), |
| 84 |
expectedStatus: http.StatusNoContent, |
| 85 |
} |
| 86 |
if _, err := c.sendRequest(req); err != nil { |
| 87 |
return errors.Newf(err, "failed to delete key with name: %s", keyName) |
| 88 |
} |
| 89 |
return nil |
| 90 |
} |