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
170 lines (143 loc) · 4.26 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
package firewalls
import (
"net/http"
"github.com/huaweicloud/golangsdk"
)
type CreateOpts struct {
Name string `json:"name,true"`
Description string `json:"description,omitempty"`
}
type CreateOptsBuilder interface {
ToFirewallCreateMap() (map[string]interface{}, error)
}
func (opts CreateOpts) ToFirewallCreateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(&opts, "firewall")
if err != nil {
return nil, err
}
return b, nil
}
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToFirewallCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
return
}
func Delete(client *golangsdk.ServiceClient, firewallID string) (r DeleteResult) {
url := DeleteURL(client, firewallID)
_, r.Err = client.Delete(url, nil)
return
}
func Get(client *golangsdk.ServiceClient, fwID string) (r GetResult) {
url := GetURL(client, fwID)
_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
return
}
type ReqSubnet struct {
ID string `json:"id"`
VpcID string `json:"vpc_id"`
}
type UpdateOpts struct {
Name string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
AdminStateUp *bool `json:"admin_state_up,omitempty"`
Subnets *[]ReqSubnet `json:"subnets,omitempty"`
}
type UpdateOptsBuilder interface {
ToUpdateFirewallMap() (map[string]interface{}, error)
}
func (opts UpdateOpts) ToUpdateFirewallMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "firewall")
if err != nil {
return nil, err
}
return b, nil
}
func Update(client *golangsdk.ServiceClient, fwID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToUpdateFirewallMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(UpdateURL(client, fwID), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
return
}
type ReqFirewallRulesOpts struct {
ID string `json:"id,omitempty"`
Description string `json:"description"`
Enabled *bool `json:"enabled,omitempty"`
Name string `json:"name"`
Protocol string `json:"protocol"`
Action string `json:"action"`
IPVersion int `json:"ip_version,omitempty"`
DstIPAddr string `json:"destination_ip_address"`
DstPort string `json:"destination_port"`
SrcIPAddr string `json:"source_ip_address"`
SrcPort string `json:"source_port"`
OperateType string `json:"operate_type"`
}
type ReqPolicyOpts struct {
PolicyID string `json:"id"`
FirewallRules *[]ReqFirewallRulesOpts `json:"firewall_rules,omitempty"`
}
type UpdateRuleOpts struct {
ReqFirewallOutPolicy *ReqPolicyOpts `json:"egress_firewall_policy,omitempty"`
ReqFirewallInPolicy *ReqPolicyOpts `json:"ingress_firewall_policy,omitempty"`
}
type UpdateRuleOptsBuilder interface {
ToUpdateFirewallRuleMap() (map[string]interface{}, error)
}
func (opts UpdateRuleOpts) ToUpdateFirewallRuleMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "firewall")
if err != nil {
return nil, err
}
return b, nil
}
func UpdateRule(client *golangsdk.ServiceClient, fwID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) {
b, err := opts.ToUpdateFirewallRuleMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(UpdateRuleURL(client, fwID), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
return
}
type ListOpts struct {
Limit int `q:"limit"`
Offset int `q:"offset"`
ID string `q:"id"`
Name string `q:"name"`
}
type ListFirewallsOptsBuilder interface {
ToListFirewallsQuery() (string, error)
}
func (opts ListOpts) ToListFirewallsQuery() (string, error) {
b, err := golangsdk.BuildQueryString(&opts)
if err != nil {
return "", err
}
return b.String(), nil
}
func List(client *golangsdk.ServiceClient, opts ListFirewallsOptsBuilder) (r ListResult) {
listURL := rootURL(client)
if opts != nil {
query, err := opts.ToListFirewallsQuery()
if err != nil {
r.Err = err
return r
}
listURL += query
}
_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{http.StatusOK},
})
return
}