blob: 96b96eab35fe4b96c480f8fc0b999e22d223007d [file] [log] [blame]
Ash Wilson0482daf2014-10-22 11:24:40 -04001package keypairs
Ash Wilsonad612f62014-10-22 14:04:37 -04002
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9// KeyPair is an SSH key known to the OpenStack cluster that is available to be injected into
10// servers.
11type KeyPair struct {
12 // Name is used to refer to this keypair from other services within this region.
13 Name string `mapstructure:"name"`
14
15 // Fingerprint is a short sequence of bytes that can be used to authenticate or validate a longer
16 // public key.
17 Fingerprint string `mapstructure:"fingerprint"`
18
19 // PublicKey is the public key from this pair, in OpenSSH format. "ssh-rsa AAAAB3Nz..."
20 PublicKey string `mapstructure:"public_key"`
21
22 // PrivateKey is the private key from this pair, in PEM format.
23 // "-----BEGIN RSA PRIVATE KEY-----\nMIICXA..." It is only present if this keypair was just
24 // returned from a Create call
25 PrivateKey string `mapstructure:"private_key"`
26
27 // UserID is the user who owns this keypair.
28 UserID string `mapstructure:"user_id"`
29}
30
31// KeyPairPage stores a single, only page of KeyPair results from a List call.
32type KeyPairPage struct {
33 pagination.SinglePageBase
34}
35
36// IsEmpty determines whether or not a KeyPairPage is empty.
37func (page KeyPairPage) IsEmpty() (bool, error) {
38 ks, err := ExtractKeyPairs(page)
39 return len(ks) == 0, err
40}
41
42// ExtractKeyPairs interprets a page of results as a slice of KeyPairs.
43func ExtractKeyPairs(page pagination.Page) ([]KeyPair, error) {
44 type pair struct {
45 KeyPair KeyPair `mapstructure:"keypair"`
46 }
47
48 var resp struct {
49 KeyPairs []pair `mapstructure:"keypairs"`
50 }
51
52 err := mapstructure.Decode(page.(KeyPairPage).Body, &resp)
53 results := make([]KeyPair, len(resp.KeyPairs))
54 for i, pair := range resp.KeyPairs {
55 results[i] = pair.KeyPair
56 }
57 return results, err
58}
59
60type keyPairResult struct {
61 gophercloud.Result
62}
63
64// Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct.
65func (r keyPairResult) Extract() (*KeyPair, error) {
66 if r.Err != nil {
67 return nil, r.Err
68 }
69
70 var res struct {
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040071 KeyPair *KeyPair `json:"keypair" mapstructure:"keypair"`
Ash Wilsonad612f62014-10-22 14:04:37 -040072 }
73
74 err := mapstructure.Decode(r.Body, &res)
75 return res.KeyPair, err
76}
77
78// CreateResult is the response from a Create operation. Call its Extract method to interpret it
79// as a KeyPair.
80type CreateResult struct {
81 keyPairResult
82}
83
84// GetResult is the response from a Get operation. Call its Extract method to interpret it
85// as a KeyPair.
86type GetResult struct {
87 keyPairResult
88}
89
90// DeleteResult is the response from a Delete operation. Call its Extract method to determine if
91// the call succeeded or failed.
92type DeleteResult struct {
93 gophercloud.Result
94}
Ash Wilson41766632014-10-22 15:07:58 -040095
96// Extract determines whether or not a deletion request was accepted.
97func (r DeleteResult) Extract() error {
98 return r.Err
99}