blob: 2a010844015d82c9d364a60db2da26a729d50774 [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/openstack/compute/v2/servers"
6 "github.com/gophercloud/gophercloud/pagination"
Ash Wilsonad612f62014-10-22 14:04:37 -04007)
8
Jon Perritt5fdb7222014-11-17 14:41:54 -07009// CreateOptsExt adds a KeyPair option to the base CreateOpts.
10type CreateOptsExt struct {
11 servers.CreateOptsBuilder
12 KeyName string `json:"key_name,omitempty"`
13}
14
15// ToServerCreateMap adds the key_name and, optionally, key_data options to
16// the base server creation options.
17func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
18 base, err := opts.CreateOptsBuilder.ToServerCreateMap()
19 if err != nil {
20 return nil, err
21 }
22
23 if opts.KeyName == "" {
24 return base, nil
25 }
26
27 serverMap := base["server"].(map[string]interface{})
28 serverMap["key_name"] = opts.KeyName
29
30 return base, nil
31}
32
Ash Wilsonad612f62014-10-22 14:04:37 -040033// List returns a Pager that allows you to iterate over a collection of KeyPairs.
34func List(client *gophercloud.ServiceClient) pagination.Pager {
35 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
36 return KeyPairPage{pagination.SinglePageBase(r)}
37 })
38}
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040039
Ash Wilson10fede72014-10-22 14:58:52 -040040// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
41// CreateOpts struct in this package does.
42type CreateOptsBuilder interface {
43 ToKeyPairCreateMap() (map[string]interface{}, error)
44}
45
Jon Perritt5fdb7222014-11-17 14:41:54 -070046// CreateOpts specifies keypair creation or import parameters.
Ash Wilson10fede72014-10-22 14:58:52 -040047type CreateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060048 // Name is a friendly name to refer to this KeyPair in other services.
49 Name string `json:"name" required:"true"`
Ash Wilson10fede72014-10-22 14:58:52 -040050 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. If provided, this key
51 // will be imported and no new key will be created.
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 PublicKey string `json:"public_key,omitempty"`
Ash Wilson10fede72014-10-22 14:58:52 -040053}
54
55// ToKeyPairCreateMap constructs a request body from CreateOpts.
56func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060057 return gophercloud.BuildRequestBody(opts, "keypair")
Ash Wilson10fede72014-10-22 14:58:52 -040058}
59
60// Create requests the creation of a new keypair on the server, or to import a pre-existing
61// keypair.
62func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060063 var r CreateResult
64 b, err := opts.ToKeyPairCreateMap()
Ash Wilson10fede72014-10-22 14:58:52 -040065 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060066 r.Err = err
67 return r
Ash Wilson10fede72014-10-22 14:58:52 -040068 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060069 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010070 OkCodes: []int{200},
Ash Wilson10fede72014-10-22 14:58:52 -040071 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060072 return r
Ash Wilson10fede72014-10-22 14:58:52 -040073}
74
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040075// Get returns public data about a previously uploaded KeyPair.
76func Get(client *gophercloud.ServiceClient, name string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060077 var r GetResult
78 _, r.Err = client.Get(getURL(client, name), &r.Body, nil)
79 return r
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040080}
Ash Wilson41766632014-10-22 15:07:58 -040081
82// Delete requests the deletion of a previous stored KeyPair from the server.
83func Delete(client *gophercloud.ServiceClient, name string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060084 var r DeleteResult
85 _, r.Err = client.Delete(deleteURL(client, name), nil)
86 return r
Ash Wilson41766632014-10-22 15:07:58 -040087}