blob: 6ae553ebe176b4b98b1c11ea708e5126d42f30b9 [file] [log] [blame]
Ash Wilson6935a9b2014-10-23 09:41:22 -04001package diskconfig
2
3import (
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02004 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/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
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 // DiskConfig controls how the rebuilt server's disk is partitioned.
54 DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"`
Ash Wilson6935a9b2014-10-23 09:41:22 -040055}
56
57// ToServerRebuildMap adds the diskconfig option to the base server rebuild options.
58func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060059 if opts.DiskConfig != Auto && opts.DiskConfig != Manual {
60 err := gophercloud.ErrInvalidInput{}
Jon Perrittf094fef2016-03-07 01:41:59 -060061 err.Argument = "diskconfig.RebuildOptsExt.DiskConfig"
62 err.Info = "Must be either diskconfig.Auto or diskconfig.Manual"
Ash Wilson6935a9b2014-10-23 09:41:22 -040063 return nil, err
64 }
65
66 base, err := opts.RebuildOptsBuilder.ToServerRebuildMap()
67 if err != nil {
68 return nil, err
69 }
70
71 serverMap := base["rebuild"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -040072 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -040073
74 return base, nil
75}
76
77// ResizeOptsExt adds a DiskConfig option to the base server resize options.
78type ResizeOptsExt struct {
79 servers.ResizeOptsBuilder
80
81 // DiskConfig [optional] controls how the resized server's disk is partitioned.
82 DiskConfig DiskConfig
83}
84
85// ToServerResizeMap adds the diskconfig option to the base server creation options.
86func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060087 if opts.DiskConfig != Auto && opts.DiskConfig != Manual {
88 err := gophercloud.ErrInvalidInput{}
Jon Perrittf094fef2016-03-07 01:41:59 -060089 err.Argument = "diskconfig.ResizeOptsExt.DiskConfig"
90 err.Info = "Must be either diskconfig.Auto or diskconfig.Manual"
Ash Wilson6935a9b2014-10-23 09:41:22 -040091 return nil, err
92 }
93
94 base, err := opts.ResizeOptsBuilder.ToServerResizeMap()
95 if err != nil {
96 return nil, err
97 }
98
99 serverMap := base["resize"].(map[string]interface{})
Ash Wilson5f14f542014-10-23 10:28:14 -0400100 serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
Ash Wilson6935a9b2014-10-23 09:41:22 -0400101
102 return base, nil
103}