blob: 287e4127c5aaa61e37bc74a773e1b153134d133b [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
Ash Wilson4bf41a32015-02-12 15:52:44 -050084 _, res.Err = client.Request("POST", createURL(client), gophercloud.RequestOpts{
85 JSONBody: reqBody,
86 JSONResponse: &res.Body,
87 OkCodes: []int{200},
Ash Wilson10fede72014-10-22 14:58:52 -040088 })
89 return res
90}
91
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040092// Get returns public data about a previously uploaded KeyPair.
93func Get(client *gophercloud.ServiceClient, name string) GetResult {
94 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050095 _, res.Err = client.Request("GET", getURL(client, name), gophercloud.RequestOpts{
96 JSONResponse: &res.Body,
97 OkCodes: []int{200},
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040098 })
99 return res
100}
Ash Wilson41766632014-10-22 15:07:58 -0400101
102// Delete requests the deletion of a previous stored KeyPair from the server.
103func Delete(client *gophercloud.ServiceClient, name string) DeleteResult {
104 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500105 _, res.Err = client.Request("DELETE", deleteURL(client, name), gophercloud.RequestOpts{
106 OkCodes: []int{202},
Ash Wilson41766632014-10-22 15:07:58 -0400107 })
108 return res
109}