Mark Peek | 5a9151f | 2013-08-17 18:59:59 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | ) |
| 6 | |
| 7 | // See the CloudImagesProvider interface for details. |
| 8 | func (gsp *genericServersProvider) ListKeyPairs() ([]KeyPair, error) { |
| 9 | type KeyPairs struct { |
| 10 | KeyPairs []struct { |
| 11 | KeyPair KeyPair `json:"keypair"` |
| 12 | } `json:"keypairs"` |
| 13 | } |
| 14 | |
| 15 | var kp KeyPairs |
| 16 | |
| 17 | err := gsp.context.WithReauth(gsp.access, func() error { |
| 18 | url := gsp.endpoint + "/os-keypairs" |
| 19 | return perigee.Get(url, perigee.Options{ |
| 20 | CustomClient: gsp.context.httpClient, |
| 21 | Results: &kp, |
| 22 | MoreHeaders: map[string]string{ |
| 23 | "X-Auth-Token": gsp.access.AuthToken(), |
| 24 | }, |
| 25 | }) |
| 26 | }) |
| 27 | |
| 28 | // Flatten out the list of keypairs |
| 29 | var keypairs []KeyPair |
| 30 | for _, k := range kp.KeyPairs { |
| 31 | keypairs = append(keypairs, k.KeyPair) |
| 32 | } |
| 33 | return keypairs, err |
| 34 | } |
| 35 | |
| 36 | func (gsp *genericServersProvider) CreateKeyPair(nkp NewKeyPair) (KeyPair, error) { |
| 37 | var kp KeyPair |
| 38 | |
| 39 | err := gsp.context.WithReauth(gsp.access, func() error { |
| 40 | url := gsp.endpoint + "/os-keypairs" |
| 41 | return perigee.Post(url, perigee.Options{ |
| 42 | ReqBody: &struct { |
| 43 | KeyPair *NewKeyPair `json:"keypair"` |
| 44 | }{&nkp}, |
| 45 | CustomClient: gsp.context.httpClient, |
| 46 | Results: &struct{ KeyPair *KeyPair }{&kp}, |
| 47 | MoreHeaders: map[string]string{ |
| 48 | "X-Auth-Token": gsp.access.AuthToken(), |
| 49 | }, |
| 50 | }) |
| 51 | }) |
| 52 | return kp, err |
| 53 | } |
| 54 | |
| 55 | // See the CloudImagesProvider interface for details. |
| 56 | func (gsp *genericServersProvider) DeleteKeyPair(name string) error { |
| 57 | err := gsp.context.WithReauth(gsp.access, func() error { |
| 58 | url := gsp.endpoint + "/os-keypairs/" + name |
| 59 | return perigee.Delete(url, perigee.Options{ |
| 60 | CustomClient: gsp.context.httpClient, |
| 61 | MoreHeaders: map[string]string{ |
| 62 | "X-Auth-Token": gsp.access.AuthToken(), |
| 63 | }, |
| 64 | OkCodes: []int{202}, |
| 65 | }) |
| 66 | }) |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | func (gsp *genericServersProvider) ShowKeyPair(name string) (KeyPair, error) { |
| 71 | var kp KeyPair |
| 72 | |
| 73 | err := gsp.context.WithReauth(gsp.access, func() error { |
| 74 | url := gsp.endpoint + "/os-keypairs/" + name |
| 75 | return perigee.Get(url, perigee.Options{ |
| 76 | CustomClient: gsp.context.httpClient, |
| 77 | Results: &struct{ KeyPair *KeyPair }{&kp}, |
| 78 | MoreHeaders: map[string]string{ |
| 79 | "X-Auth-Token": gsp.access.AuthToken(), |
| 80 | }, |
| 81 | }) |
| 82 | }) |
| 83 | return kp, err |
| 84 | } |
| 85 | |
| 86 | type KeyPair struct { |
| 87 | FingerPrint string `json:"fingerprint"` |
| 88 | Name string `json:"name"` |
Mark Peek | 3d151fb | 2013-08-18 12:25:10 -0700 | [diff] [blame] | 89 | PrivateKey string `json:"private_key,omitempty"` |
Mark Peek | 5a9151f | 2013-08-17 18:59:59 -0700 | [diff] [blame] | 90 | PublicKey string `json:"public_key"` |
| 91 | UserID string `json:"user_id,omitempty"` |
| 92 | } |
| 93 | |
| 94 | type NewKeyPair struct { |
| 95 | Name string `json:"name"` |
| 96 | PublicKey string `json:"public_key,omitempty"` |
| 97 | } |