/
requests.go
234 lines (207 loc) · 7.65 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package clusters
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// CreateOptsBuilder Builder.
type CreateOptsBuilder interface {
ToClusterCreateMap() (map[string]interface{}, error)
}
// CreateOpts params
type CreateOpts struct {
ClusterTemplateID string `json:"cluster_template_id" required:"true"`
CreateTimeout *int `json:"create_timeout"`
DiscoveryURL string `json:"discovery_url,omitempty"`
DockerVolumeSize *int `json:"docker_volume_size,omitempty"`
FlavorID string `json:"flavor_id,omitempty"`
Keypair string `json:"keypair,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
MasterCount *int `json:"master_count,omitempty"`
MasterFlavorID string `json:"master_flavor_id,omitempty"`
Name string `json:"name"`
NodeCount *int `json:"node_count,omitempty"`
FloatingIPEnabled *bool `json:"floating_ip_enabled,omitempty"`
MasterLBEnabled *bool `json:"master_lb_enabled,omitempty"`
FixedNetwork string `json:"fixed_network,omitempty"`
FixedSubnet string `json:"fixed_subnet,omitempty"`
MergeLabels *bool `json:"merge_labels,omitempty"`
}
// ToClusterCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToClusterCreateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// Create requests the creation of a new cluster.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToClusterCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{202},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Get retrieves a specific clusters based on its unique ID.
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete deletes the specified cluster ID.
func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
resp, err := client.Delete(deleteURL(client, id), nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToClustersListQuery() (string, error)
}
// ListOpts allows the sorting of paginated collections through
// the API. SortKey allows you to sort by a particular cluster attribute.
// SortDir sets the direction, and is either `asc' or `desc'.
// Marker and Limit are used for pagination.
type ListOpts struct {
Marker string `q:"marker"`
Limit int `q:"limit"`
SortKey string `q:"sort_key"`
SortDir string `q:"sort_dir"`
}
// ToClustersListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToClustersListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List returns a Pager which allows you to iterate over a collection of
// clusters. It accepts a ListOptsBuilder, which allows you to sort
// the returned collection for greater efficiency.
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToClustersListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return ClusterPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// ListDetail returns a Pager which allows you to iterate over a collection of
// clusters with detailed information.
// It accepts a ListOptsBuilder, which allows you to sort the returned
// collection for greater efficiency.
func ListDetail(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listDetailURL(c)
if opts != nil {
query, err := opts.ToClustersListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return ClusterPage{pagination.LinkedPageBase{PageResult: r}}
})
}
type UpdateOp string
const (
AddOp UpdateOp = "add"
RemoveOp UpdateOp = "remove"
ReplaceOp UpdateOp = "replace"
)
type UpdateOpts struct {
Op UpdateOp `json:"op" required:"true"`
Path string `json:"path" required:"true"`
Value interface{} `json:"value,omitempty"`
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToClustersUpdateMap() (map[string]interface{}, error)
}
// ToClusterUpdateMap assembles a request body based on the contents of
// UpdateOpts.
func (opts UpdateOpts) ToClustersUpdateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// Update implements cluster updated request.
func Update(client *gophercloud.ServiceClient, id string, opts []UpdateOptsBuilder) (r UpdateResult) {
var o []map[string]interface{}
for _, opt := range opts {
b, err := opt.ToClustersUpdateMap()
if err != nil {
r.Err = err
return r
}
o = append(o, b)
}
resp, err := client.Patch(updateURL(client, id), o, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 202},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
type UpgradeOpts struct {
ClusterTemplate string `json:"cluster_template" required:"true"`
MaxBatchSize *int `json:"max_batch_size,omitempty"`
NodeGroup string `json:"nodegroup,omitempty"`
}
// UpgradeOptsBuilder allows extensions to add additional parameters to the
// Upgrade request.
type UpgradeOptsBuilder interface {
ToClustersUpgradeMap() (map[string]interface{}, error)
}
// ToClustersUpgradeMap constructs a request body from UpgradeOpts.
func (opts UpgradeOpts) ToClustersUpgradeMap() (map[string]interface{}, error) {
if opts.MaxBatchSize == nil {
defaultMaxBatchSize := 1
opts.MaxBatchSize = &defaultMaxBatchSize
}
return gophercloud.BuildRequestBody(opts, "")
}
// Upgrade implements cluster upgrade request.
func Upgrade(client *gophercloud.ServiceClient, id string, opts UpgradeOptsBuilder) (r UpgradeResult) {
b, err := opts.ToClustersUpgradeMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(upgradeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{202},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// ResizeOptsBuilder allows extensions to add additional parameters to the
// Resize request.
type ResizeOptsBuilder interface {
ToClusterResizeMap() (map[string]interface{}, error)
}
// ResizeOpts params
type ResizeOpts struct {
NodeCount *int `json:"node_count" required:"true"`
NodesToRemove []string `json:"nodes_to_remove,omitempty"`
NodeGroup string `json:"nodegroup,omitempty"`
}
// ToClusterResizeMap constructs a request body from ResizeOpts.
func (opts ResizeOpts) ToClusterResizeMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// Resize an existing cluster node count.
func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ResizeResult) {
b, err := opts.ToClusterResizeMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(resizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 202},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}