Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 1 | package diskconfig |
| 2 | |
| 3 | import ( |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 4 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 5 | "github.com/gophercloud/gophercloud/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 |
| 53 | |
| 54 | // DiskConfig [optional] controls how the rebuilt server's disk is partitioned. |
| 55 | DiskConfig DiskConfig |
| 56 | } |
| 57 | |
| 58 | // ToServerRebuildMap adds the diskconfig option to the base server rebuild options. |
| 59 | func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 60 | if opts.DiskConfig != Auto && opts.DiskConfig != Manual { |
| 61 | err := gophercloud.ErrInvalidInput{} |
| 62 | err.Function = "diskconfig.ToServerRebuildMap" |
| 63 | err.Argument = "diskconfig.RebuildOptsExt.DiskConfig" |
| 64 | err.Info = "Must be either diskconfig.Auto or diskconfig.Manual" |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | base, err := opts.RebuildOptsBuilder.ToServerRebuildMap() |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | serverMap := base["rebuild"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 74 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 75 | |
| 76 | return base, nil |
| 77 | } |
| 78 | |
| 79 | // ResizeOptsExt adds a DiskConfig option to the base server resize options. |
| 80 | type ResizeOptsExt struct { |
| 81 | servers.ResizeOptsBuilder |
| 82 | |
| 83 | // DiskConfig [optional] controls how the resized server's disk is partitioned. |
| 84 | DiskConfig DiskConfig |
| 85 | } |
| 86 | |
| 87 | // ToServerResizeMap adds the diskconfig option to the base server creation options. |
| 88 | func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 89 | if opts.DiskConfig != Auto && opts.DiskConfig != Manual { |
| 90 | err := gophercloud.ErrInvalidInput{} |
| 91 | err.Function = "diskconfig.ToServerResizeMap" |
| 92 | err.Argument = "diskconfig.ResizeOptsExt.DiskConfig" |
| 93 | err.Info = "Must be either diskconfig.Auto or diskconfig.Manual" |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | base, err := opts.ResizeOptsBuilder.ToServerResizeMap() |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | serverMap := base["resize"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 103 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 104 | |
| 105 | return base, nil |
| 106 | } |