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" |
Joe Topjian | ecf63dd | 2015-12-12 20:33:50 +0000 | [diff] [blame] | 18 | Blank SourceType = "blank" |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 19 | ) |
| 20 | |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 21 | // BlockDevice is a structure with options for booting a server instance |
| 22 | // from a volume. The volume may be created from an image, snapshot, or another |
| 23 | // volume. |
| 24 | type BlockDevice struct { |
| 25 | // BootIndex [optional] is the boot index. It defaults to 0. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 26 | BootIndex int `json:"boot_index"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 27 | |
| 28 | // DeleteOnTermination [optional] specifies whether or not to delete the attached volume |
| 29 | // when the server is deleted. Defaults to `false`. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 30 | DeleteOnTermination bool `json:"delete_on_termination"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 31 | |
| 32 | // DestinationType [optional] is the type that gets created. Possible values are "volume" |
| 33 | // and "local". |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 34 | DestinationType string `json:"destination_type"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 35 | |
Joe Topjian | ecf63dd | 2015-12-12 20:33:50 +0000 | [diff] [blame] | 36 | // GuestFormat [optional] specifies the format of the block device. |
| 37 | GuestFormat string `json:"guest_format"` |
| 38 | |
Jon Perritt | 6171022 | 2014-10-24 13:01:31 -0500 | [diff] [blame] | 39 | // SourceType [required] must be one of: "volume", "snapshot", "image". |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 40 | SourceType SourceType `json:"source_type"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 41 | |
Jon Perritt | 6171022 | 2014-10-24 13:01:31 -0500 | [diff] [blame] | 42 | // 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] | 43 | UUID string `json:"uuid"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 44 | |
| 45 | // VolumeSize [optional] is the size of the volume to create (in gigabytes). |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 46 | VolumeSize int `json:"volume_size"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // CreateOptsExt is a structure that extends the server `CreateOpts` structure |
| 50 | // by allowing for a block device mapping. |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 51 | type CreateOptsExt struct { |
| 52 | servers.CreateOptsBuilder |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 53 | BlockDevice []BlockDevice `json:"block_device_mapping_v2,omitempty"` |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // ToServerCreateMap adds the block device mapping option to the base server |
| 57 | // creation options. |
| 58 | func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) { |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 59 | base, err := opts.CreateOptsBuilder.ToServerCreateMap() |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 64 | if len(opts.BlockDevice) == 0 { |
| 65 | return nil, errors.New("Required fields UUID and SourceType not set.") |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 66 | } |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 67 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 68 | serverMap := base["server"].(map[string]interface{}) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 69 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 70 | blockDevice := make([]map[string]interface{}, len(opts.BlockDevice)) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 71 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 72 | for i, bd := range opts.BlockDevice { |
| 73 | if string(bd.SourceType) == "" { |
| 74 | return nil, errors.New("SourceType must be one of: volume, image, snapshot.") |
| 75 | } |
| 76 | |
| 77 | blockDevice[i] = make(map[string]interface{}) |
| 78 | |
| 79 | blockDevice[i]["source_type"] = bd.SourceType |
| 80 | blockDevice[i]["boot_index"] = strconv.Itoa(bd.BootIndex) |
| 81 | blockDevice[i]["delete_on_termination"] = strconv.FormatBool(bd.DeleteOnTermination) |
| 82 | blockDevice[i]["volume_size"] = strconv.Itoa(bd.VolumeSize) |
| 83 | if bd.UUID != "" { |
| 84 | blockDevice[i]["uuid"] = bd.UUID |
| 85 | } |
| 86 | if bd.DestinationType != "" { |
| 87 | blockDevice[i]["destination_type"] = bd.DestinationType |
| 88 | } |
Joe Topjian | ecf63dd | 2015-12-12 20:33:50 +0000 | [diff] [blame] | 89 | if bd.GuestFormat != "" { |
| 90 | blockDevice[i]["guest_format"] = bd.GuestFormat |
| 91 | } |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 92 | |
| 93 | } |
| 94 | serverMap["block_device_mapping_v2"] = blockDevice |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 95 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 96 | return base, nil |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 97 | } |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 98 | |
| 99 | // 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] | 100 | func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) servers.CreateResult { |
| 101 | var res servers.CreateResult |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 102 | |
| 103 | reqBody, err := opts.ToServerCreateMap() |
| 104 | if err != nil { |
| 105 | res.Err = err |
| 106 | return res |
| 107 | } |
| 108 | |
Joe Topjian | 9d6e6a9 | 2015-10-31 23:39:22 +0000 | [diff] [blame] | 109 | // Delete imageName and flavorName that come from ToServerCreateMap(). |
| 110 | // As of Liberty, Boot From Volume is failing if they are passed. |
| 111 | delete(reqBody["server"].(map[string]interface{}), "imageName") |
| 112 | delete(reqBody["server"].(map[string]interface{}), "flavorName") |
| 113 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 114 | _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 115 | OkCodes: []int{200, 202}, |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 116 | }) |
| 117 | return res |
| 118 | } |