forked from bizflycloud/gobizfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_server_firewall.go
299 lines (242 loc) · 8.73 KB
/
cloud_server_firewall.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
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"net/http"
"strings"
)
const (
firewallBasePath = "/firewalls"
)
var _ FirewallService = (*firewall)(nil)
type firewall struct {
client *Client
}
type FirewallService interface {
List(ctx context.Context, opts *ListOptions) ([]*Firewall, error)
Create(ctx context.Context, fcr *FirewallRequestPayload) (*FirewallDetail, error)
Get(ctx context.Context, id string) (*FirewallDetail, error)
Delete(ctx context.Context, id string) (*FirewallDeleteResponse, error)
RemoveServer(ctx context.Context, id string, rsfr *FirewallRemoveServerRequest) (*Firewall, error)
Update(ctx context.Context, id string, ufr *FirewallRequestPayload) (*FirewallDetail, error)
DeleteRule(ctx context.Context, id string) (*FirewallDeleteResponse, error)
CreateRule(ctx context.Context, fwID string, fsrcr *FirewallSingleRuleCreateRequest) (*FirewallRule, error)
}
// BaseFirewall - contains base information fields of a firewall
type BaseFirewall struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Tags []string `json:"tags"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
RevisionNumber int `json:"revision_number"`
ProjectID string `json:"project_id"`
ServersCount int `json:"servers_count"`
RulesCount int `json:"rules_count"`
NetworkInterfaceCount int `json:"network_interface_count"`
InBound []FirewallRule `json:"inbound"`
OutBound []FirewallRule `json:"outbound"`
}
// Firewall - contains information fields of a firewall and the applied servers
type Firewall struct {
BaseFirewall
Servers []string `json:"servers"`
}
// FirewallDetail - contains information fields of a firewall and the applied servers and network interfaces
type FirewallDetail struct {
BaseFirewall
Servers []*Server `json:"servers"`
NetworkInterface []*NetworkInterface `json:"network_interface"`
}
// FirewallRule - contains information fields of a firewall rule
type FirewallRule struct {
ID string `json:"id"`
FirewallID string `json:"security_group_id"`
EtherType string `json:"ethertype"`
Direction string `json:"direction"`
Protocol string `json:"protocol"`
PortRangeMin int `json:"port_range_min"`
PortRangeMax int `json:"port_range_max"`
RemoteIPPrefix string `json:"remote_ip_prefix"`
Description string `json:"description"`
Tags []string `json:"tags"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
RevisionNumber int `json:"revision_number"`
ProjectID string `json:"project_id"`
Type string `json:"type"`
CIDR string `json:"cidr"`
PortRange string `json:"port_range"`
}
// FirewallRuleCreateRequest - payload for creating a firewall rule
type FirewallRuleCreateRequest struct {
Type string `json:"type"`
Protocol string `json:"protocol"`
PortRange string `json:"port_range"`
CIDR string `json:"cidr"`
}
// FirewallSingleRuleCreateRequest - payload for creating a single direction firewall rule
type FirewallSingleRuleCreateRequest struct {
FirewallRuleCreateRequest
Direction string `json:"direction"`
}
// FirewallRequestPayload - payload for creating a firewall
type FirewallRequestPayload struct {
Name string `json:"name"`
InBound []FirewallRuleCreateRequest `json:"inbound,omitempty"`
OutBound []FirewallRuleCreateRequest `json:"outbound,omitempty"`
Targets []string `json:"targets,omitempty"` // Deprecated: This field will be removed in the near future
NetworkInterfaces []string `json:"network_interfaces,omitempty"`
}
// FirewallDeleteResponse represents the response body when deleting a firewall
type FirewallDeleteResponse struct {
Message string `json:"message"`
}
// FirewallRemoveServerRequest represents the request body when removing a server from a firewall
type FirewallRemoveServerRequest struct {
Servers []string `json:"servers"`
}
// FirewallRuleCreateResponse represents the response body when creating a firewall rule
type FirewallRuleCreateResponse struct {
Rule FirewallRule `json:"security_group_rule"`
}
// List lists all firewall.
func (f *firewall) List(ctx context.Context, opts *ListOptions) ([]*Firewall, error) {
req, err := f.client.NewRequest(ctx, http.MethodGet, serverServiceName, firewallBasePath, nil)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var firewalls []*Firewall
if err := json.NewDecoder(resp.Body).Decode(&firewalls); err != nil {
return nil, err
}
return firewalls, nil
}
// Create a firewall.
func (f *firewall) Create(ctx context.Context, fcr *FirewallRequestPayload) (*FirewallDetail, error) {
req, err := f.client.NewRequest(ctx, http.MethodPost, serverServiceName, firewallBasePath, fcr)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var firewall *FirewallDetail
if err := json.NewDecoder(resp.Body).Decode(&firewall); err != nil {
return nil, err
}
return firewall, nil
}
// Get detail a firewall.
func (f *firewall) Get(ctx context.Context, id string) (*FirewallDetail, error) {
req, err := f.client.NewRequest(ctx, http.MethodGet, serverServiceName, firewallBasePath+"/"+id, nil)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var firewall *FirewallDetail
if err := json.NewDecoder(resp.Body).Decode(&firewall); err != nil {
return nil, err
}
return firewall, nil
}
// RemoveServer - Remove applied servers from a firewall.
func (f *firewall) RemoveServer(ctx context.Context, id string, rsfr *FirewallRemoveServerRequest) (*Firewall, error) {
req, err := f.client.NewRequest(ctx, http.MethodDelete, serverServiceName, strings.Join([]string{firewallBasePath, id, "servers"}, "/"), rsfr)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var firewall *Firewall
if err := json.NewDecoder(resp.Body).Decode(&firewall); err != nil {
return nil, err
}
return firewall, nil
}
// Update Firewall
func (f *firewall) Update(ctx context.Context, id string, ufr *FirewallRequestPayload) (*FirewallDetail, error) {
req, err := f.client.NewRequest(ctx, http.MethodPatch, serverServiceName, firewallBasePath+"/"+id, ufr)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var firewall *FirewallDetail
if err := json.NewDecoder(resp.Body).Decode(&firewall); err != nil {
return nil, err
}
return firewall, nil
}
// Delete a Firewall
func (f *firewall) Delete(ctx context.Context, id string) (*FirewallDeleteResponse, error) {
req, err := f.client.NewRequest(ctx, http.MethodDelete, serverServiceName, firewallBasePath+"/"+id, nil)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dwr *FirewallDeleteResponse
if err := json.NewDecoder(resp.Body).Decode(&dwr); err != nil {
return nil, err
}
return dwr, nil
}
// DeleteRule - delete a rule in a firewall
func (f *firewall) DeleteRule(ctx context.Context, id string) (*FirewallDeleteResponse, error) {
req, err := f.client.NewRequest(ctx, http.MethodDelete, serverServiceName, firewallBasePath+"/"+id, nil)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dwr *FirewallDeleteResponse
if err := json.NewDecoder(resp.Body).Decode(&dwr); err != nil {
return nil, err
}
return dwr, nil
}
// CreateRule - create a new rule in a firewall
func (f *firewall) CreateRule(ctx context.Context, fwID string, fsrcr *FirewallSingleRuleCreateRequest) (*FirewallRule, error) {
req, err := f.client.NewRequest(ctx, http.MethodPost, serverServiceName, strings.Join([]string{firewallBasePath, fwID, "rules"}, "/"), fsrcr)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var firewallRuleCreateResponse struct {
Rule *FirewallRule `json:"security_group_rule"`
}
if err := json.NewDecoder(resp.Body).Decode(&firewallRuleCreateResponse); err != nil {
return nil, err
}
return firewallRuleCreateResponse.Rule, nil
}