blob: 2b40943d10467a3af01e2585d2bd459eeedacfcc [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.
42func ExtractKeyPairs(page pagination.Page) ([]KeyPair, error) {
Jon Perritt12395212016-02-24 10:41:17 -060043 r := page.(KeyPairPage)
Ash Wilsonad612f62014-10-22 14:04:37 -040044 type pair struct {
Jon Perritt12395212016-02-24 10:41:17 -060045 KeyPair KeyPair `json:"keypair"`
Ash Wilsonad612f62014-10-22 14:04:37 -040046 }
Jon Perritt12395212016-02-24 10:41:17 -060047 var s struct {
48 KeyPairs []pair `json:"keypairs"`
Ash Wilsonad612f62014-10-22 14:04:37 -040049 }
Jon Perritt12395212016-02-24 10:41:17 -060050 err := r.ExtractInto(&s)
51 results := make([]KeyPair, len(s.KeyPairs))
52 for i, pair := range s.KeyPairs {
Ash Wilsonad612f62014-10-22 14:04:37 -040053 results[i] = pair.KeyPair
54 }
55 return results, err
56}
57
58type keyPairResult struct {
59 gophercloud.Result
60}
61
62// Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct.
63func (r keyPairResult) Extract() (*KeyPair, error) {
Jon Perritt12395212016-02-24 10:41:17 -060064 var s struct {
65 KeyPair *KeyPair `json:"keypair"`
Ash Wilsonad612f62014-10-22 14:04:37 -040066 }
Jon Perritt12395212016-02-24 10:41:17 -060067 err := r.ExtractInto(&s)
68 return s.KeyPair, err
Ash Wilsonad612f62014-10-22 14:04:37 -040069}
70
71// CreateResult is the response from a Create operation. Call its Extract method to interpret it
72// as a KeyPair.
73type 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.
79type 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.
85type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050086 gophercloud.ErrResult
Ash Wilson41766632014-10-22 15:07:58 -040087}