Skip to content

Commit f500ba6

Browse files
authored
Merge pull request #140 from hetznercloud/add-description-field-to-firewall-rules
Add description field to firewall rules
2 parents 647cd79 + 84dc5c7 commit f500ba6

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

hcloud/firewalls/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, client, data, complete=True):
1414
rules = data.get('rules', [])
1515
if rules:
1616
rules = [FirewallRule(direction=rule["direction"], source_ips=rule["source_ips"],
17-
destination_ips=rule["destination_ips"], protocol=rule['protocol'], port=rule["port"])
17+
destination_ips=rule["destination_ips"], protocol=rule['protocol'], port=rule["port"], description=rule["description"])
1818
for rule in rules]
1919
data['rules'] = rules
2020

hcloud/firewalls/domain.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ class FirewallRule:
6060
List of permitted IPv4/IPv6 addresses in CIDR notation. Use 0.0.0.0/0 to allow all IPv4 addresses and ::/0 to allow all IPv6 addresses. You can specify 100 CIDRs at most.
6161
:param destination_ips: List[str]
6262
List of permitted IPv4/IPv6 addresses in CIDR notation. Use 0.0.0.0/0 to allow all IPv4 addresses and ::/0 to allow all IPv6 addresses. You can specify 100 CIDRs at most.
63+
:param description: str
64+
Short description of the firewall rule
6365
"""
6466
__slots__ = (
6567
"direction",
6668
"port",
6769
"protocol",
6870
"source_ips",
69-
"destination_ips"
71+
"destination_ips",
72+
"description"
7073
)
7174

7275
DIRECTION_IN = "in"
@@ -92,12 +95,14 @@ def __init__(
9295
source_ips, # type: List[str]
9396
port=None, # type: Optional[str]
9497
destination_ips=None, # type: Optional[List[str]]
98+
description=None, # type: Optional[str]
9599
):
96100
self.direction = direction
97101
self.port = port
98102
self.protocol = protocol
99103
self.source_ips = source_ips
100104
self.destination_ips = destination_ips or []
105+
self.description = description
101106

102107
def to_payload(self):
103108
payload = {
@@ -109,6 +114,8 @@ def to_payload(self):
109114
payload.update({"destination_ips": self.destination_ips})
110115
if self.port is not None:
111116
payload.update({"port": self.port})
117+
if self.description is not None:
118+
payload.update({"description": self.description})
112119
return payload
113120

114121

tests/unit/firewalls/conftest.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def response_create_firewall():
1919
],
2020
"destination_ips": [],
2121
"protocol": "tcp",
22-
"port": "80"
22+
"port": "80",
23+
"description": None
2324
},
2425
{
2526
"direction": "out",
@@ -30,7 +31,8 @@ def response_create_firewall():
3031
"ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128"
3132
],
3233
"protocol": "tcp",
33-
"port": "80"
34+
"port": "80",
35+
"description": "allow http out"
3436
}
3537
],
3638
"applied_to": [
@@ -111,7 +113,8 @@ def firewall_response():
111113
],
112114
"destination_ips": [],
113115
"protocol": "tcp",
114-
"port": "80"
116+
"port": "80",
117+
"description": "allow http in"
115118
},
116119
{
117120
"direction": "out",
@@ -122,7 +125,8 @@ def firewall_response():
122125
"ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128"
123126
],
124127
"protocol": "tcp",
125-
"port": "80"
128+
"port": "80",
129+
"description": "allow http out"
126130
}
127131
],
128132
"applied_to": [
@@ -162,7 +166,8 @@ def two_firewalls_response():
162166
],
163167
"destination_ips": [],
164168
"protocol": "tcp",
165-
"port": "80"
169+
"port": "80",
170+
"description": "allow http in"
166171
}
167172
],
168173
"applied_to": [
@@ -189,7 +194,8 @@ def two_firewalls_response():
189194
"ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128"
190195
],
191196
"protocol": "tcp",
192-
"port": "443"
197+
"port": "443",
198+
"description": "allow https in"
193199
}
194200
],
195201
"applied_to": [
@@ -224,7 +230,8 @@ def one_firewalls_response():
224230
"ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128"
225231
],
226232
"protocol": "tcp",
227-
"port": "80"
233+
"port": "80",
234+
"description": "allow http in"
228235
}
229236
],
230237
"applied_to": [
@@ -258,7 +265,8 @@ def response_update_firewall():
258265
],
259266
"destination_ips": [],
260267
"protocol": "tcp",
261-
"port": "80"
268+
"port": "80",
269+
"description": "allow http in"
262270
}
263271
],
264272
"applied_to": [

tests/unit/firewalls/test_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_bound_firewall_init(self, firewall_response):
4646
]
4747
assert isinstance(firewall_in_rule.destination_ips, list)
4848
assert len(firewall_in_rule.destination_ips) == 0
49+
assert firewall_in_rule.description == "allow http in"
4950

5051
firewall_out_rule = bound_firewall.rules[1]
5152
assert isinstance(firewall_out_rule, FirewallRule)
@@ -61,6 +62,7 @@ def test_bound_firewall_init(self, firewall_response):
6162
"28.239.14.0/24",
6263
"ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128"
6364
]
65+
assert firewall_out_rule.description == "allow http out"
6466

6567
@pytest.mark.parametrize(
6668
"params",
@@ -123,9 +125,9 @@ def test_set_rules(self, hetzner_client, bound_firewall, response_set_rules):
123125
hetzner_client.request.return_value = response_set_rules
124126
actions = bound_firewall.set_rules([
125127
FirewallRule(direction=FirewallRule.DIRECTION_IN, protocol=FirewallRule.PROTOCOL_ICMP,
126-
source_ips=["0.0.0.0/0", "::/0"])])
128+
source_ips=["0.0.0.0/0", "::/0"], description="New firewall description")])
127129
hetzner_client.request.assert_called_with(url="/firewalls/1/actions/set_rules", method="POST", json={
128-
"rules": [{"direction": "in", "protocol": "icmp", "source_ips": ["0.0.0.0/0", "::/0"]}]})
130+
"rules": [{"direction": "in", "protocol": "icmp", "source_ips": ["0.0.0.0/0", "::/0"], "description": "New firewall description"}]})
129131

130132
assert actions[0].id == 13
131133
assert actions[0].progress == 100

0 commit comments

Comments
 (0)