Skip to content

Commit 266d29d

Browse files
implementation
1 parent 52759a1 commit 266d29d

7 files changed

+58
-19
lines changed

linode_api4/groups/networking.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
VLAN,
55
Base,
66
Firewall,
7+
FirewallTemplate,
78
Instance,
89
IPAddress,
910
IPv6Pool,
@@ -94,6 +95,21 @@ def firewall_create(self, label, rules, **kwargs):
9495
f = Firewall(self.client, result["id"], result)
9596
return f
9697

98+
def firewall_templates(self, *filters):
99+
"""
100+
Returns a list of Firewall Templates available to the current user.
101+
102+
API Documentation: Not yet available.
103+
104+
:param filters: Any number of filters to apply to this query.
105+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
106+
for more details on filtering.
107+
108+
:returns: A list of Firewall Templates available to the current user.
109+
:rtype: PaginatedList of FirewallTemplate
110+
"""
111+
return self.client._get_and_filter(FirewallTemplate, *filters)
112+
97113
def ips(self, *filters):
98114
"""
99115
Returns a list of IP addresses on this account, excluding private addresses.

linode_api4/objects/networking.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ def device_create(self, id, type="linode", **kwargs):
307307
return c
308308

309309

310+
class FirewallTemplate(Base):
311+
"""
312+
Represents a single Linode Firewall template.
313+
314+
API documentation: Not yet available.
315+
"""
316+
317+
api_endpoint = "/networking/firewalls/templates/{slug}"
318+
319+
id_attribute = "slug"
320+
321+
properties = {"slug": Property(identifier=True), "rules": Property()}
322+
323+
310324
class NetworkTransferPrice(Base):
311325
"""
312326
An NetworkTransferPrice represents the structure of a valid network transfer price.

test/unit/groups/networking_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from test.unit.base import ClientBaseCase
2+
from test.unit.objects.firewall_test import FirewallTemplatesTest
3+
4+
5+
class NetworkingGroupTest(ClientBaseCase):
6+
"""
7+
Tests methods under the NetworkingGroup class.
8+
"""
9+
10+
def test_get_templates(self):
11+
templates = self.client.networking.firewall_templates()
12+
13+
assert templates[0].slug == "public"
14+
FirewallTemplatesTest.assert_rules(templates[0].rules)
15+
16+
assert templates[1].slug == "vpc"
17+
FirewallTemplatesTest.assert_rules(templates[1].rules)

test/unit/objects/firewall_test.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from test.unit.base import ClientBaseCase
22

3-
from linode_api4 import MappedObject
3+
from linode_api4 import FirewallTemplate, MappedObject
44
from linode_api4.objects import Firewall, FirewallDevice
55

66

@@ -85,7 +85,8 @@ def test_get_device(self):
8585

8686

8787
class FirewallTemplatesTest(ClientBaseCase):
88-
def _assert_rules(self, rules: MappedObject):
88+
@staticmethod
89+
def assert_rules(rules: MappedObject):
8990
assert rules.outbound_policy == "DROP"
9091
assert len(rules.outbound) == 1
9192

@@ -95,8 +96,8 @@ def _assert_rules(self, rules: MappedObject):
9596
outbound_rule = rules.outbound[0]
9697
assert outbound_rule.action == "ACCEPT"
9798
assert outbound_rule.addresses.ipv4[0] == "192.0.2.0/24"
98-
assert outbound_rule.addresses.ipv4[0] == "198.51.100.2/32"
99-
assert outbound_rule.addresses.ipv6[0] == "2001::DB8::/128"
99+
assert outbound_rule.addresses.ipv4[1] == "198.51.100.2/32"
100+
assert outbound_rule.addresses.ipv6[0] == "2001:DB8::/128"
100101
assert outbound_rule.description == "test"
101102
assert outbound_rule.label == "test-rule"
102103
assert outbound_rule.ports == "22-24, 80, 443"
@@ -105,28 +106,19 @@ def _assert_rules(self, rules: MappedObject):
105106
inbound_rule = rules.outbound[0]
106107
assert inbound_rule.action == "ACCEPT"
107108
assert inbound_rule.addresses.ipv4[0] == "192.0.2.0/24"
108-
assert inbound_rule.addresses.ipv4[0] == "198.51.100.2/32"
109-
assert inbound_rule.addresses.ipv6[0] == "2001::DB8::/128"
109+
assert inbound_rule.addresses.ipv4[1] == "198.51.100.2/32"
110+
assert inbound_rule.addresses.ipv6[0] == "2001:DB8::/128"
110111
assert inbound_rule.description == "test"
111112
assert inbound_rule.label == "test-rule"
112113
assert inbound_rule.ports == "22-24, 80, 443"
113114
assert inbound_rule.protocol == "TCP"
114115

115-
def test_get_templates(self):
116-
templates = self.client.networking.firewall_templates()
117-
118-
assert templates[0].slug == "public"
119-
self._assert_rules(templates[0].rules)
120-
121-
assert templates[1].slug == "vpc"
122-
self._assert_rules(templates[1].rules)
123-
124-
def test_get_template_public(self):
116+
def test_get_public(self):
125117
template = self.client.load(FirewallTemplate, "public")
126118
assert template.slug == "public"
127-
self._assert_rules(template.rules)
119+
self.assert_rules(template.rules)
128120

129-
def test_get_template_vpc(self):
121+
def test_get_vpc(self):
130122
template = self.client.load(FirewallTemplate, "vpc")
131123
assert template.slug == "vpc"
132-
self._assert_rules(template.rules)
124+
self.assert_rules(template.rules)

0 commit comments

Comments
 (0)