/
firewall.go
66 lines (60 loc) · 1.6 KB
/
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
package types
// Firewall defines a collection of firewall rules
type Firewall []FirewallRule
// ToMap returns a slice of map[string]string version
func (f Firewall) ToMap() []map[string]string {
var sm []map[string]string
for _, m := range f {
sm = append(sm, map[string]string{
"destination": m.Destination,
"destinationPort": m.DestinationPort,
"protocol": m.Protocol,
})
}
return sm
}
// Eql checks whether another firewall object is equal.
func (f Firewall) Eql(o Firewall) bool {
l := len(f)
found := 0
if l != len(o) {
return false
}
for i := 0; i < l; i++ {
cur := f[i]
for j := 0; j < l; j++ {
oCur := o[j]
if cur.Eql(oCur) {
found++
}
}
}
return found == l
}
// DeDup removes duplicate firewall rules
func (f Firewall) DeDup() Firewall {
newFirewall := Firewall{}
for _, x := range f {
isEql := false
for _, y := range newFirewall {
if x.Eql(y) {
isEql = true
continue
}
}
if !isEql {
newFirewall = append(newFirewall, x)
}
}
return newFirewall
}
// FirewallRule represents information about a destination to allow connections to.
type FirewallRule struct {
Destination string `structs:"destination" mapstructure:"destination,omitempty"`
DestinationPort string `structs:"destinationPort" mapstructure:"destinationPort,omitempty"`
Protocol string `structs:"protocol" mapstructure:"protocol,omitempty"`
}
// Eql returns true if another firewall rule is equal
func (r FirewallRule) Eql(o FirewallRule) bool {
return r.Destination == o.Destination && r.DestinationPort == o.DestinationPort && r.Protocol == o.Protocol
}