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 | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame^] | 13 | // BlockDevice is a structure with options for booting a server instance |
| 14 | // from a volume. The volume may be created from an image, snapshot, or another |
| 15 | // volume. |
| 16 | type BlockDevice struct { |
| 17 | // BootIndex [optional] is the boot index. It defaults to 0. |
| 18 | BootIndex int |
| 19 | |
| 20 | // DeleteOnTermination [optional] specifies whether or not to delete the attached volume |
| 21 | // when the server is deleted. Defaults to `false`. |
| 22 | DeleteOnTermination bool |
| 23 | |
| 24 | // DestinationType [optional] is the type that gets created. Possible values are "volume" |
| 25 | // and "local". |
| 26 | DestinationType string |
| 27 | |
| 28 | // SourceType [optional] must be one of: "volume", "snapshot", "image". |
| 29 | SourceType string |
| 30 | |
| 31 | // UUID [optional] is the unique identifier for the volume, snapshot, or image (see above) |
| 32 | UUID string |
| 33 | |
| 34 | // VolumeSize [optional] is the size of the volume to create (in gigabytes). |
| 35 | VolumeSize int |
| 36 | } |
| 37 | |
| 38 | // CreateOptsExt is a structure that extends the server `CreateOpts` structure |
| 39 | // by allowing for a block device mapping. |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 40 | type CreateOptsExt struct { |
| 41 | servers.CreateOptsBuilder |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame^] | 42 | BlockDevice BlockDevice |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // ToServerCreateMap adds the block device mapping option to the base server |
| 46 | // creation options. |
| 47 | func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) { |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame^] | 48 | if opts.BlockDevice.SourceType != "volume" && |
| 49 | opts.BlockDevice.SourceType != "image" && |
| 50 | opts.BlockDevice.SourceType != "snapshot" && |
| 51 | opts.BlockDevice.SourceType != "" { |
| 52 | return nil, errors.New("SourceType must be one of: volume, image, snapshot, [blank].") |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 53 | } |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 54 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 55 | base, err := opts.CreateOptsBuilder.ToServerCreateMap() |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 59 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 60 | serverMap := base["server"].(map[string]interface{}) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame^] | 61 | |
| 62 | bd := make(map[string]interface{}) |
| 63 | bd["source_type"] = opts.BlockDevice.SourceType |
| 64 | bd["boot_index"] = strconv.Itoa(opts.BlockDevice.BootIndex) |
| 65 | bd["delete_on_termination"] = strconv.FormatBool(opts.BlockDevice.DeleteOnTermination) |
| 66 | bd["volume_size"] = strconv.Itoa(opts.BlockDevice.VolumeSize) |
| 67 | if opts.BlockDevice.UUID != "" { |
| 68 | bd["uuid"] = opts.BlockDevice.UUID |
| 69 | } |
| 70 | if opts.BlockDevice.DestinationType != "" { |
| 71 | bd["destination_type"] = opts.BlockDevice.DestinationType |
| 72 | } |
| 73 | |
| 74 | serverMap["block_device_mapping_v2"] = []map[string]interface{}{bd} |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 75 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 76 | return base, nil |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 77 | } |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame^] | 78 | |
| 79 | // Create requests the creation of a server from the given block device mapping. |
| 80 | func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) servers.CreateResult { |
| 81 | var res servers.CreateResult |
| 82 | |
| 83 | reqBody, err := opts.ToServerCreateMap() |
| 84 | if err != nil { |
| 85 | res.Err = err |
| 86 | return res |
| 87 | } |
| 88 | |
| 89 | _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{ |
| 90 | MoreHeaders: client.AuthenticatedHeaders(), |
| 91 | ReqBody: reqBody, |
| 92 | Results: &res.Body, |
| 93 | OkCodes: []int{200}, |
| 94 | }) |
| 95 | return res |
| 96 | } |