/
requests.go
124 lines (100 loc) · 3.42 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
115
116
117
118
119
120
121
122
123
124
package schedulerhints
import (
"regexp"
"github.com/gophercloud/gophercloud"
)
// SchedulerHints represents a set of scheduling hints that are passed to the
// OpenStack scheduler.
type SchedulerHints struct {
// DifferentHost will place the volume on a different back-end that does not
// host the given volumes.
DifferentHost []string
// SameHost will place the volume on a back-end that hosts the given volumes.
SameHost []string
// LocalToInstance will place volume on same host on a given instance
LocalToInstance string
// Query is a conditional statement that results in back-ends able to
// host the volume.
Query string
// AdditionalProperies are arbitrary key/values that are not validated by nova.
AdditionalProperties map[string]interface{}
}
// VolumeCreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type VolumeCreateOptsBuilder interface {
ToVolumeCreateMap() (map[string]interface{}, error)
}
// CreateOptsBuilder builds the scheduler hints into a serializable format.
type CreateOptsBuilder interface {
ToVolumeSchedulerHintsCreateMap() (map[string]interface{}, error)
}
// ToVolumeSchedulerHintsMap builds the scheduler hints into a serializable format.
func (opts SchedulerHints) ToVolumeSchedulerHintsCreateMap() (map[string]interface{}, error) {
sh := make(map[string]interface{})
uuidRegex, _ := regexp.Compile("^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$")
if len(opts.DifferentHost) > 0 {
for _, diffHost := range opts.DifferentHost {
if !uuidRegex.MatchString(diffHost) {
err := gophercloud.ErrInvalidInput{}
err.Argument = "schedulerhints.SchedulerHints.DifferentHost"
err.Value = opts.DifferentHost
err.Info = "The hosts must be in UUID format."
return nil, err
}
}
sh["different_host"] = opts.DifferentHost
}
if len(opts.SameHost) > 0 {
for _, sameHost := range opts.SameHost {
if !uuidRegex.MatchString(sameHost) {
err := gophercloud.ErrInvalidInput{}
err.Argument = "schedulerhints.SchedulerHints.SameHost"
err.Value = opts.SameHost
err.Info = "The hosts must be in UUID format."
return nil, err
}
}
sh["same_host"] = opts.SameHost
}
if opts.LocalToInstance != "" {
if !uuidRegex.MatchString(opts.LocalToInstance) {
err := gophercloud.ErrInvalidInput{}
err.Argument = "schedulerhints.SchedulerHints.LocalToInstance"
err.Value = opts.LocalToInstance
err.Info = "The instance must be in UUID format."
return nil, err
}
sh["local_to_instance"] = opts.LocalToInstance
}
if opts.Query != "" {
sh["query"] = opts.Query
}
if opts.AdditionalProperties != nil {
for k, v := range opts.AdditionalProperties {
sh[k] = v
}
}
return sh, nil
}
// CreateOptsExt adds a SchedulerHints option to the base CreateOpts.
type CreateOptsExt struct {
VolumeCreateOptsBuilder
// SchedulerHints provides a set of hints to the scheduler.
SchedulerHints CreateOptsBuilder
}
// ToVolumeCreateMap adds the SchedulerHints option to the base volume creation options.
func (opts CreateOptsExt) ToVolumeCreateMap() (map[string]interface{}, error) {
base, err := opts.VolumeCreateOptsBuilder.ToVolumeCreateMap()
if err != nil {
return nil, err
}
schedulerHints, err := opts.SchedulerHints.ToVolumeSchedulerHintsCreateMap()
if err != nil {
return nil, err
}
if len(schedulerHints) == 0 {
return base, nil
}
base["OS-SCH-HNT:scheduler_hints"] = schedulerHints
return base, nil
}