blob: 3fd9c9e0adbb0933846649a26818d511b383dd49 [file] [log] [blame]
Ash Wilson6935a9b2014-10-23 09:41:22 -04001package diskconfig
2
3import (
4 "errors"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Ash Wilson6935a9b2014-10-23 09:41:22 -04007)
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.
Jon Perritt8dd49db2014-10-24 12:39:07 -050044 DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"`
Ash Wilson6935a9b2014-10-23 09:41:22 -040045}
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
Jon Perritt8dd49db2014-10-24 12:39:07 -050054 if string(opts.DiskConfig) == "" {
55 return base, nil
56 }
57
Ash Wilson6935a9b2014-10-23 09:41:22 -040058 serverMap := base["server"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040059 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040060
Jon Perritt4149d7c2014-10-23 21:23:46 -050061 return base, nil
Ash Wilson6935a9b2014-10-23 09:41:22 -040062}
63
64// RebuildOptsExt adds a DiskConfig option to the base RebuildOpts.
65type 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.
73func (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 Wilson5f14f542014-10-23 10:28:14 -040085 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040086
87 return base, nil
88}
89
90// ResizeOptsExt adds a DiskConfig option to the base server resize options.
91type 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.
99func (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 Wilson5f14f542014-10-23 10:28:14 -0400111 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -0400112
113 return base, nil
114}