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 ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // KeyPair is an SSH key known to the OpenStack cluster that is available to be injected into |
| 9 | // servers. |
| 10 | type KeyPair struct { |
| 11 | // Name is used to refer to this keypair from other services within this region. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 12 | Name string `json:"name"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 13 | |
| 14 | // Fingerprint is a short sequence of bytes that can be used to authenticate or validate a longer |
| 15 | // public key. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 16 | Fingerprint string `json:"fingerprint"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 17 | |
| 18 | // PublicKey is the public key from this pair, in OpenSSH format. "ssh-rsa AAAAB3Nz..." |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 19 | PublicKey string `json:"public_key"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 20 | |
| 21 | // PrivateKey is the private key from this pair, in PEM format. |
| 22 | // "-----BEGIN RSA PRIVATE KEY-----\nMIICXA..." It is only present if this keypair was just |
| 23 | // returned from a Create call |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 24 | PrivateKey string `json:"private_key"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 25 | |
| 26 | // UserID is the user who owns this keypair. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 27 | UserID string `json:"user_id"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // KeyPairPage stores a single, only page of KeyPair results from a List call. |
| 31 | type KeyPairPage struct { |
| 32 | pagination.SinglePageBase |
| 33 | } |
| 34 | |
| 35 | // IsEmpty determines whether or not a KeyPairPage is empty. |
| 36 | func (page KeyPairPage) IsEmpty() (bool, error) { |
| 37 | ks, err := ExtractKeyPairs(page) |
| 38 | return len(ks) == 0, err |
| 39 | } |
| 40 | |
| 41 | // ExtractKeyPairs interprets a page of results as a slice of KeyPairs. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 42 | func ExtractKeyPairs(r pagination.Page) ([]KeyPair, error) { |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 43 | type pair struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 44 | KeyPair KeyPair `json:"keypair"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 45 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 46 | var s struct { |
| 47 | KeyPairs []pair `json:"keypairs"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 48 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 49 | err := (r.(KeyPairPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 50 | results := make([]KeyPair, len(s.KeyPairs)) |
| 51 | for i, pair := range s.KeyPairs { |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 52 | results[i] = pair.KeyPair |
| 53 | } |
| 54 | return results, err |
| 55 | } |
| 56 | |
| 57 | type keyPairResult struct { |
| 58 | gophercloud.Result |
| 59 | } |
| 60 | |
| 61 | // Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct. |
| 62 | func (r keyPairResult) Extract() (*KeyPair, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 63 | var s struct { |
| 64 | KeyPair *KeyPair `json:"keypair"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 65 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 66 | err := r.ExtractInto(&s) |
| 67 | return s.KeyPair, err |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 71 | // as a KeyPair. |
| 72 | type CreateResult struct { |
| 73 | keyPairResult |
| 74 | } |
| 75 | |
| 76 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 77 | // as a KeyPair. |
| 78 | type GetResult struct { |
| 79 | keyPairResult |
| 80 | } |
| 81 | |
| 82 | // DeleteResult is the response from a Delete operation. Call its Extract method to determine if |
| 83 | // the call succeeded or failed. |
| 84 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 85 | gophercloud.ErrResult |
Ash Wilson | 4176663 | 2014-10-22 15:07:58 -0400 | [diff] [blame] | 86 | } |