This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
278 lines (239 loc) · 7.67 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
package clusters
import (
"reflect"
"github.com/huaweicloud/golangsdk"
)
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"`
ID string `json:"uuid"`
Type string `json:"type"`
VpcID string `json:"vpc"`
Phase string `json:"phase"`
}
// List returns collection of clusters.
func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Clusters, error) {
var r ListResult
_, r.Err = client.Get(rootURL(client), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
allClusters, err := r.ExtractClusters()
if err != nil {
return nil, err
}
return FilterClusters(allClusters, opts), nil
}
func FilterClusters(clusters []Clusters, opts ListOpts) []Clusters {
var refinedClusters []Clusters
var matched bool
m := map[string]FilterStruct{}
if opts.Name != "" {
m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}}
}
if opts.ID != "" {
m["Id"] = FilterStruct{Value: opts.ID, Driller: []string{"Metadata"}}
}
if opts.Type != "" {
m["Type"] = FilterStruct{Value: opts.Type, Driller: []string{"Spec"}}
}
if opts.VpcID != "" {
m["VpcId"] = FilterStruct{Value: opts.VpcID, Driller: []string{"Spec", "HostNetwork"}}
}
if opts.Phase != "" {
m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}}
}
if len(m) > 0 && len(clusters) > 0 {
for _, cluster := range clusters {
matched = true
for key, value := range m {
if sVal := GetStructNestedField(&cluster, key, value.Driller); !(sVal == value.Value) {
matched = false
}
}
if matched {
refinedClusters = append(refinedClusters, cluster)
}
}
} else {
refinedClusters = clusters
}
return refinedClusters
}
type FilterStruct struct {
Value string
Driller []string
}
func GetStructNestedField(v *Clusters, 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())
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToClusterCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains all the values needed to create a new cluster
type CreateOpts struct {
// API type, fixed value Cluster
Kind string `json:"kind" required:"true"`
// API version, fixed value v3
ApiVersion string `json:"apiversion" required:"true"`
// Metadata required to create a cluster
Metadata CreateMetaData `json:"metadata" required:"true"`
// specifications to create a cluster
Spec Spec `json:"spec" required:"true"`
}
// Metadata required to create a cluster
type CreateMetaData struct {
// Cluster unique name
Name string `json:"name" required:"true"`
// Cluster tag, key/value pair format
Labels map[string]string `json:"labels,omitempty"`
// Cluster annotation, key/value pair format
Annotations map[string]string `json:"annotations,omitempty"`
}
// ToClusterCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToClusterCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create accepts a CreateOpts struct and uses the values to create a new
// logical cluster.
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToClusterCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
return
}
// Get retrieves a particular cluster based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
// GetCert retrieves a particular cluster certificate based on its unique ID.
func GetCert(c *golangsdk.ServiceClient, id string) (r GetCertResult) {
_, r.Err = c.Get(certificateURL(c, id), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
// UpdateOpts contains all the values needed to update a new cluster
type UpdateOpts struct {
Spec UpdateSpec `json:"spec" required:"true"`
}
type UpdateSpec struct {
// Cluster description
Description string `json:"description,omitempty"`
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToClusterUpdateMap() (map[string]interface{}, error)
}
// ToClusterUpdateMap builds an update body based on UpdateOpts.
func (opts UpdateOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update allows clusters to update description.
func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToClusterUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
type DeleteOpts struct {
ErrorStatus string `q:"errorStatus"`
DeleteEfs string `q:"delete_efs"`
DeleteENI string `q:"delete_eni"`
DeleteEvs string `q:"delete_evs"`
DeleteNet string `q:"delete_net"`
DeleteObs string `q:"delete_obs"`
DeleteSfs string `q:"delete_sfs"`
}
type DeleteOptsBuilder interface {
ToDeleteQuery() (string, error)
}
func (opts DeleteOpts) ToDeleteQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
// DeleteWithOpts will permanently delete a particular cluster based on its unique ID,
// and can delete associated resources based on DeleteOpts.
func DeleteWithOpts(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
url := resourceURL(c, id)
if opts != nil {
var query string
query, r.Err = opts.ToDeleteQuery()
if r.Err != nil {
return
}
url += query
}
_, r.Err = c.Delete(url, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
// Delete will permanently delete a particular cluster based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
type UpdateIpOpts struct {
Action string `json:"action" required:"true"`
Spec IpSpec `json:"spec,omitempty"`
ElasticIp string `json:"elasticIp"`
}
type IpSpec struct {
ID string `json:"id" required:"true"`
}
type UpdateIpOptsBuilder interface {
ToMasterIpUpdateMap() (map[string]interface{}, error)
}
func (opts UpdateIpOpts) ToMasterIpUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "spec")
}
// Update the access information of a specified cluster.
func UpdateMasterIp(c *golangsdk.ServiceClient, id string, opts UpdateIpOptsBuilder) (r UpdateIpResult) {
b, err := opts.ToMasterIpUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(masterIpURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
func Operation(c *golangsdk.ServiceClient, id, action string) (r OperationResult) {
_, r.Err = c.Post(operationURL(c, id, action), nil, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}