-
Notifications
You must be signed in to change notification settings - Fork 6
/
requests.go
369 lines (313 loc) · 10.4 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package nodes
import (
"reflect"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/common/tags"
)
var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}
// ListOpts allows the filtering of list data using given parameters.
type ListOpts struct {
Name string `json:"name"`
Uid string `json:"uid"`
Phase string `json:"phase"`
}
// List returns collection of nodes.
func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]Nodes, error) {
var r ListResult
_, r.Err = client.Get(rootURL(client, clusterID), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
allNodes, err := r.ExtractNode()
if err != nil {
return nil, err
}
return FilterNodes(allNodes, opts), nil
}
func FilterNodes(nodes []Nodes, opts ListOpts) []Nodes {
var refinedNodes []Nodes
var matched bool
m := map[string]FilterStruct{}
if opts.Name != "" {
m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}}
}
if opts.Uid != "" {
m["Id"] = FilterStruct{Value: opts.Uid, Driller: []string{"Metadata"}}
}
if opts.Phase != "" {
m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}}
}
if len(m) > 0 && len(nodes) > 0 {
for _, nodes := range nodes {
matched = true
for key, value := range m {
if sVal := GetStructNestedField(&nodes, key, value.Driller); !(sVal == value.Value) {
matched = false
}
}
if matched {
refinedNodes = append(refinedNodes, nodes)
}
}
} else {
refinedNodes = nodes
}
return refinedNodes
}
func GetStructNestedField(v *Nodes, field string, structDriller []string) string {
r := reflect.ValueOf(v)
for _, drillField := range structDriller {
f := reflect.Indirect(r).FieldByName(drillField).Interface()
r = reflect.ValueOf(f)
}
f1 := reflect.Indirect(r).FieldByName(field)
return string(f1.String())
}
type FilterStruct struct {
Value string
Driller []string
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOpts struct {
// API type, fixed value Node
Kind string `json:"kind" required:"true"`
// API version, fixed value v3
ApiVersion string `json:"apiversion" required:"true"`
// Metadata required to create a Node
Metadata CreateMetaData `json:"metadata"`
// specifications to create a Node
Spec Spec `json:"spec" required:"true"`
}
// Metadata required to create a Node
type CreateMetaData struct {
// Node name
Name string `json:"name,omitempty"`
// Node tag, key value pair format
Labels map[string]string `json:"labels,omitempty"`
// Node annotation, key value pair format
Annotations map[string]string `json:"annotations,omitempty"`
}
// Create accepts a CreateOpts struct and uses the values to create a new
// logical Node. When it is created, the Node does not have an internal
// interface
type CreateOptsBuilder interface {
ToNodeCreateMap() (map[string]interface{}, error)
}
// ToNodeCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create accepts a CreateOpts struct and uses the values to create a new
// logical node.
func Create(c *golangsdk.ServiceClient, clusterid string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToNodeCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
_, r.Err = c.Post(rootURL(c, clusterid), b, &r.Body, reqOpt)
return
}
type AddOpts struct {
// API type, fixed value List
Kind string `json:"kind" required:"true"`
// API version, fixed value v3
ApiVersion string `json:"apiversion" required:"true"`
// List of nodes to add
NodeList []AddNode `json:"nodeList" required:"true"`
}
type AddNode struct {
ServerID string `json:"serverID" required:"true"`
Spec AddNodeSpec `json:"spec" required:"true"`
}
type AddNodeSpec struct {
// The OS of the node
Os string `json:"os" required:"true"`
// Node login parameters
Login LoginSpec `json:"login" required:"true"`
//Node name
Name string `json:"name,omitempty"`
// ECS server config of the node
ServerConfig *ServerConfig `json:"serverConfig,omitempty"`
// Volume management config of the node
VolumeConfig *VolumeConfig `json:"volumeConfig,omitempty"`
// Runtime config of the node
RuntimeConfig *RuntimeConfig `json:"runtimeConfig,omitempty"`
// Kubernetes options of the node
K8sOptions *K8sOptions `json:"k8sOptions,omitempty"`
// Custom lifecycle config of the node
Lifecycle *Lifecycle `json:"lifecycle,omitempty"`
// Extended parameter
ExtendParam map[string]interface{} `json:"extendParam,omitempty"`
}
type ServerConfig struct {
// Tag of a VM, key value pair format
UserTags []tags.ResourceTag `json:"userTags,omitempty"`
// System disk parameter of the node
RootVolume *RootVolume
}
type RootVolume struct {
// Custom image ID
ImageID string `json:"imageID,omitempty"`
// User master key ID, default to empty, means the disk in not encrypted
CmkID string `json:"cmkID,omitempty"`
}
type VolumeConfig struct {
// Docker data disk configurations
LvmConfig string `json:"lvmConfig,omitempty"`
}
type RuntimeConfig struct {
// The available disk space of a single Docker container on the node in device mapper mode
DockerBaseSize int `json:"dockerBaseSize,omitempty"`
// The runtime spec
Runtime *RunTimeSpec `json:"runtime,omitempty"`
}
type K8sOptions struct {
// Tag of a Kubernetes node, key value pair format
Labels map[string]string `json:"labels,omitempty"`
// taints to created nodes to configure anti-affinity
Taints []TaintSpec `json:"taints,omitempty"`
// The maximum number of pods allowed to be created on a node
MaxPods int `json:"maxPods,omitempty"`
// NIC queue number configuration
NicMultiQueue string `json:"nicMultiqueue,omitempty"`
// NIC pre-binding ratio configuration
NicThreshold string `json:"nicThreshold,omitempty"`
}
type Lifecycle struct {
// Preinstall script
Preinstall string `json:"preInstall,omitempty"`
// Postinstall script
PostInstall string `json:"postInstall,omitempty"`
}
type AddOptsBuilder interface {
ToNodeAddMap() (map[string]interface{}, error)
}
func (opts AddOpts) ToNodeAddMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func Add(c *golangsdk.ServiceClient, clusterid string, opts AddOptsBuilder) (r AddResult) {
b, err := opts.ToNodeAddMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Post(addNodeURL(c, clusterid), b, &r.Body, reqOpt)
return
}
type ResetOpts struct {
// API type, fixed value List
Kind string `json:"kind" required:"true"`
// API version, fixed value v3
ApiVersion string `json:"apiversion" required:"true"`
// List of nodes to reset
NodeList []ResetNode `json:"nodeList" required:"true"`
}
type ResetNode struct {
NodeID string `json:"nodeID" required:"true"`
Spec AddNodeSpec `json:"spec" required:"true"`
}
type ResetOptsBuilder interface {
ToNodeResetMap() (map[string]interface{}, error)
}
func (opts ResetOpts) ToNodeResetMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func Reset(c *golangsdk.ServiceClient, clusterid string, opts ResetOptsBuilder) (r AddResult) {
b, err := opts.ToNodeResetMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(resetNodeURL(c, clusterid), b, &r.Body, nil)
return
}
// Get retrieves a particular nodes based on its unique ID and cluster ID.
func Get(c *golangsdk.ServiceClient, clusterid, nodeid string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, clusterid, nodeid), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToNodeUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contains all the values needed to update a new node
type UpdateOpts struct {
Metadata UpdateMetadata `json:"metadata,omitempty"`
}
type UpdateMetadata struct {
Name string `json:"name,omitempty"`
}
// ToNodeUpdateMap builds an update body based on UpdateOpts.
func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update allows nodes to be updated.
func Update(c *golangsdk.ServiceClient, clusterid, nodeid string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToNodeUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(resourceURL(c, clusterid, nodeid), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete will permanently delete a particular node based on its unique ID and cluster ID.
func Delete(c *golangsdk.ServiceClient, clusterid, nodeid string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, clusterid, nodeid), &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
type RemoveOptsBuilder interface {
ToNodeRemoveMap() (map[string]interface{}, error)
}
type RemoveOpts struct {
// API type, fixed value RemoveNodesTask
Kind string `json:"kind,omitempty"`
// API version, fixed value v3
Apiversion string `json:"apiVersion,omitempty"`
Spec RemoveNodeSpec `json:"spec" required:"true"`
}
type RemoveNodeSpec struct {
Login LoginSpec `json:"login" required:"true"`
Nodes []NodeItem `json:"nodes,omitempty"`
}
type NodeItem struct {
Uid string `json:"uid,omitempty"`
}
func (opts RemoveOpts) ToNodeRemoveMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func Remove(c *golangsdk.ServiceClient, clusterid string, opts RemoveOptsBuilder) (r DeleteResult) {
b, err := opts.ToNodeRemoveMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(removeNodeURL(c, clusterid), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
// GetJobDetails retrieves a particular job based on its unique ID
func GetJobDetails(c *golangsdk.ServiceClient, jobid string) (r GetResult) {
_, r.Err = c.Get(getJobURL(c, jobid), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}