-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
300 lines (265 loc) · 10.6 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
package instances
import (
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/pagination"
)
// CreateOpts allows to create an APIG dedicated instance using given parameters.
type CreateOpts struct {
// Name of the APIG dedicated instance. The name can contains of 3 to 64 characters.
Name string `json:"instance_name" required:"true"`
// Edition of the APIG dedicated instance. Currently, the editions are support:
// (IPv4): BASIC, PROFESSIONAL, ENTERPRISE, PLATINUM
// (IPv6): BASIC_IPV6, PROFESSIONAL_IPV6, ENTERPRISE_IPV6, PLATINUM_IPV6
Edition string `json:"spec_id" required:"true"`
// VPC ID.
VpcId string `json:"vpc_id" required:"true"`
// Subnet network ID.
SubnetId string `json:"subnet_id" required:"true"`
// ID of the security group to which the APIG dedicated instance belongs to.
SecurityGroupId string `json:"security_group_id" required:"true"`
// ID of the APIG dedicated instance, which will be automatically generated if you do not specify this parameter.
Id string `json:"instance_id,omitempty"`
// Description about the APIG dedicated instance.
Description string `json:"description,omitempty"`
// Start time of the maintenance time window in the format "xx:00:00".
// The value of xx can be 02, 06, 10, 14, 18, or 22.
MaintainBegin string `json:"maintain_begin,omitempty"`
// End time of the maintenance time window in the format "xx:00:00".
// There is a 4-hour difference between the start time and end time.
MaintainEnd string `json:"maintain_end,omitempty"`
// EIP ID.
EipId string `json:"eip_id,omitempty"`
// Outbound access bandwidth. This parameter is required if public outbound access is enabled for the APIG
// dedicated instance.
// Zero means turn off the egress access.
BandwidthSize int `json:"bandwidth_size"`
// Enterprise project ID. This parameter is required if you are using an enterprise account.
EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
// AZs.
AvailableZoneIds []string `json:"available_zone_ids,omitempty"`
// Whether public access with an IPv6 address is supported.
Ipv6Enable bool `json:"ipv6_enable,omitempty"`
// The type of load balancer used by the instance.
// The valid values are as follows:
// + lvs: Linux virtual server
// + elb: Elastic load balance
LoadbalancerProvider string `json:"loadbalancer_provider,omitempty"`
}
type CreateOptsBuilder interface {
ToInstanceCreateMap() (map[string]interface{}, error)
}
func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create is a method by which to create function that create a APIG dedicated instance.
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
reqBody, err := opts.ToInstanceCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client), reqBody, &r.Body, nil)
return
}
// Get is a method to obtain the specified APIG dedicated instance according to the instance Id.
func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
return
}
// ListOpts allows to filter list data using given parameters.
type ListOpts struct {
// ID of the APIG dedicated instance.
Id string `q:"instance_id"`
// Name of the APIG dedicated instance.
Name string `q:"instance_name"`
// Instance status.
Status string `q:"status"`
// Offset from which the query starts.
// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
Offset int `q:"offset"`
// Number of items displayed on each page.
Limit int `q:"limit"`
}
type ListOptsBuilder interface {
ToInstanceListQuery() (string, error)
}
func (opts ListOpts) ToInstanceListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// List is a method to obtain an array of one or more APIG dedicated instance according to the query parameters.
func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := rootURL(client)
if opts != nil {
query, err := opts.ToInstanceListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return InstancePage{pagination.SinglePageBase(r)}
})
}
// UpdateOpts allows to update an existing APIG dedicated instance using given parameters.
type UpdateOpts struct {
// Description about the APIG dedicated instance.
Description *string `json:"description,omitempty"`
// Start time of the maintenance time window in the format "xx:00:00".
// The value of xx can be 02, 06, 10, 14, 18, or 22.
MaintainBegin string `json:"maintain_begin,omitempty"`
// End time of the maintenance time window in the format "xx:00:00".
// There is a 4-hour difference between the start time and end time.
MaintainEnd string `json:"maintain_end,omitempty"`
// Description about the APIG dedicated instance.
Name string `json:"instance_name,omitempty"`
// ID of the security group to which the APIG dedicated instance belongs to.
SecurityGroupId string `json:"security_group_id,omitempty"`
}
type UpdateOptsBuilder interface {
ToInstanceUpdateMap() (map[string]interface{}, error)
}
func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update is a method by which to update an existing APIG dedicated instance.
func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
reqBody, err := opts.ToInstanceUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resourceURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete is a method to delete an existing APIG dedicated instance
func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = client.Delete(resourceURL(client, id), nil)
return
}
// EgressAccessOpts allows the bandwidth size of an existing APIG dedicated instance to be updated with the given
// parameters.
type EgressAccessOpts struct {
// Outbound access bandwidth, in Mbit/s.
BandwidthSize string `json:"bandwidth_size,omitempty"`
}
type EgressAccessOptsBuilder interface {
ToEgressAccessMap() (map[string]interface{}, error)
}
func (opts EgressAccessOpts) ToEgressAccessMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// EnableEgressAccess is a method by which to enable the egress access of an existing APIG dedicated instance.
func EnableEgressAccess(client *golangsdk.ServiceClient, id string, opts EgressAccessOptsBuilder) (r EnableEgressResult) {
reqBody, err := opts.ToEgressAccessMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(egressURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 201},
})
return
}
// UpdateEgressBandwidth is a method by which to update the egress bandwidth size of an existing APIG dedicated instance.
func UpdateEgressBandwidth(client *golangsdk.ServiceClient, id string, opts EgressAccessOptsBuilder) (r UdpateEgressResult) {
reqBody, err := opts.ToEgressAccessMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(egressURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// DisableEgressAccess is a method by which to disable the egress access of an existing APIG dedicated instance.
func DisableEgressAccess(client *golangsdk.ServiceClient, id string) (r DisableEgressResult) {
_, r.Err = client.Delete(egressURL(client, id), nil)
return
}
// IngressAccessOpts allows binding and updating the eip associated with an existing APIG dedicated instance with the
// given parameters.
type IngressAccessOpts struct {
// EIP ID
EipId string `json:"eip_id,omitempty"`
}
type IngressAccessOptsBuilder interface {
ToEnableIngressAccessMap() (map[string]interface{}, error)
}
func (opts IngressAccessOpts) ToEnableIngressAccessMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// UpdateIngressAccess is a method to bind and update the eip associated with an existing APIG dedicated instance.
func EnableIngressAccess(client *golangsdk.ServiceClient, id string, opts IngressAccessOptsBuilder) (r EnableIngressResult) {
reqBody, err := opts.ToEnableIngressAccessMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(ingressURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// DisableIngressAccess is a method to unbind the eip associated with an existing APIG dedicated instance.
func DisableIngressAccess(client *golangsdk.ServiceClient, id string) (r DisableIngressResult) {
_, r.Err = client.Delete(ingressURL(client, id), &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// FeatureOpts allows to update the dedicated APIG instance features.
type FeatureOpts struct {
// Feature name.
Name string `json:"name" required:"true"`
// Whether to enable the feature.
Enable *bool `json:"enable" required:"true"`
// Parameter configuration.
Config string `json:"config,omitempty"`
}
var requestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// UpdateFeature is a method used to update the feature configuration.
func UpdateFeature(c *golangsdk.ServiceClient, instanceId string, opts FeatureOpts) (*Feature, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var r Feature
_, err = c.Post(featureURL(c, instanceId), b, &r, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return &r, err
}
type ListFeaturesOpts struct {
// Offset from which the query starts.
// If the offset is less than 0, the value is automatically converted to 0. Defaults to 0.
Offset int `q:"offset"`
// Number of items displayed on each page.
// Defaults to 20. The maximum value is 500.
Limit int `q:"limit"`
}
// ListFeatures is a method used to obtain the list of feature configuration details.
func ListFeatures(c *golangsdk.ServiceClient, instanceId string, opts ListFeaturesOpts) ([]Feature, error) {
url := featureURL(c, instanceId)
query, err := golangsdk.BuildQueryString(opts)
if err != nil {
return nil, err
}
url += query.String()
pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := FeaturePage{pagination.OffsetPageBase{PageResult: r}}
return p
}).AllPages()
if err != nil {
return nil, err
}
return ExtractFeatures(pages)
}