Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 1 | package diskconfig |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 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. |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 44 | DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"` |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 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 | |
Jon Perritt | 8dd49db | 2014-10-24 12:39:07 -0500 | [diff] [blame] | 54 | if string(opts.DiskConfig) == "" { |
| 55 | return base, nil |
| 56 | } |
| 57 | |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 58 | serverMap := base["server"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 59 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 60 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 61 | return base, nil |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // RebuildOptsExt adds a DiskConfig option to the base RebuildOpts. |
| 65 | type RebuildOptsExt struct { |
| 66 | servers.RebuildOptsBuilder |
| 67 | |
| 68 | // DiskConfig [optional] controls how the rebuilt server's disk is partitioned. |
| 69 | DiskConfig DiskConfig |
| 70 | } |
| 71 | |
| 72 | // ToServerRebuildMap adds the diskconfig option to the base server rebuild options. |
| 73 | func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) { |
| 74 | err := opts.DiskConfig.validate() |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | |
| 79 | base, err := opts.RebuildOptsBuilder.ToServerRebuildMap() |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | |
| 84 | serverMap := base["rebuild"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 85 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 86 | |
| 87 | return base, nil |
| 88 | } |
| 89 | |
| 90 | // ResizeOptsExt adds a DiskConfig option to the base server resize options. |
| 91 | type ResizeOptsExt struct { |
| 92 | servers.ResizeOptsBuilder |
| 93 | |
| 94 | // DiskConfig [optional] controls how the resized server's disk is partitioned. |
| 95 | DiskConfig DiskConfig |
| 96 | } |
| 97 | |
| 98 | // ToServerResizeMap adds the diskconfig option to the base server creation options. |
| 99 | func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) { |
| 100 | err := opts.DiskConfig.validate() |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | base, err := opts.ResizeOptsBuilder.ToServerResizeMap() |
| 106 | if err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | serverMap := base["resize"].(map[string]interface{}) |
Ash Wilson | 5f14f54 | 2014-10-23 10:28:14 -0400 | [diff] [blame] | 111 | serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig) |
Ash Wilson | 6935a9b | 2014-10-23 09:41:22 -0400 | [diff] [blame] | 112 | |
| 113 | return base, nil |
| 114 | } |