blob: 7b372a355fbe8a254ca9cf2239afc930b6083fa0 [file] [log] [blame]
Ash Wilson0482daf2014-10-22 11:24:40 -04001package keypairs
Ash Wilsonad612f62014-10-22 14:04:37 -04002
3import (
Ash Wilson10fede72014-10-22 14:58:52 -04004 "errors"
5
Ash Wilson4aaaa1e2014-10-22 14:38:02 -04006 "github.com/racker/perigee"
Ash Wilsonad612f62014-10-22 14:04:37 -04007 "github.com/rackspace/gophercloud"
Jon Perritt5fdb7222014-11-17 14:41:54 -07008 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilsonad612f62014-10-22 14:04:37 -04009 "github.com/rackspace/gophercloud/pagination"
10)
11
Jon Perritt5fdb7222014-11-17 14:41:54 -070012// CreateOptsExt adds a KeyPair option to the base CreateOpts.
13type CreateOptsExt struct {
14 servers.CreateOptsBuilder
15 KeyName string `json:"key_name,omitempty"`
16}
17
18// ToServerCreateMap adds the key_name and, optionally, key_data options to
19// the base server creation options.
20func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
21 base, err := opts.CreateOptsBuilder.ToServerCreateMap()
22 if err != nil {
23 return nil, err
24 }
25
26 if opts.KeyName == "" {
27 return base, nil
28 }
29
30 serverMap := base["server"].(map[string]interface{})
31 serverMap["key_name"] = opts.KeyName
32
33 return base, nil
34}
35
Ash Wilsonad612f62014-10-22 14:04:37 -040036// List returns a Pager that allows you to iterate over a collection of KeyPairs.
37func List(client *gophercloud.ServiceClient) pagination.Pager {
38 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
39 return KeyPairPage{pagination.SinglePageBase(r)}
40 })
41}
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040042
Ash Wilson10fede72014-10-22 14:58:52 -040043// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
44// CreateOpts struct in this package does.
45type CreateOptsBuilder interface {
46 ToKeyPairCreateMap() (map[string]interface{}, error)
47}
48
Jon Perritt5fdb7222014-11-17 14:41:54 -070049// CreateOpts specifies keypair creation or import parameters.
Ash Wilson10fede72014-10-22 14:58:52 -040050type CreateOpts struct {
51 // Name [required] is a friendly name to refer to this KeyPair in other services.
52 Name string
53
54 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. If provided, this key
55 // will be imported and no new key will be created.
56 PublicKey string
57}
58
59// ToKeyPairCreateMap constructs a request body from CreateOpts.
60func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) {
61 if opts.Name == "" {
62 return nil, errors.New("Missing field required for keypair creation: Name")
63 }
64
65 keypair := make(map[string]interface{})
66 keypair["name"] = opts.Name
67 if opts.PublicKey != "" {
68 keypair["public_key"] = opts.PublicKey
69 }
70
71 return map[string]interface{}{"keypair": keypair}, nil
72}
73
74// Create requests the creation of a new keypair on the server, or to import a pre-existing
75// keypair.
76func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
77 var res CreateResult
78
79 reqBody, err := opts.ToKeyPairCreateMap()
80 if err != nil {
81 res.Err = err
82 return res
83 }
84
85 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
86 MoreHeaders: client.AuthenticatedHeaders(),
87 ReqBody: reqBody,
88 Results: &res.Body,
89 OkCodes: []int{200},
90 })
91 return res
92}
93
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040094// Get returns public data about a previously uploaded KeyPair.
95func Get(client *gophercloud.ServiceClient, name string) GetResult {
96 var res GetResult
97 _, res.Err = perigee.Request("GET", getURL(client, name), perigee.Options{
98 MoreHeaders: client.AuthenticatedHeaders(),
99 Results: &res.Body,
100 OkCodes: []int{200},
101 })
102 return res
103}
Ash Wilson41766632014-10-22 15:07:58 -0400104
105// Delete requests the deletion of a previous stored KeyPair from the server.
106func Delete(client *gophercloud.ServiceClient, name string) DeleteResult {
107 var res DeleteResult
108 _, res.Err = perigee.Request("DELETE", deleteURL(client, name), perigee.Options{
109 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilson41766632014-10-22 15:07:58 -0400110 OkCodes: []int{202},
111 })
112 return res
113}