blob: cc390f73d217c9f9ce4ea52bb740c50e4bf2b968 [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.
Jon Perritt4149d7c2014-10-23 21:23:46 -050048func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
49 base, err := opts.CreateOptsBuilder.ToServerCreateMap()
50 if err != nil {
51 return nil, err
52 }
Ash Wilson6935a9b2014-10-23 09:41:22 -040053
54 serverMap := base["server"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040055 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040056
Jon Perritt4149d7c2014-10-23 21:23:46 -050057 return base, nil
Ash Wilson6935a9b2014-10-23 09:41:22 -040058}
59
60// RebuildOptsExt adds a DiskConfig option to the base RebuildOpts.
61type 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.
69func (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 Wilson5f14f542014-10-23 10:28:14 -040081 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040082
83 return base, nil
84}
85
86// ResizeOptsExt adds a DiskConfig option to the base server resize options.
87type 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.
95func (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 Wilson5f14f542014-10-23 10:28:14 -0400107 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -0400108
109 return base, nil
110}