blob: 8ae8cd39396c96c44ebd696b9a341d07c0254f2a [file] [log] [blame]
Mark Peek5a9151f2013-08-17 18:59:59 -07001package gophercloud
2
3import (
4 "github.com/racker/perigee"
5)
6
7// See the CloudImagesProvider interface for details.
8func (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
36func (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 },
Marc Abramowitz74daf532014-08-12 09:22:35 -070050 OkCodes: []int{200},
Mark Peek5a9151f2013-08-17 18:59:59 -070051 })
52 })
53 return kp, err
54}
55
56// See the CloudImagesProvider interface for details.
57func (gsp *genericServersProvider) DeleteKeyPair(name string) error {
58 err := gsp.context.WithReauth(gsp.access, func() error {
59 url := gsp.endpoint + "/os-keypairs/" + name
60 return perigee.Delete(url, perigee.Options{
61 CustomClient: gsp.context.httpClient,
62 MoreHeaders: map[string]string{
63 "X-Auth-Token": gsp.access.AuthToken(),
64 },
65 OkCodes: []int{202},
66 })
67 })
68 return err
69}
70
71func (gsp *genericServersProvider) ShowKeyPair(name string) (KeyPair, error) {
72 var kp KeyPair
73
74 err := gsp.context.WithReauth(gsp.access, func() error {
75 url := gsp.endpoint + "/os-keypairs/" + name
76 return perigee.Get(url, perigee.Options{
77 CustomClient: gsp.context.httpClient,
78 Results: &struct{ KeyPair *KeyPair }{&kp},
79 MoreHeaders: map[string]string{
80 "X-Auth-Token": gsp.access.AuthToken(),
81 },
82 })
83 })
84 return kp, err
85}
86
87type KeyPair struct {
88 FingerPrint string `json:"fingerprint"`
89 Name string `json:"name"`
Mark Peek3d151fb2013-08-18 12:25:10 -070090 PrivateKey string `json:"private_key,omitempty"`
Mark Peek5a9151f2013-08-17 18:59:59 -070091 PublicKey string `json:"public_key"`
92 UserID string `json:"user_id,omitempty"`
93}
94
95type NewKeyPair struct {
96 Name string `json:"name"`
97 PublicKey string `json:"public_key,omitempty"`
98}