-
Notifications
You must be signed in to change notification settings - Fork 6
/
requests.go
177 lines (148 loc) · 4.31 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
package vpcs
import (
"net/http"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
)
// CreateOptsBuilder allows extensions to add additional parameters to the
// create request.
type CreateOptsBuilder interface {
ToVPCCreateMap() (map[string]interface{}, error)
}
type CreateOpts struct {
//vpc name
Name string `json:"name" required:"true"`
//cidr,172.16.0.0/12~172.248.255.0/24
Cidr string `json:"cidr" required:"true"`
//mode,SYSTEM or CUSTOMER,SYSTEM: system will design and create subnet when you need;
// CUSTOMER: you should design and create by yourself
Mode string `json:"mode" required:"true"`
}
// ToSecurityGroupsCreateMap converts CreateOpts structures to map[string]interface{}
func (opts CreateOpts) ToVPCCreateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(&opts, "vpc")
if err != nil {
return nil, err
}
return b, nil
}
type UpdateOpts struct {
// Specifies the name of the VPC. The name must be unique for a
// tenant. The value is a string of no more than 64 characters and can contain digits,
// letters, underscores (_), and hyphens (-).
Name string `json:"name,omitempty"`
// Specifies the range of available subnets in the VPC. The value
// must be in CIDR format, for example, 192.168.0.0/16. The value ranges from 10.0.0.0/8
// to 10.255.255.0/24, 172.16.0.0/12 to 172.31.255.0/24, or 192.168.0.0/16 to
// 192.168.255.0/24.
Cidr string `json:"cidr,omitempty"`
}
type UpdateOptsBuilder interface {
ToVPCUpdateMap() (map[string]interface{}, error)
}
func (opts UpdateOpts) ToVPCUpdateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "vpc")
if err != nil {
return nil, err
}
return b, nil
}
// Get get vpc detail
func Get(client *golangsdk.ServiceClient, vpcID string) (r GetResult) {
getURL := GetURL(client, vpcID)
var resp *http.Response
resp, r.Err = client.Get(getURL, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
if r.Err != nil {
return
}
defer resp.Body.Close()
return
}
// Create create vpc
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToVPCCreateMap()
if err != nil {
r.Err = err
return
}
createURL := rootURL(client)
var resp *http.Response
resp, r.Err = client.Post(createURL, b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
if r.Err != nil {
return
}
defer resp.Body.Close()
return
}
//Update update vpc info,name especially
func Update(client *golangsdk.ServiceClient, vpcID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToVPCUpdateMap()
if err != nil {
r.Err = err
return
}
updateURL := UpdateURL(client, vpcID)
var resp *http.Response
resp, r.Err = client.Put(updateURL, b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
if r.Err != nil {
return
}
defer resp.Body.Close()
return
}
//Delete delete the vpc
func Delete(client *golangsdk.ServiceClient, vpcID string) (r DeleteResult) {
deleteURL := DeleteURL(client, vpcID)
var resp *http.Response
resp, r.Err = client.Delete(deleteURL, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusNoContent},
})
if r.Err != nil {
return
}
defer resp.Body.Close()
return
}
// ListOpts allows the filtering collections through the API.
type ListOpts struct {
Limit int `q:"limit"`
Offset int `q:"offset"`
// ID is the unique identifier for the vpc.
ID string `q:"id"`
// Name is the human readable name for the vpc. It does not have to be unique.
Name string `q:"name"`
}
type ListVPCOptsBuilder interface {
ToListVPCQuery() (string, error)
}
// ToListVPCQuery converts ListVPCOpts structures to query string
func (opts ListOpts) ToListVPCQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// List returns collection of vpcs.
// It accepts a ListOpts struct, which allows you to filter
// the returned collection for greater efficiency.
func List(client *golangsdk.ServiceClient, opts ListVPCOptsBuilder) (r ListResult) {
listURL := rootURL(client)
if opts != nil {
query, err := opts.ToListVPCQuery()
if err != nil {
r.Err = err
return
}
listURL += query
}
_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
return
}