Ash Wilson | 0482daf | 2014-10-22 11:24:40 -0400 | [diff] [blame] | 1 | package keypairs |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 7 | ) |
| 8 | |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 9 | // CreateOptsExt adds a KeyPair option to the base CreateOpts. |
| 10 | type 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. |
| 17 | func (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 Wilson | ad612f6 | 2014-10-22 14:04:37 -0400 | [diff] [blame] | 33 | // List returns a Pager that allows you to iterate over a collection of KeyPairs. |
| 34 | func 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 Wilson | 4aaaa1e | 2014-10-22 14:38:02 -0400 | [diff] [blame] | 39 | |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 40 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the |
| 41 | // CreateOpts struct in this package does. |
| 42 | type CreateOptsBuilder interface { |
| 43 | ToKeyPairCreateMap() (map[string]interface{}, error) |
| 44 | } |
| 45 | |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 46 | // CreateOpts specifies keypair creation or import parameters. |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 47 | type CreateOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 48 | // Name is a friendly name to refer to this KeyPair in other services. |
| 49 | Name string `json:"name" required:"true"` |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 50 | // 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 Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | PublicKey string `json:"public_key,omitempty"` |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // ToKeyPairCreateMap constructs a request body from CreateOpts. |
| 56 | func (opts CreateOpts) ToKeyPairCreateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 57 | return gophercloud.BuildRequestBody(opts, "keypair") |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Create requests the creation of a new keypair on the server, or to import a pre-existing |
| 61 | // keypair. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 62 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 63 | b, err := opts.ToKeyPairCreateMap() |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 64 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 65 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 66 | return |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 67 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 68 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 69 | OkCodes: []int{200}, |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 70 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 71 | return |
Ash Wilson | 10fede7 | 2014-10-22 14:58:52 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | 4aaaa1e | 2014-10-22 14:38:02 -0400 | [diff] [blame] | 74 | // Get returns public data about a previously uploaded KeyPair. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 75 | func Get(client *gophercloud.ServiceClient, name string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 76 | _, r.Err = client.Get(getURL(client, name), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 77 | return |
Ash Wilson | 4aaaa1e | 2014-10-22 14:38:02 -0400 | [diff] [blame] | 78 | } |
Ash Wilson | 4176663 | 2014-10-22 15:07:58 -0400 | [diff] [blame] | 79 | |
| 80 | // Delete requests the deletion of a previous stored KeyPair from the server. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 81 | func Delete(client *gophercloud.ServiceClient, name string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 82 | _, r.Err = client.Delete(deleteURL(client, name), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 83 | return |
Ash Wilson | 4176663 | 2014-10-22 15:07:58 -0400 | [diff] [blame] | 84 | } |