Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 1 | package bootfromvolume |
| 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" |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 8 | // SourceType represents the type of medium being used to create the volume. |
| 9 | type SourceType string |
| 10 | |
| 11 | const ( |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 12 | // Volume SourceType |
| 13 | Volume SourceType = "volume" |
| 14 | // Snapshot SourceType |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 15 | Snapshot SourceType = "snapshot" |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 16 | // Image SourceType |
| 17 | Image SourceType = "image" |
| 18 | // Blank SourceType |
| 19 | Blank SourceType = "blank" |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 20 | ) |
| 21 | |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 22 | // BlockDevice is a structure with options for booting a server instance |
| 23 | // from a volume. The volume may be created from an image, snapshot, or another |
| 24 | // volume. |
| 25 | type BlockDevice struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 26 | // SourceType must be one of: "volume", "snapshot", "image". |
| 27 | SourceType SourceType `json:"source_type" required:"true"` |
| 28 | // UUID is the unique identifier for the volume, snapshot, or image (see above) |
| 29 | UUID string `json:"uuid,omitempty"` |
| 30 | // BootIndex is the boot index. It defaults to 0. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 31 | BootIndex int `json:"boot_index"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 32 | // DeleteOnTermination specifies whether or not to delete the attached volume |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 33 | // when the server is deleted. Defaults to `false`. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 34 | DeleteOnTermination bool `json:"delete_on_termination"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 35 | // DestinationType is the type that gets created. Possible values are "volume" |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 36 | // and "local". |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 37 | DestinationType string `json:"destination_type,omitempty"` |
| 38 | // GuestFormat specifies the format of the block device. |
| 39 | GuestFormat string `json:"guest_format,omitempty"` |
| 40 | // VolumeSize is the size of the volume to create (in gigabytes). |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 41 | VolumeSize int `json:"volume_size"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // CreateOptsExt is a structure that extends the server `CreateOpts` structure |
| 45 | // by allowing for a block device mapping. |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 46 | type CreateOptsExt struct { |
| 47 | servers.CreateOptsBuilder |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 48 | BlockDevice []BlockDevice `json:"block_device_mapping_v2,omitempty"` |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // ToServerCreateMap adds the block device mapping option to the base server |
| 52 | // creation options. |
| 53 | func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) { |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 54 | base, err := opts.CreateOptsBuilder.ToServerCreateMap() |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 59 | if len(opts.BlockDevice) == 0 { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 60 | err := gophercloud.ErrMissingInput{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 61 | err.Argument = "bootfromvolume.CreateOptsExt.BlockDevice" |
| 62 | return nil, err |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 63 | } |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 64 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 65 | serverMap := base["server"].(map[string]interface{}) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 66 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 67 | blockDevice := make([]map[string]interface{}, len(opts.BlockDevice)) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 68 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 69 | for i, bd := range opts.BlockDevice { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 70 | b, err := gophercloud.BuildRequestBody(bd, "") |
| 71 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 72 | return nil, err |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 73 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 74 | blockDevice[i] = b |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 75 | } |
| 76 | serverMap["block_device_mapping_v2"] = blockDevice |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 77 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 78 | return base, nil |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 79 | } |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 80 | |
| 81 | // Create requests the creation of a server from the given block device mapping. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 82 | func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) (r servers.CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 83 | b, err := opts.ToServerCreateMap() |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 84 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 85 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 86 | return |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 87 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 88 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 89 | OkCodes: []int{200, 202}, |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 90 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 91 | return |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 92 | } |