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. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 18 | BootIndex int `json:"boot_index"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 19 | |
| 20 | // DeleteOnTermination [optional] specifies whether or not to delete the attached volume |
| 21 | // when the server is deleted. Defaults to `false`. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 22 | DeleteOnTermination bool `json:"delete_on_termination"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 23 | |
| 24 | // DestinationType [optional] is the type that gets created. Possible values are "volume" |
| 25 | // and "local". |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 26 | DestinationType string `json:"destination_type"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 27 | |
| 28 | // SourceType [optional] must be one of: "volume", "snapshot", "image". |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 29 | SourceType string `json:"source_type"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 30 | |
| 31 | // UUID [optional] is the unique identifier for the volume, snapshot, or image (see above) |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 32 | UUID string `json:"uuid"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 33 | |
| 34 | // VolumeSize [optional] is the size of the volume to create (in gigabytes). |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 35 | VolumeSize int `json:"volume_size"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 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 | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 42 | BlockDevice BlockDevice `json:"block_device_mapping_v2,omitempty"` |
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 | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 48 | base, err := opts.CreateOptsBuilder.ToServerCreateMap() |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | var blockDevice BlockDevice |
| 54 | if opts.BlockDevice == blockDevice { |
| 55 | return base, nil |
| 56 | } |
| 57 | |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 58 | if opts.BlockDevice.SourceType != "volume" && |
| 59 | opts.BlockDevice.SourceType != "image" && |
| 60 | opts.BlockDevice.SourceType != "snapshot" && |
| 61 | opts.BlockDevice.SourceType != "" { |
| 62 | 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] | 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 | |
| 67 | bd := make(map[string]interface{}) |
| 68 | bd["source_type"] = opts.BlockDevice.SourceType |
| 69 | bd["boot_index"] = strconv.Itoa(opts.BlockDevice.BootIndex) |
| 70 | bd["delete_on_termination"] = strconv.FormatBool(opts.BlockDevice.DeleteOnTermination) |
| 71 | bd["volume_size"] = strconv.Itoa(opts.BlockDevice.VolumeSize) |
| 72 | if opts.BlockDevice.UUID != "" { |
| 73 | bd["uuid"] = opts.BlockDevice.UUID |
| 74 | } |
| 75 | if opts.BlockDevice.DestinationType != "" { |
| 76 | bd["destination_type"] = opts.BlockDevice.DestinationType |
| 77 | } |
| 78 | |
| 79 | serverMap["block_device_mapping_v2"] = []map[string]interface{}{bd} |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 80 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 81 | return base, nil |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 82 | } |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 83 | |
| 84 | // Create requests the creation of a server from the given block device mapping. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 85 | func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) CreateResult { |
| 86 | var res CreateResult |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 87 | |
| 88 | reqBody, err := opts.ToServerCreateMap() |
| 89 | if err != nil { |
| 90 | res.Err = err |
| 91 | return res |
| 92 | } |
| 93 | |
| 94 | _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{ |
| 95 | MoreHeaders: client.AuthenticatedHeaders(), |
| 96 | ReqBody: reqBody, |
| 97 | Results: &res.Body, |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 98 | OkCodes: []int{200, 202}, |
| 99 | DumpReqJson: true, |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 100 | }) |
| 101 | return res |
| 102 | } |