Ash Wilson | 0482daf | 2014-10-22 11:24:40 -0400 | [diff] [blame] | 1 | package keypairs |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
| 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. |
| 11 | type 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. |
| 32 | type KeyPairPage struct { |
| 33 | pagination.SinglePageBase |
| 34 | } |
| 35 | |
| 36 | // IsEmpty determines whether or not a KeyPairPage is empty. |
| 37 | func (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. |
| 43 | func 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 | |
| 60 | type keyPairResult struct { |
| 61 | gophercloud.Result |
| 62 | } |
| 63 | |
| 64 | // Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct. |
| 65 | func (r keyPairResult) Extract() (*KeyPair, error) { |
| 66 | if r.Err != nil { |
| 67 | return nil, r.Err |
| 68 | } |
| 69 | |
| 70 | var res struct { |
Ash Wilson | 4aaaa1e | 2014-10-22 14:38:02 -0400 | [diff] [blame] | 71 | KeyPair *KeyPair `json:"keypair" mapstructure:"keypair"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 72 | } |
| 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. |
| 80 | type 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. |
| 86 | type 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. |
| 92 | type DeleteResult struct { |
| 93 | gophercloud.Result |
| 94 | } |
Ash Wilson | 4176663 | 2014-10-22 15:07:58 -0400 | [diff] [blame^] | 95 | |
| 96 | // Extract determines whether or not a deletion request was accepted. |
| 97 | func (r DeleteResult) Extract() error { |
| 98 | return r.Err |
| 99 | } |