blob: 06a922acb2474a56803b257ddf841b03747a5c45 [file] [log] [blame]
Ash Wilson6935a9b2014-10-23 09:41:22 -04001package diskconfig
2
3import (
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.
11type DiskConfig string
12
13const (
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.
27var ErrInvalidDiskConfig = errors.New("DiskConfig must be either diskconfig.Auto or diskconfig.Manual.")
28
29// Validate ensures that a DiskConfig contains an appropriate value.
30func (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.
40type 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.
48func (opts CreateOptsExt) ToServerCreateMap() map[string]interface{} {
49 base := opts.CreateOptsBuilder.ToServerCreateMap()
50
51 serverMap := base["server"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040052 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040053
54 return base
55}
56
57// RebuildOptsExt adds a DiskConfig option to the base RebuildOpts.
58type RebuildOptsExt struct {
59 servers.RebuildOptsBuilder
60
61 // DiskConfig [optional] controls how the rebuilt server's disk is partitioned.
62 DiskConfig DiskConfig
63}
64
65// ToServerRebuildMap adds the diskconfig option to the base server rebuild options.
66func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) {
67 err := opts.DiskConfig.validate()
68 if err != nil {
69 return nil, err
70 }
71
72 base, err := opts.RebuildOptsBuilder.ToServerRebuildMap()
73 if err != nil {
74 return nil, err
75 }
76
77 serverMap := base["rebuild"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040078 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040079
80 return base, nil
81}
82
83// ResizeOptsExt adds a DiskConfig option to the base server resize options.
84type ResizeOptsExt struct {
85 servers.ResizeOptsBuilder
86
87 // DiskConfig [optional] controls how the resized server's disk is partitioned.
88 DiskConfig DiskConfig
89}
90
91// ToServerResizeMap adds the diskconfig option to the base server creation options.
92func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) {
93 err := opts.DiskConfig.validate()
94 if err != nil {
95 return nil, err
96 }
97
98 base, err := opts.ResizeOptsBuilder.ToServerResizeMap()
99 if err != nil {
100 return nil, err
101 }
102
103 serverMap := base["resize"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -0400104 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -0400105
106 return base, nil
107}