Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 1 | package bootfromvolume |
| 2 | |
| 3 | import ( |
Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 4 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 5 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/compute/v2/servers" |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 8 | type ( |
| 9 | // DestinationType represents the type of medium being used as the |
| 10 | // destination of the bootable device. |
| 11 | DestinationType string |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 12 | |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 13 | // SourceType represents the type of medium being used as the source of the |
| 14 | // bootable device. |
| 15 | SourceType string |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 16 | ) |
| 17 | |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 18 | const ( |
| 19 | // DestinationLocal DestinationType is for using an ephemeral disk as the |
| 20 | // destination. |
| 21 | DestinationLocal DestinationType = "local" |
| 22 | |
| 23 | // DestinationVolume DestinationType is for using a volume as the destination. |
| 24 | DestinationVolume DestinationType = "volume" |
| 25 | |
| 26 | // SourceBlank SourceType is for a "blank" or empty source. |
| 27 | SourceBlank SourceType = "blank" |
| 28 | |
| 29 | // SourceImage SourceType is for using images as the source of a block device. |
| 30 | SourceImage SourceType = "image" |
| 31 | |
| 32 | // SourceSnapshot SourceType is for using a volume snapshot as the source of |
| 33 | // a block device. |
| 34 | SourceSnapshot SourceType = "snapshot" |
| 35 | |
| 36 | // SourceVolume SourceType is for using a volume as the source of block |
| 37 | // device. |
| 38 | SourceVolume SourceType = "volume" |
| 39 | ) |
| 40 | |
| 41 | // BlockDevice is a structure with options for creating block devices in a |
| 42 | // server. The block device may be created from an image, snapshot, new volume, |
| 43 | // or existing volume. The destination may be a new volume, existing volume |
| 44 | // which will be attached to the instance, ephemeral disk, or boot device. |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 45 | type BlockDevice struct { |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 46 | // SourceType must be one of: "volume", "snapshot", "image", or "blank". |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 47 | SourceType SourceType `json:"source_type" required:"true"` |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 48 | |
| 49 | // UUID is the unique identifier for the existing volume, snapshot, or |
| 50 | // image (see above). |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 51 | UUID string `json:"uuid,omitempty"` |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 52 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 53 | // BootIndex is the boot index. It defaults to 0. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 54 | BootIndex int `json:"boot_index"` |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 55 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 56 | // DeleteOnTermination specifies whether or not to delete the attached volume |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 57 | // when the server is deleted. Defaults to `false`. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 58 | DeleteOnTermination bool `json:"delete_on_termination"` |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 59 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 60 | // DestinationType is the type that gets created. Possible values are "volume" |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 61 | // and "local". |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 62 | DestinationType DestinationType `json:"destination_type,omitempty"` |
| 63 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 64 | // GuestFormat specifies the format of the block device. |
| 65 | GuestFormat string `json:"guest_format,omitempty"` |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 66 | |
| 67 | // VolumeSize is the size of the volume to create (in gigabytes). This can be |
| 68 | // omitted for existing volumes. |
| 69 | VolumeSize int `json:"volume_size,omitempty"` |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // CreateOptsExt is a structure that extends the server `CreateOpts` structure |
| 73 | // by allowing for a block device mapping. |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 74 | type CreateOptsExt struct { |
| 75 | servers.CreateOptsBuilder |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 76 | BlockDevice []BlockDevice `json:"block_device_mapping_v2,omitempty"` |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // ToServerCreateMap adds the block device mapping option to the base server |
| 80 | // creation options. |
| 81 | func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) { |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 82 | base, err := opts.CreateOptsBuilder.ToServerCreateMap() |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 87 | if len(opts.BlockDevice) == 0 { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 88 | err := gophercloud.ErrMissingInput{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 89 | err.Argument = "bootfromvolume.CreateOptsExt.BlockDevice" |
| 90 | return nil, err |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 91 | } |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 92 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 93 | serverMap := base["server"].(map[string]interface{}) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 94 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 95 | blockDevice := make([]map[string]interface{}, len(opts.BlockDevice)) |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 96 | |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 97 | for i, bd := range opts.BlockDevice { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 98 | b, err := gophercloud.BuildRequestBody(bd, "") |
| 99 | if err != nil { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 100 | return nil, err |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 101 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 102 | blockDevice[i] = b |
Jon Perritt | 01686cd | 2014-10-24 14:10:16 -0500 | [diff] [blame] | 103 | } |
| 104 | serverMap["block_device_mapping_v2"] = blockDevice |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 105 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 106 | return base, nil |
Jon Perritt | 654fb0e | 2014-10-23 20:54:14 -0500 | [diff] [blame] | 107 | } |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 108 | |
| 109 | // Create requests the creation of a server from the given block device mapping. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 110 | func Create(client *gophercloud.ServiceClient, opts servers.CreateOptsBuilder) (r servers.CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 111 | b, err := opts.ToServerCreateMap() |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 112 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 113 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 114 | return |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 115 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 116 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 117 | OkCodes: []int{200, 202}, |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 118 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 119 | return |
Jon Perritt | d9a4bf7 | 2014-10-23 23:44:04 -0500 | [diff] [blame] | 120 | } |