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