blob: b9b32eebd19b2775ce73ef5021b3d1bac8c1a4bd [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 {
48 // Name [required] is a friendly name to refer to this KeyPair in other services.
49 Name string
50
51 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. If provided, this key
52 // will be imported and no new key will be created.
53 PublicKey string
54}
55
56// ToKeyPairCreateMap constructs a request body from CreateOpts.
57func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) {
58 if opts.Name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -060059 err := gophercloud.ErrMissingInput{}
60 err.Function = "keypairs.ToKeyPairCreateMap"
61 err.Argument = "keypairs.CreateOpts.Name"
62 return nil, err
Ash Wilson10fede72014-10-22 14:58:52 -040063 }
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
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010085 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
86 OkCodes: []int{200},
Ash Wilson10fede72014-10-22 14:58:52 -040087 })
88 return res
89}
90
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040091// Get returns public data about a previously uploaded KeyPair.
92func Get(client *gophercloud.ServiceClient, name string) GetResult {
93 var res GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010094 _, res.Err = client.Get(getURL(client, name), &res.Body, nil)
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040095 return res
96}
Ash Wilson41766632014-10-22 15:07:58 -040097
98// Delete requests the deletion of a previous stored KeyPair from the server.
99func Delete(client *gophercloud.ServiceClient, name string) DeleteResult {
100 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100101 _, res.Err = client.Delete(deleteURL(client, name), nil)
Ash Wilson41766632014-10-22 15:07:58 -0400102 return res
103}