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