-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfirewallpolicies.go
198 lines (179 loc) · 5.68 KB
/
firewallpolicies.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
package oneandone
import (
"net/http"
)
type FirewallPolicy struct {
Identity
descField
DefaultPolicy uint8 `json:"default"`
CreationDate string `json:"creation_date,omitempty"`
State string `json:"state,omitempty"`
Rules []FirewallPolicyRule `json:"rules,omitempty"`
ServerIps []ServerIpInfo `json:"server_ips,omitempty"`
ApiPtr
}
type FirewallPolicyRule struct {
idField
Protocol string `json:"protocol,omitempty"`
PortFrom *int `json:"port_from,omitempty"`
PortTo *int `json:"port_to,omitempty"`
Port string `json:"port,omitempty"`
Action string `json:"action,omitempty"`
Description string `json:"description,omitempty"`
SourceIp string `json:"source,omitempty"`
}
type FirewallPolicyRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Rules []FirewallPolicyRule `json:"rules,omitempty"`
}
// GET /firewall_policies
func (api *API) ListFirewallPolicies(args ...interface{}) ([]FirewallPolicy, error) {
url, err := processQueryParams(createUrl(api, firewallPolicyPathSegment), args...)
if err != nil {
return nil, err
}
result := []FirewallPolicy{}
err = api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
for index, _ := range result {
result[index].api = api
}
return result, nil
}
// POST /firewall_policies
func (api *API) CreateFirewallPolicy(fp_data *FirewallPolicyRequest) (string, *FirewallPolicy, error) {
result := new(FirewallPolicy)
url := createUrl(api, firewallPolicyPathSegment)
err := api.Client.Post(url, &fp_data, &result, http.StatusAccepted)
if err != nil {
return "", nil, err
}
result.api = api
return result.Id, result, nil
}
// GET /firewall_policies/{id}
func (api *API) GetFirewallPolicy(fp_id string) (*FirewallPolicy, error) {
result := new(FirewallPolicy)
url := createUrl(api, firewallPolicyPathSegment, fp_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// DELETE /firewall_policies/{id}
func (api *API) DeleteFirewallPolicy(fp_id string) (*FirewallPolicy, error) {
result := new(FirewallPolicy)
url := createUrl(api, firewallPolicyPathSegment, fp_id)
err := api.Client.Delete(url, nil, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// PUT /firewall_policies/{id}
func (api *API) UpdateFirewallPolicy(fp_id string, fp_new_name string, fp_new_desc string) (*FirewallPolicy, error) {
result := new(FirewallPolicy)
data := FirewallPolicyRequest{
Name: fp_new_name,
Description: fp_new_desc,
}
url := createUrl(api, firewallPolicyPathSegment, fp_id)
err := api.Client.Put(url, &data, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /firewall_policies/{id}/server_ips
func (api *API) ListFirewallPolicyServerIps(fp_id string) ([]ServerIpInfo, error) {
result := []ServerIpInfo{}
url := createUrl(api, firewallPolicyPathSegment, fp_id, "server_ips")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// GET /firewall_policies/{id}/server_ips/{id}
func (api *API) GetFirewallPolicyServerIp(fp_id string, ip_id string) (*ServerIpInfo, error) {
result := new(ServerIpInfo)
url := createUrl(api, firewallPolicyPathSegment, fp_id, "server_ips", ip_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// POST /firewall_policies/{id}/server_ips
func (api *API) AddFirewallPolicyServerIps(fp_id string, ip_ids []string) (*FirewallPolicy, error) {
result := new(FirewallPolicy)
request := serverIps{
ServerIps: ip_ids,
}
url := createUrl(api, firewallPolicyPathSegment, fp_id, "server_ips")
err := api.Client.Post(url, &request, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /firewall_policies/{id}/rules
func (api *API) ListFirewallPolicyRules(fp_id string) ([]FirewallPolicyRule, error) {
result := []FirewallPolicyRule{}
url := createUrl(api, firewallPolicyPathSegment, fp_id, "rules")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// POST /firewall_policies/{id}/rules
func (api *API) AddFirewallPolicyRules(fp_id string, fp_rules []FirewallPolicyRule) (*FirewallPolicy, error) {
result := new(FirewallPolicy)
data := struct {
Rules []FirewallPolicyRule `json:"rules"`
}{fp_rules}
url := createUrl(api, firewallPolicyPathSegment, fp_id, "rules")
err := api.Client.Post(url, &data, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /firewall_policies/{id}/rules/{id}
func (api *API) GetFirewallPolicyRule(fp_id string, rule_id string) (*FirewallPolicyRule, error) {
result := new(FirewallPolicyRule)
url := createUrl(api, firewallPolicyPathSegment, fp_id, "rules", rule_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// DELETE /firewall_policies/{id}/rules/{id}
func (api *API) DeleteFirewallPolicyRule(fp_id string, rule_id string) (*FirewallPolicy, error) {
result := new(FirewallPolicy)
url := createUrl(api, firewallPolicyPathSegment, fp_id, "rules", rule_id)
err := api.Client.Delete(url, nil, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
func (fp *FirewallPolicy) GetState() (string, error) {
in, err := fp.api.GetFirewallPolicy(fp.Id)
if in == nil {
return "", err
}
return in.State, err
}