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. |
| 42 | func ExtractKeyPairs(page pagination.Page) ([]KeyPair, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 43 | r := page.(KeyPairPage) |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 44 | type pair struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 45 | KeyPair KeyPair `json:"keypair"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 46 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 47 | var s struct { |
| 48 | KeyPairs []pair `json:"keypairs"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 49 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 50 | err := r.ExtractInto(&s) |
| 51 | results := make([]KeyPair, len(s.KeyPairs)) |
| 52 | for i, pair := range s.KeyPairs { |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 53 | results[i] = pair.KeyPair |
| 54 | } |
| 55 | return results, err |
| 56 | } |
| 57 | |
| 58 | type keyPairResult struct { |
| 59 | gophercloud.Result |
| 60 | } |
| 61 | |
| 62 | // Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct. |
| 63 | func (r keyPairResult) Extract() (*KeyPair, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 64 | var s struct { |
| 65 | KeyPair *KeyPair `json:"keypair"` |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 66 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 67 | err := r.ExtractInto(&s) |
| 68 | return s.KeyPair, err |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 72 | // as a KeyPair. |
| 73 | type CreateResult struct { |
| 74 | keyPairResult |
| 75 | } |
| 76 | |
| 77 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 78 | // as a KeyPair. |
| 79 | type GetResult struct { |
| 80 | keyPairResult |
| 81 | } |
| 82 | |
| 83 | // DeleteResult is the response from a Delete operation. Call its Extract method to determine if |
| 84 | // the call succeeded or failed. |
| 85 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 86 | gophercloud.ErrResult |
Ash Wilson | 4176663 | 2014-10-22 15:07:58 -0400 | [diff] [blame] | 87 | } |