blob: f4d8d35c3e9736893cff98f1106d7cc0764e9d8f [file] [log] [blame]
Ash Wilson0482daf2014-10-22 11:24:40 -04001package keypairs
Ash Wilsonad612f62014-10-22 14:04:37 -04002
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Ash Wilsonad612f62014-10-22 14:04:37 -04006)
7
8// KeyPair is an SSH key known to the OpenStack cluster that is available to be injected into
9// servers.
10type KeyPair struct {
11 // Name is used to refer to this keypair from other services within this region.
Jon Perritt12395212016-02-24 10:41:17 -060012 Name string `json:"name"`
Ash Wilsonad612f62014-10-22 14:04:37 -040013
14 // Fingerprint is a short sequence of bytes that can be used to authenticate or validate a longer
15 // public key.
Jon Perritt12395212016-02-24 10:41:17 -060016 Fingerprint string `json:"fingerprint"`
Ash Wilsonad612f62014-10-22 14:04:37 -040017
18 // PublicKey is the public key from this pair, in OpenSSH format. "ssh-rsa AAAAB3Nz..."
Jon Perritt12395212016-02-24 10:41:17 -060019 PublicKey string `json:"public_key"`
Ash Wilsonad612f62014-10-22 14:04:37 -040020
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 Perritt12395212016-02-24 10:41:17 -060024 PrivateKey string `json:"private_key"`
Ash Wilsonad612f62014-10-22 14:04:37 -040025
26 // UserID is the user who owns this keypair.
Jon Perritt12395212016-02-24 10:41:17 -060027 UserID string `json:"user_id"`
Ash Wilsonad612f62014-10-22 14:04:37 -040028}
29
30// KeyPairPage stores a single, only page of KeyPair results from a List call.
31type KeyPairPage struct {
32 pagination.SinglePageBase
33}
34
35// IsEmpty determines whether or not a KeyPairPage is empty.
36func (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 Perritt31b66462016-02-25 22:25:30 -060042func ExtractKeyPairs(r pagination.Page) ([]KeyPair, error) {
Ash Wilsonad612f62014-10-22 14:04:37 -040043 type pair struct {
Jon Perritt12395212016-02-24 10:41:17 -060044 KeyPair KeyPair `json:"keypair"`
Ash Wilsonad612f62014-10-22 14:04:37 -040045 }
Jon Perritt12395212016-02-24 10:41:17 -060046 var s struct {
47 KeyPairs []pair `json:"keypairs"`
Ash Wilsonad612f62014-10-22 14:04:37 -040048 }
Jon Perritt31b66462016-02-25 22:25:30 -060049 err := (r.(KeyPairPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060050 results := make([]KeyPair, len(s.KeyPairs))
51 for i, pair := range s.KeyPairs {
Ash Wilsonad612f62014-10-22 14:04:37 -040052 results[i] = pair.KeyPair
53 }
54 return results, err
55}
56
57type keyPairResult struct {
58 gophercloud.Result
59}
60
61// Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct.
62func (r keyPairResult) Extract() (*KeyPair, error) {
Jon Perritt12395212016-02-24 10:41:17 -060063 var s struct {
64 KeyPair *KeyPair `json:"keypair"`
Ash Wilsonad612f62014-10-22 14:04:37 -040065 }
Jon Perritt12395212016-02-24 10:41:17 -060066 err := r.ExtractInto(&s)
67 return s.KeyPair, err
Ash Wilsonad612f62014-10-22 14:04:37 -040068}
69
70// CreateResult is the response from a Create operation. Call its Extract method to interpret it
71// as a KeyPair.
72type 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.
78type 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.
84type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050085 gophercloud.ErrResult
Ash Wilson41766632014-10-22 15:07:58 -040086}