Debian Codegraph @

cloudapi/keys.go

raw symbols SCIP
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 // Key represent a public key
11 type Key struct {
12 Name string // Name for the key
13 Fingerprint string // Key Fingerprint
14 Key string // OpenSSH formatted public key
15 }
16
17 /*func (k Key) Equals(other Key) bool {
18 if k.Name == other.Name && k.Fingerprint == other.Fingerprint && k.Key == other.Key {
19 return true
20 }
21 return false
22 }*/
23
24 // CreateKeyOpts represent the option that can be specified
25 // when creating a new key.
26 type CreateKeyOpts struct {
27 Name string `json:"name"` // Name for the key, optional
28 Key string `json:"key"` // OpenSSH formatted public key
29 }
30
31 // ListKeys returns a list of public keys registered with a specific account.
32 // See API docs: http://apidocs.joyent.com/cloudapi/#ListKeys
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 // GetKey returns the key identified by keyName.
47 // See API docs: http://apidocs.joyent.com/cloudapi/#GetKey
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 // CreateKey creates a new key with the specified options.
62 // See API docs: http://apidocs.joyent.com/cloudapi/#CreateKey
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 // DeleteKey deletes the key identified by keyName.
79 // See API docs: http://apidocs.joyent.com/cloudapi/#DeleteKey
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 }