Skip to content

Commit 2b51e3c

Browse files
zliang-akamaiCopilotmgwoj
authored
TPT-4234: Fix firewall device for linode interfaces and add entities field to firewall (#901)
* Fix firewall device for linode interfaces#829 * Fix unit test * Update the unit test * feat: add entities field to Firewall struct and define FirewallEntity type * Fix unit test * Integration test update * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update FirewallDeviceEntity and tests * Reuse FirewallDeviceEntity * fmt * Fix test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Michal Wojcik <32574975+mgwoj@users.noreply.github.com>
1 parent 6a5e955 commit 2b51e3c

12 files changed

Lines changed: 2102 additions & 1146 deletions

firewall_devices.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ func (device *FirewallDevice) UnmarshalJSON(b []byte) error {
5757

5858
// FirewallDeviceEntity contains information about a device associated with a Firewall
5959
type FirewallDeviceEntity struct {
60-
ID int `json:"id"`
61-
Type FirewallDeviceType `json:"type"`
62-
Label string `json:"label"`
63-
URL string `json:"url"`
60+
ID int `json:"id"`
61+
Type FirewallDeviceType `json:"type"`
62+
Label string `json:"label"`
63+
URL string `json:"url"`
64+
ParentEntity *FirewallDeviceEntity `json:"parent_entity"`
6465
}
6566

6667
// ListFirewallDevices get devices associated with a given Firewall

firewalls.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ const (
2020

2121
// A Firewall is a set of networking rules (iptables) applied to Devices with which it is associated
2222
type Firewall struct {
23-
ID int `json:"id"`
24-
Label string `json:"label"`
25-
Status FirewallStatus `json:"status"`
26-
Tags []string `json:"tags,omitempty"`
27-
Rules FirewallRuleSet `json:"rules"`
28-
Created *time.Time `json:"-"`
29-
Updated *time.Time `json:"-"`
23+
ID int `json:"id"`
24+
Label string `json:"label"`
25+
Status FirewallStatus `json:"status"`
26+
Tags []string `json:"tags"`
27+
Rules FirewallRuleSet `json:"rules"`
28+
Entities []FirewallDeviceEntity `json:"entities"`
29+
Created *time.Time `json:"-"`
30+
Updated *time.Time `json:"-"`
3031
}
3132

3233
// DevicesCreationOptions fields are used when adding devices during the Firewall creation process.
3334
type DevicesCreationOptions struct {
34-
Linodes []int `json:"linodes,omitempty"`
35-
NodeBalancers []int `json:"nodebalancers,omitempty"`
36-
Interfaces []int `json:"interfaces,omitempty"`
35+
Linodes []int `json:"linodes,omitempty"`
36+
NodeBalancers []int `json:"nodebalancers,omitempty"`
37+
LinodeInterfaces []int `json:"linode_interfaces,omitempty"`
3738
}
3839

3940
// FirewallCreateOptions fields are those accepted by CreateFirewall

test/integration/firewalls_devices_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ func TestFirewallDevices_List_smoke(t *testing.T) {
2424
}
2525
defer teardownFirewall()
2626

27+
if len(firewall.Entities) != 1 {
28+
t.Errorf("expected exactly 1 firewall entity on create response, got %d", len(firewall.Entities))
29+
}
30+
31+
if len(firewall.Entities) > 0 {
32+
if firewall.Entities[0].Type != linodego.FirewallDeviceLinode {
33+
t.Errorf("expected entity type %q, got %q", linodego.FirewallDeviceLinode, firewall.Entities[0].Type)
34+
}
35+
if firewall.Entities[0].ID != instance.ID {
36+
t.Errorf("expected entity id %d, got %d", instance.ID, firewall.Entities[0].ID)
37+
}
38+
if firewall.Entities[0].ParentEntity != nil {
39+
t.Errorf("expected parent entity to be nil for a linode device, got %+v", firewall.Entities[0].ParentEntity)
40+
}
41+
}
42+
2743
firewallDevices, err := client.ListFirewallDevices(context.Background(), firewall.ID, nil)
2844
if err != nil {
2945
t.Error(err)
@@ -32,6 +48,21 @@ func TestFirewallDevices_List_smoke(t *testing.T) {
3248
if len(firewallDevices) != 1 {
3349
t.Errorf("expected 1 firewall device but got %d", len(firewallDevices))
3450
}
51+
52+
if len(firewallDevices) > 0 {
53+
if firewallDevices[0].Entity.Type != linodego.FirewallDeviceLinode {
54+
t.Errorf("expected device entity type %q, got %q", linodego.FirewallDeviceLinode, firewallDevices[0].Entity.Type)
55+
}
56+
if firewallDevices[0].Entity.ID != instance.ID {
57+
t.Errorf("expected device entity id %d, got %d", instance.ID, firewallDevices[0].Entity.ID)
58+
}
59+
if firewallDevices[0].Entity.Label == "" {
60+
t.Error("expected non-empty device entity label")
61+
}
62+
if firewallDevices[0].Entity.ParentEntity != nil {
63+
t.Errorf("expected parent entity to be nil for a linode device, got %+v", firewallDevices[0].Entity.ParentEntity)
64+
}
65+
}
3566
}
3667

3768
func TestFirewallDevice_Get(t *testing.T) {
@@ -59,6 +90,31 @@ func TestFirewallDevice_Get(t *testing.T) {
5990
t.Error(err)
6091
} else if !cmp.Equal(device, firewallDevice) {
6192
t.Errorf("expected device to match create result but got diffs: %s", cmp.Diff(device, firewallDevice))
93+
} else {
94+
if device.Entity.Label == "" {
95+
t.Error("expected non-empty device entity label")
96+
}
97+
if device.Entity.ParentEntity != nil {
98+
t.Errorf("expected parent entity to be nil for a linode device, got %+v", device.Entity.ParentEntity)
99+
}
100+
}
101+
102+
refreshedFirewall, err := client.GetFirewall(context.Background(), firewall.ID)
103+
if err != nil {
104+
t.Error(err)
105+
}
106+
107+
if len(refreshedFirewall.Entities) != 1 {
108+
t.Errorf("expected exactly 1 firewall entity after device attach, got %d", len(refreshedFirewall.Entities))
109+
}
110+
111+
if len(refreshedFirewall.Entities) > 0 {
112+
if refreshedFirewall.Entities[0].Type != linodego.FirewallDeviceLinode {
113+
t.Errorf("expected entity type %q, got %q", linodego.FirewallDeviceLinode, refreshedFirewall.Entities[0].Type)
114+
}
115+
if refreshedFirewall.Entities[0].ID != instance.ID {
116+
t.Errorf("expected entity id %d, got %d", instance.ID, refreshedFirewall.Entities[0].ID)
117+
}
62118
}
63119
}
64120

test/integration/firewalls_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func TestFirewall_Get(t *testing.T) {
7777
t.Errorf("failed to get newly created firewall %d: %s", created.ID, err)
7878
}
7979

80+
if len(result.Entities) != 0 {
81+
t.Errorf("expected no firewall entities for a newly created firewall, got %d", len(result.Entities))
82+
}
83+
8084
if result.Rules.Inbound[0].Label != rules.Inbound[0].Label {
8185
t.Errorf("Expected firewall rules to be %#v but got %#v", rules, result.Rules)
8286
}
@@ -120,6 +124,10 @@ func TestFirewall_Update(t *testing.T) {
120124
t.Error(err)
121125
}
122126

127+
if len(updated.Entities) != 0 {
128+
t.Errorf("expected no firewall entities after update, got %d", len(updated.Entities))
129+
}
130+
123131
if !cmp.Equal(updated.Tags, *updateOpts.Tags) {
124132
t.Errorf("expected tags to be updated: %s", cmp.Diff(updated.Tags, *updateOpts.Tags))
125133
}

0 commit comments

Comments
 (0)