blob: 715b49436500d7933c99f8cf69f200ae109460f0 [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 Wilson4aaaa1e2014-10-22 14:38:02 -04006 "github.com/racker/perigee"
Ash Wilsonad612f62014-10-22 14:04:37 -04007 "github.com/rackspace/gophercloud"
Jon Perritt5fdb7222014-11-17 14:41:54 -07008 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilsonad612f62014-10-22 14:04:37 -04009 "github.com/rackspace/gophercloud/pagination"
10)
11
Jon Perritt5fdb7222014-11-17 14:41:54 -070012// CreateOptsExt adds a KeyPair option to the base CreateOpts.
13type CreateOptsExt struct {
14 servers.CreateOptsBuilder
15 KeyName string `json:"key_name,omitempty"`
16}
17
18// ToServerCreateMap adds the key_name and, optionally, key_data options to
19// the base server creation options.
20func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
21 base, err := opts.CreateOptsBuilder.ToServerCreateMap()
22 if err != nil {
23 return nil, err
24 }
25
26 if opts.KeyName == "" {
27 return base, nil
28 }
29
30 serverMap := base["server"].(map[string]interface{})
31 serverMap["key_name"] = opts.KeyName
32
33 return base, nil
34}
35
Ash Wilsonad612f62014-10-22 14:04:37 -040036// List returns a Pager that allows you to iterate over a collection of KeyPairs.
37func List(client *gophercloud.ServiceClient) pagination.Pager {
38 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
39 return KeyPairPage{pagination.SinglePageBase(r)}
40 })
41}
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040042
Ash Wilson10fede72014-10-22 14:58:52 -040043// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
44// CreateOpts struct in this package does.
45type CreateOptsBuilder interface {
46 ToKeyPairCreateMap() (map[string]interface{}, error)
47}
48
Jon Perritt5fdb7222014-11-17 14:41:54 -070049// CreateOpts specifies keypair creation or import parameters.
Ash Wilson10fede72014-10-22 14:58:52 -040050type CreateOpts struct {
51 // Name [required] is a friendly name to refer to this KeyPair in other services.
52 Name string
53
54 // PublicKey [optional] is a pregenerated OpenSSH-formatted public key. If provided, this key
55 // will be imported and no new key will be created.
56 PublicKey string
57}
58
59// ToKeyPairCreateMap constructs a request body from CreateOpts.
60func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) {
61 if opts.Name == "" {
62 return nil, errors.New("Missing field required for keypair creation: Name")
63 }
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
Ash Wilson4bf41a32015-02-12 15:52:44 -050085 _, res.Err = client.Request("POST", createURL(client), gophercloud.RequestOpts{
86 JSONBody: reqBody,
87 JSONResponse: &res.Body,
88 OkCodes: []int{200},
Ash Wilson10fede72014-10-22 14:58:52 -040089 })
90 return res
91}
92
Ash Wilson4aaaa1e2014-10-22 14:38:02 -040093// Get returns public data about a previously uploaded KeyPair.
94func Get(client *gophercloud.ServiceClient, name string) GetResult {
95 var res GetResult
96 _, res.Err = perigee.Request("GET", getURL(client, name), perigee.Options{
97 MoreHeaders: client.AuthenticatedHeaders(),
98 Results: &res.Body,
99 OkCodes: []int{200},
100 })
101 return res
102}
Ash Wilson41766632014-10-22 15:07:58 -0400103
104// Delete requests the deletion of a previous stored KeyPair from the server.
105func Delete(client *gophercloud.ServiceClient, name string) DeleteResult {
106 var res DeleteResult
107 _, res.Err = perigee.Request("DELETE", deleteURL(client, name), perigee.Options{
108 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilson41766632014-10-22 15:07:58 -0400109 OkCodes: []int{202},
110 })
111 return res
112}