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