/
firewall_policies.go
111 lines (101 loc) · 3.6 KB
/
firewall_policies.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
package brightbox
import (
"time"
)
// FirewallPolicy represents a firewall policy.
// https://api.gb1.brightbox.com/1.0/#firewall_policy
type FirewallPolicy struct {
Id string
Name string
Default bool
CreatedAt time.Time `json:"created_at"`
Description string
ServerGroup *ServerGroup `json:"server_group"`
Rules []FirewallRule `json:"rules"`
}
// FirewallPolicyOptions is used in conjunction with CreateFirewallPolicy and
// UpdateFirewallPolicy to create and update firewall policies.
type FirewallPolicyOptions struct {
Id string `json:"-"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
ServerGroup *string `json:"server_group,omitempty"`
}
// FirewallPolicies retrieves a list of all firewall policies
func (c *Client) FirewallPolicies() ([]FirewallPolicy, error) {
var policies []FirewallPolicy
_, err := c.MakeApiRequest("GET", "/1.0/firewall_policies", nil, &policies)
if err != nil {
return nil, err
}
return policies, err
}
// FirewallPolicy retrieves a detailed view of one firewall policy
func (c *Client) FirewallPolicy(identifier string) (*FirewallPolicy, error) {
policy := new(FirewallPolicy)
_, err := c.MakeApiRequest("GET", "/1.0/firewall_policies/"+identifier, nil, policy)
if err != nil {
return nil, err
}
return policy, err
}
// CreateFirewallPolicy creates a new firewall policy.
//
// It takes a FirewallPolicyOptions struct for specifying name and other
// attributes. Not all attributes can be specified at create time (such as Id,
// which is allocated for you)
func (c *Client) CreateFirewallPolicy(policyOptions *FirewallPolicyOptions) (*FirewallPolicy, error) {
policy := new(FirewallPolicy)
_, err := c.MakeApiRequest("POST", "/1.0/firewall_policies", policyOptions, &policy)
if err != nil {
return nil, err
}
return policy, nil
}
// UpdateFirewallPolicy updates an existing firewall policy.
//
// It takes a FirewallPolicyOptions struct for specifying name and other
// attributes. Not all attributes can be update(such as server_group which is
// instead changed with ApplyFirewallPolicy).
//
// Specify the policy you want to update using the Id field
func (c *Client) UpdateFirewallPolicy(policyOptions *FirewallPolicyOptions) (*FirewallPolicy, error) {
policy := new(FirewallPolicy)
_, err := c.MakeApiRequest("PUT", "/1.0/firewall_policies/"+policyOptions.Id, policyOptions, &policy)
if err != nil {
return nil, err
}
return policy, nil
}
// DestroyFirewallPolicy issues a request to destroy the firewall policy
func (c *Client) DestroyFirewallPolicy(identifier string) error {
_, err := c.MakeApiRequest("DELETE", "/1.0/firewall_policies/"+identifier, nil, nil)
if err != nil {
return err
}
return nil
}
// ApplyFirewallPolicy issues a request to apply the given firewall policy to
// the given server group.
//
func (c *Client) ApplyFirewallPolicy(policyId string, serverGroupId string) (*FirewallPolicy, error) {
policy := new(FirewallPolicy)
_, err := c.MakeApiRequest("POST", "/1.0/firewall_policies/"+policyId+"/apply_to",
map[string]string{"server_group": serverGroupId}, &policy)
if err != nil {
return nil, err
}
return policy, nil
}
// RemoveFirewallPolicy issues a request to remove the given firewall policy from
// the given server group.
//
func (c *Client) RemoveFirewallPolicy(policyId string, serverGroupId string) (*FirewallPolicy, error) {
policy := new(FirewallPolicy)
_, err := c.MakeApiRequest("POST", "/1.0/firewall_policies/"+policyId+"/remove",
map[string]string{"server_group": serverGroupId}, &policy)
if err != nil {
return nil, err
}
return policy, nil
}