Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 1 | package diskconfig |
| 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" |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // DiskConfig represents one of the two possible settings for the DiskConfig option when creating, |
| 9 | // rebuilding, or resizing servers: Auto or Manual. |
| 10 | type DiskConfig string |
| 11 | |
| 12 | const ( |
| 13 | // Auto builds a server with a single partition the size of the target flavor disk and |
| 14 | // automatically adjusts the filesystem to fit the entire partition. Auto may only be used with |
| 15 | // images and servers that use a single EXT3 partition. |
| 16 | Auto DiskConfig = "AUTO" |
| 17 | |
| 18 | // Manual builds a server using whatever partition scheme and filesystem are present in the source |
| 19 | // image. If the target flavor disk is larger, the remaining space is left unpartitioned. This |
| 20 | // enables images to have non-EXT3 filesystems, multiple partitions, and so on, and enables you |
| 21 | // to manage the disk configuration. It also results in slightly shorter boot times. |
| 22 | Manual DiskConfig = "MANUAL" |
| 23 | ) |
| 24 | |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 25 | // CreateOptsExt adds a DiskConfig option to the base CreateOpts. |
| 26 | type CreateOptsExt struct { |
| 27 | servers.CreateOptsBuilder |
| 28 | |
| 29 | // DiskConfig [optional] controls how the created server's disk is partitioned. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 30 | DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"` |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // ToServerCreateMap adds the diskconfig option to the base server creation options. |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 34 | func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) { |
| 35 | base, err := opts.CreateOptsBuilder.ToServerCreateMap() |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 39 | |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 40 | if string(opts.DiskConfig) == "" { |
| 41 | return base, nil |
| 42 | } |
| 43 | |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 44 | serverMap := base["server"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 45 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 46 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 47 | return base, nil |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // RebuildOptsExt adds a DiskConfig option to the base RebuildOpts. |
| 51 | type RebuildOptsExt struct { |
| 52 | servers.RebuildOptsBuilder |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 53 | // DiskConfig controls how the rebuilt server's disk is partitioned. |
| 54 | DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"` |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // ToServerRebuildMap adds the diskconfig option to the base server rebuild options. |
| 58 | func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 59 | if opts.DiskConfig != Auto && opts.DiskConfig != Manual { |
| 60 | err := gophercloud.ErrInvalidInput{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 61 | err.Argument = "diskconfig.RebuildOptsExt.DiskConfig" |
| 62 | err.Info = "Must be either diskconfig.Auto or diskconfig.Manual" |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | base, err := opts.RebuildOptsBuilder.ToServerRebuildMap() |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | serverMap := base["rebuild"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 72 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 73 | |
| 74 | return base, nil |
| 75 | } |
| 76 | |
| 77 | // ResizeOptsExt adds a DiskConfig option to the base server resize options. |
| 78 | type ResizeOptsExt struct { |
| 79 | servers.ResizeOptsBuilder |
| 80 | |
| 81 | // DiskConfig [optional] controls how the resized server's disk is partitioned. |
| 82 | DiskConfig DiskConfig |
| 83 | } |
| 84 | |
| 85 | // ToServerResizeMap adds the diskconfig option to the base server creation options. |
| 86 | func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 87 | if opts.DiskConfig != Auto && opts.DiskConfig != Manual { |
| 88 | err := gophercloud.ErrInvalidInput{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 89 | err.Argument = "diskconfig.ResizeOptsExt.DiskConfig" |
| 90 | err.Info = "Must be either diskconfig.Auto or diskconfig.Manual" |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | base, err := opts.ResizeOptsBuilder.ToServerResizeMap() |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | |
| 99 | serverMap := base["resize"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 100 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 101 | |
| 102 | return base, nil |
| 103 | } |