blob: c56ee67ea2d294df48581071532243958b0c7536 [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 Wilsonad612f62014-10-22 14:04:37 -04006 "github.com/rackspace/gophercloud"
Jon Perritt5fdb7222014-11-17 14:41:54 -07007 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilsonad612f62014-10-22 14:04:37 -04008 "github.com/rackspace/gophercloud/pagination"
9)
10
Jon Perritt5fdb7222014-11-17 14:41:54 -070011// CreateOptsExt adds a KeyPair option to the base CreateOpts.
12type CreateOptsExt struct {
13 servers.CreateOptsBuilder
14 KeyName string `json:"key_name,omitempty"`
15}
16
17// ToServerCreateMap adds the key_name and, optionally, key_data options to
18// the base server creation options.
19func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
20 base, err := opts.CreateOptsBuilder.ToServerCreateMap()
21 if err != nil {
22 return nil, err
23 }
24
25 if opts.KeyName == "" {
26 return base, nil
27 }
28
29 serverMap := base["server"].(map[string]interface{})
30 serverMap["key_name"] = opts.KeyName
31
32 return base, nil
33}
34
Ash Wilsonad612f62014-10-22 14:04:37 -040035// List returns a Pager that allows you to iterate over a collection of KeyPairs.
36func List(client *gophercloud.ServiceClient) pagination.Pager {
37 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
38 return KeyPairPage{pagination.SinglePageBase(r)}
39 })
40}
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040041
Ash Wilson10fede72014-10-22 14:58:52 -040042// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
43// CreateOpts struct in this package does.
44type CreateOptsBuilder interface {
45 ToKeyPairCreateMap() (map[string]interface{}, error)
46}
47
Jon Perritt5fdb7222014-11-17 14:41:54 -070048// CreateOpts specifies keypair creation or import parameters.
Ash Wilson10fede72014-10-22 14:58:52 -040049type CreateOpts struct {
50 // Name [required] is a friendly name to refer to this KeyPair in other services.
51 Name string
52
53 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. If provided, this key
54 // will be imported and no new key will be created.
55 PublicKey string
56}
57
58// ToKeyPairCreateMap constructs a request body from CreateOpts.
59func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) {
60 if opts.Name == "" {
61 return nil, errors.New("Missing field required for keypair creation: Name")
62 }
63
64 keypair := make(map[string]interface{})
65 keypair["name"] = opts.Name
66 if opts.PublicKey != "" {
67 keypair["public_key"] = opts.PublicKey
68 }
69
70 return map[string]interface{}{"keypair": keypair}, nil
71}
72
73// Create requests the creation of a new keypair on the server, or to import a pre-existing
74// keypair.
75func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
76 var res CreateResult
77
78 reqBody, err := opts.ToKeyPairCreateMap()
79 if err != nil {
80 res.Err = err
81 return res
82 }
83
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010084 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
85 OkCodes: []int{200},
Ash Wilson10fede72014-10-22 14:58:52 -040086 })
87 return res
88}
89
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040090// Get returns public data about a previously uploaded KeyPair.
91func Get(client *gophercloud.ServiceClient, name string) GetResult {
92 var res GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010093 _, res.Err = client.Get(getURL(client, name), &res.Body, nil)
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040094 return res
95}
Ash Wilson41766632014-10-22 15:07:58 -040096
97// Delete requests the deletion of a previous stored KeyPair from the server.
98func Delete(client *gophercloud.ServiceClient, name string) DeleteResult {
99 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100100 _, res.Err = client.Delete(deleteURL(client, name), nil)
Ash Wilson41766632014-10-22 15:07:58 -0400101 return res
102}