blob: adf1e5596f8b75f6ba44518c93202ce569d16b04 [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.
Jon Perritt3860b512016-03-29 12:01:48 -050062func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060063 b, err := opts.ToKeyPairCreateMap()
Ash Wilson10fede72014-10-22 14:58:52 -040064 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060065 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050066 return
Ash Wilson10fede72014-10-22 14:58:52 -040067 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060068 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010069 OkCodes: []int{200},
Ash Wilson10fede72014-10-22 14:58:52 -040070 })
jrperritt29ae6b32016-04-13 12:59:37 -050071 return
Ash Wilson10fede72014-10-22 14:58:52 -040072}
73
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040074// Get returns public data about a previously uploaded KeyPair.
Jon Perritt3860b512016-03-29 12:01:48 -050075func Get(client *gophercloud.ServiceClient, name string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060076 _, r.Err = client.Get(getURL(client, name), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050077 return
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040078}
Ash Wilson41766632014-10-22 15:07:58 -040079
80// Delete requests the deletion of a previous stored KeyPair from the server.
Jon Perritt3860b512016-03-29 12:01:48 -050081func Delete(client *gophercloud.ServiceClient, name string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060082 _, r.Err = client.Delete(deleteURL(client, name), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050083 return
Ash Wilson41766632014-10-22 15:07:58 -040084}