blob: 19798d7288936b174e9f8f763827963f2e501431 [file] [log] [blame]
Ash Wilson6935a9b2014-10-23 09:41:22 -04001package diskconfig
2
3import (
Jon Perrittf094fef2016-03-07 01:41:59 -06004 "github.com/gophercloud/gophercloud"
Jon Perritt27249f42016-02-18 10:35:59 -06005 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Ash Wilson6935a9b2014-10-23 09:41:22 -04006)
7
8// DiskConfig represents one of the two possible settings for the DiskConfig option when creating,
9// rebuilding, or resizing servers: Auto or Manual.
10type DiskConfig string
11
12const (
13 // Auto builds a server with a single partition the size of the target flavor disk and
14 // automatically adjusts the filesystem to fit the entire partition. Auto may only be used with
15 // images and servers that use a single EXT3 partition.
16 Auto DiskConfig = "AUTO"
17
18 // Manual builds a server using whatever partition scheme and filesystem are present in the source
19 // image. If the target flavor disk is larger, the remaining space is left unpartitioned. This
20 // enables images to have non-EXT3 filesystems, multiple partitions, and so on, and enables you
21 // to manage the disk configuration. It also results in slightly shorter boot times.
22 Manual DiskConfig = "MANUAL"
23)
24
Ash Wilson6935a9b2014-10-23 09:41:22 -040025// CreateOptsExt adds a DiskConfig option to the base CreateOpts.
26type CreateOptsExt struct {
27 servers.CreateOptsBuilder
28
29 // DiskConfig [optional] controls how the created server's disk is partitioned.
Jon Perritt8dd49db2014-10-24 12:39:07 -050030 DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"`
Ash Wilson6935a9b2014-10-23 09:41:22 -040031}
32
33// ToServerCreateMap adds the diskconfig option to the base server creation options.
Jon Perritt4149d7c2014-10-23 21:23:46 -050034func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
35 base, err := opts.CreateOptsBuilder.ToServerCreateMap()
36 if err != nil {
37 return nil, err
38 }
Ash Wilson6935a9b2014-10-23 09:41:22 -040039
Jon Perritt8dd49db2014-10-24 12:39:07 -050040 if string(opts.DiskConfig) == "" {
41 return base, nil
42 }
43
Ash Wilson6935a9b2014-10-23 09:41:22 -040044 serverMap := base["server"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040045 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040046
Jon Perritt4149d7c2014-10-23 21:23:46 -050047 return base, nil
Ash Wilson6935a9b2014-10-23 09:41:22 -040048}
49
50// RebuildOptsExt adds a DiskConfig option to the base RebuildOpts.
51type RebuildOptsExt struct {
52 servers.RebuildOptsBuilder
53
54 // DiskConfig [optional] controls how the rebuilt server's disk is partitioned.
55 DiskConfig DiskConfig
56}
57
58// ToServerRebuildMap adds the diskconfig option to the base server rebuild options.
59func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060060 if opts.DiskConfig != Auto && opts.DiskConfig != Manual {
61 err := gophercloud.ErrInvalidInput{}
62 err.Function = "diskconfig.ToServerRebuildMap"
63 err.Argument = "diskconfig.RebuildOptsExt.DiskConfig"
64 err.Info = "Must be either diskconfig.Auto or diskconfig.Manual"
Ash Wilson6935a9b2014-10-23 09:41:22 -040065 return nil, err
66 }
67
68 base, err := opts.RebuildOptsBuilder.ToServerRebuildMap()
69 if err != nil {
70 return nil, err
71 }
72
73 serverMap := base["rebuild"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040074 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040075
76 return base, nil
77}
78
79// ResizeOptsExt adds a DiskConfig option to the base server resize options.
80type ResizeOptsExt struct {
81 servers.ResizeOptsBuilder
82
83 // DiskConfig [optional] controls how the resized server's disk is partitioned.
84 DiskConfig DiskConfig
85}
86
87// ToServerResizeMap adds the diskconfig option to the base server creation options.
88func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060089 if opts.DiskConfig != Auto && opts.DiskConfig != Manual {
90 err := gophercloud.ErrInvalidInput{}
91 err.Function = "diskconfig.ToServerResizeMap"
92 err.Argument = "diskconfig.ResizeOptsExt.DiskConfig"
93 err.Info = "Must be either diskconfig.Auto or diskconfig.Manual"
Ash Wilson6935a9b2014-10-23 09:41:22 -040094 return nil, err
95 }
96
97 base, err := opts.ResizeOptsBuilder.ToServerResizeMap()
98 if err != nil {
99 return nil, err
100 }
101
102 serverMap := base["resize"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -0400103 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -0400104
105 return base, nil
106}