forked from skippbox/kmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
114 lines (89 loc) · 3.43 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package diskconfig
import (
"errors"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
)
// DiskConfig represents one of the two possible settings for the DiskConfig option when creating,
// rebuilding, or resizing servers: Auto or Manual.
type DiskConfig string
const (
// Auto builds a server with a single partition the size of the target flavor disk and
// automatically adjusts the filesystem to fit the entire partition. Auto may only be used with
// images and servers that use a single EXT3 partition.
Auto DiskConfig = "AUTO"
// Manual builds a server using whatever partition scheme and filesystem are present in the source
// image. If the target flavor disk is larger, the remaining space is left unpartitioned. This
// enables images to have non-EXT3 filesystems, multiple partitions, and so on, and enables you
// to manage the disk configuration. It also results in slightly shorter boot times.
Manual DiskConfig = "MANUAL"
)
// ErrInvalidDiskConfig is returned if an invalid string is specified for a DiskConfig option.
var ErrInvalidDiskConfig = errors.New("DiskConfig must be either diskconfig.Auto or diskconfig.Manual.")
// Validate ensures that a DiskConfig contains an appropriate value.
func (config DiskConfig) validate() error {
switch config {
case Auto, Manual:
return nil
default:
return ErrInvalidDiskConfig
}
}
// CreateOptsExt adds a DiskConfig option to the base CreateOpts.
type CreateOptsExt struct {
servers.CreateOptsBuilder
// DiskConfig [optional] controls how the created server's disk is partitioned.
DiskConfig DiskConfig `json:"OS-DCF:diskConfig,omitempty"`
}
// ToServerCreateMap adds the diskconfig option to the base server creation options.
func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
base, err := opts.CreateOptsBuilder.ToServerCreateMap()
if err != nil {
return nil, err
}
if string(opts.DiskConfig) == "" {
return base, nil
}
serverMap := base["server"].(map[string]interface{})
serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
return base, nil
}
// RebuildOptsExt adds a DiskConfig option to the base RebuildOpts.
type RebuildOptsExt struct {
servers.RebuildOptsBuilder
// DiskConfig [optional] controls how the rebuilt server's disk is partitioned.
DiskConfig DiskConfig
}
// ToServerRebuildMap adds the diskconfig option to the base server rebuild options.
func (opts RebuildOptsExt) ToServerRebuildMap() (map[string]interface{}, error) {
err := opts.DiskConfig.validate()
if err != nil {
return nil, err
}
base, err := opts.RebuildOptsBuilder.ToServerRebuildMap()
if err != nil {
return nil, err
}
serverMap := base["rebuild"].(map[string]interface{})
serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
return base, nil
}
// ResizeOptsExt adds a DiskConfig option to the base server resize options.
type ResizeOptsExt struct {
servers.ResizeOptsBuilder
// DiskConfig [optional] controls how the resized server's disk is partitioned.
DiskConfig DiskConfig
}
// ToServerResizeMap adds the diskconfig option to the base server creation options.
func (opts ResizeOptsExt) ToServerResizeMap() (map[string]interface{}, error) {
err := opts.DiskConfig.validate()
if err != nil {
return nil, err
}
base, err := opts.ResizeOptsBuilder.ToServerResizeMap()
if err != nil {
return nil, err
}
serverMap := base["resize"].(map[string]interface{})
serverMap["OS-DCF:diskConfig"] = string(opts.DiskConfig)
return base, nil
}