/
firewall_rules.go
80 lines (73 loc) · 2.78 KB
/
firewall_rules.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
package brightbox
import (
"time"
)
// FirewallRule represents a firewall rule.
// https://api.gb1.brightbox.com/1.0/#firewall_rule
type FirewallRule struct {
Id string
Source string `json:"source"`
SourcePort string `json:"source_port"`
Destination string `json:"destination"`
DestinationPort string `json:"destination_port"`
Protocol string `json:"protocol"`
IcmpTypeName string `json:"icmp_type_name"`
CreatedAt time.Time `json:"created_at"`
Description string `json:"description"`
FirewallPolicy FirewallPolicy `json:"firewall_policy"`
}
// FirewallRuleOptions is used in conjunction with CreateFirewallRule and
// UpdateFirewallRule to create and update firewall rules.
type FirewallRuleOptions struct {
Id string `json:"-"`
FirewallPolicy string `json:"firewall_policy,omitempty"`
Protocol *string `json:"protocol,omitempty"`
Source *string `json:"source,omitempty"`
SourcePort *string `json:"source_port,omitempty"`
Destination *string `json:"destination,omitempty"`
DestinationPort *string `json:"destination_port,omitempty"`
IcmpTypeName *string `json:"icmp_type_name,omitempty"`
Description *string `json:"description,omitempty"`
}
// FirewallRule retrieves a detailed view of one firewall rule
func (c *Client) FirewallRule(identifier string) (*FirewallRule, error) {
rule := new(FirewallRule)
_, err := c.MakeApiRequest("GET", "/1.0/firewall_rules/"+identifier, nil, rule)
if err != nil {
return nil, err
}
return rule, err
}
// CreateFirewallRule creates a new firewall rule.
//
// It takes a FirewallRuleOptions 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) CreateFirewallRule(ruleOptions *FirewallRuleOptions) (*FirewallRule, error) {
rule := new(FirewallRule)
_, err := c.MakeApiRequest("POST", "/1.0/firewall_rules", ruleOptions, &rule)
if err != nil {
return nil, err
}
return rule, nil
}
// UpdateFirewallRule updates an existing firewall rule.
//
// It takes a FirewallRuleOptions struct for specifying the attributes. Not all
// attributes can be updated (such as firewall_policy)
func (c *Client) UpdateFirewallRule(ruleOptions *FirewallRuleOptions) (*FirewallRule, error) {
rule := new(FirewallRule)
_, err := c.MakeApiRequest("PUT", "/1.0/firewall_rules/"+ruleOptions.Id, ruleOptions, &rule)
if err != nil {
return nil, err
}
return rule, nil
}
// DestroyFirewallRule destroys an existing firewall rule
func (c *Client) DestroyFirewallRule(identifier string) error {
_, err := c.MakeApiRequest("DELETE", "/1.0/firewall_rules/"+identifier, nil, nil)
if err != nil {
return err
}
return nil
}