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
137 lines (118 loc) · 3.98 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
/*
Copyright (c) Huawei Technologies Co. Ltd. 2021. All rights reserved.
*/
package premium_instances
import "github.com/huaweicloud/golangsdk"
// requestOpts defines the request header and language.
var requestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// CreateInstanceOpts the parameters in the creating request body
type CreateInstanceOpts struct {
Region string `json:"region" required:"true"`
ChargeMode int `json:"chargemode" required:"true"`
AvailableZone string `json:"available_zone" required:"true"`
Arch string `json:"arch" required:"true"`
NamePrefix string `json:"instancename" required:"true"`
Specification string `json:"specification" required:"true"`
CpuFlavor string `json:"cpu_flavor" required:"true"`
VpcId string `json:"vpc_id" required:"true"`
SubnetId string `json:"subnet_id" required:"true"`
SecurityGroup []string `json:"security_group" required:"true"`
Count int `json:"count" required:"true"`
Ipv6Enable string `json:"ipv6_enable,omitempty"`
VolumeType string `json:"volume_type,omitempty"`
ClusterId string `json:"cluster_id,omitempty"`
PoolId string `json:"pool_id,omitempty"`
}
// ListInstanceOpts the parameters in the querying request.
type ListInstanceOpts struct {
Page int `q:"page"`
PageSize int `q:"pagesize"`
InstanceName string `q:"instancename"`
}
// UpdateInstanceOpts the parameters in the updating request.
type UpdateInstanceOpts struct {
InstanceName string `json:"instancename"`
}
// CreateInstance will create a dedicated waf instance based on the values in CreateOpts.
func CreateInstance(c *golangsdk.ServiceClient, opts CreateInstanceOpts) (*CreationgRst, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var rst golangsdk.Result
_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r CreationgRst
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// GetInstance get the waf instance detail.
func GetInstance(c *golangsdk.ServiceClient, id string) (*DedicatedInstance, error) {
var rst golangsdk.Result
_, err := c.Get(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r DedicatedInstance
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// ListInstance query a list of waf instance base on ListInstanceOpts
func ListInstance(c *golangsdk.ServiceClient, opts ListInstanceOpts) (*DedicatedInstanceList, error) {
url := rootURL(c)
query, err := golangsdk.BuildQueryString(opts)
if err != nil {
return nil, err
}
url += query.String()
var rst golangsdk.Result
_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r DedicatedInstanceList
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// UpdateInstance query a list of waf instance base on UpdateInstanceOpts
func UpdateInstance(c *golangsdk.ServiceClient, id string, opts UpdateInstanceOpts) (*DedicatedInstance, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var rst golangsdk.Result
_, err = c.Put(resourceURL(c, id), b, &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r DedicatedInstance
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// Delete will permanently delete a engine based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string) (*DedicatedInstance, error) {
var rst golangsdk.Result
_, err := c.DeleteWithResponse(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders},
)
if err == nil {
var r DedicatedInstance
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}