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