1package keypairs
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/pagination"
6)
7
8// KeyPair is an SSH key known to the OpenStack Cloud that is available to be
9// injected into servers.
10type KeyPair struct {
11	// Name is used to refer to this keypair from other services within this
12	// region.
13	Name string `json:"name"`
14
15	// Fingerprint is a short sequence of bytes that can be used to authenticate
16	// or validate a longer public key.
17	Fingerprint string `json:"fingerprint"`
18
19	// PublicKey is the public key from this pair, in OpenSSH format.
20	// "ssh-rsa AAAAB3Nz..."
21	PublicKey string `json:"public_key"`
22
23	// PrivateKey is the private key from this pair, in PEM format.
24	// "-----BEGIN RSA PRIVATE KEY-----\nMIICXA..."
25	// It is only present if this KeyPair was just returned from a Create call.
26	PrivateKey string `json:"private_key"`
27
28	// UserID is the user who owns this KeyPair.
29	UserID string `json:"user_id"`
30}
31
32// KeyPairPage stores a single page of all KeyPair results from a List call.
33// Use the ExtractKeyPairs function to convert the results to a slice of
34// KeyPairs.
35type KeyPairPage struct {
36	pagination.SinglePageBase
37}
38
39// IsEmpty determines whether or not a KeyPairPage is empty.
40func (page KeyPairPage) IsEmpty() (bool, error) {
41	ks, err := ExtractKeyPairs(page)
42	return len(ks) == 0, err
43}
44
45// ExtractKeyPairs interprets a page of results as a slice of KeyPairs.
46func ExtractKeyPairs(r pagination.Page) ([]KeyPair, error) {
47	type pair struct {
48		KeyPair KeyPair `json:"keypair"`
49	}
50	var s struct {
51		KeyPairs []pair `json:"keypairs"`
52	}
53	err := (r.(KeyPairPage)).ExtractInto(&s)
54	results := make([]KeyPair, len(s.KeyPairs))
55	for i, pair := range s.KeyPairs {
56		results[i] = pair.KeyPair
57	}
58	return results, err
59}
60
61type keyPairResult struct {
62	gophercloud.Result
63}
64
65// Extract is a method that attempts to interpret any KeyPair resource response
66// as a KeyPair struct.
67func (r keyPairResult) Extract() (*KeyPair, error) {
68	var s struct {
69		KeyPair *KeyPair `json:"keypair"`
70	}
71	err := r.ExtractInto(&s)
72	return s.KeyPair, err
73}
74
75// CreateResult is the response from a Create operation. Call its Extract method
76// to interpret it as a KeyPair.
77type CreateResult struct {
78	keyPairResult
79}
80
81// GetResult is the response from a Get operation. Call its Extract method to
82// interpret it as a KeyPair.
83type GetResult struct {
84	keyPairResult
85}
86
87// DeleteResult is the response from a Delete operation. Call its ExtractErr
88// method to determine if the call succeeded or failed.
89type DeleteResult struct {
90	gophercloud.ErrResult
91}
92