Skip to content

Commit

Permalink
feat(firewalls): add applied_to_resources to FirewallResource (#297)
Browse files Browse the repository at this point in the history
Add the read-only `applied_to_resources` field to `FirewallResource`

https://docs.hetzner.cloud/#firewalls-get-a-firewall
  • Loading branch information
jooola authored Sep 25, 2023
1 parent 951dbf3 commit 55d2b20
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions hcloud/firewalls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
CreateFirewallResponse,
Firewall,
FirewallResource,
FirewallResourceAppliedToResources,
FirewallResourceLabelSelector,
FirewallRule,
)
22 changes: 22 additions & 0 deletions hcloud/firewalls/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
CreateFirewallResponse,
Firewall,
FirewallResource,
FirewallResourceAppliedToResources,
FirewallResourceLabelSelector,
FirewallRule,
)
Expand Down Expand Up @@ -44,6 +45,24 @@ def __init__(self, client: FirewallsClient, data: dict, complete: bool = True):

data_applied_to = []
for firewall_resource in applied_to:
applied_to_resources = None
if firewall_resource.get("applied_to_resources"):
applied_to_resources = [
FirewallResourceAppliedToResources(
type=resource["type"],
server=(
BoundServer(
client._client.servers,
resource.get("server"),
complete=False,
)
if resource.get("server") is not None
else None
),
)
for resource in firewall_resource.get("applied_to_resources")
]

if firewall_resource["type"] == FirewallResource.TYPE_SERVER:
data_applied_to.append(
FirewallResource(
Expand All @@ -53,6 +72,7 @@ def __init__(self, client: FirewallsClient, data: dict, complete: bool = True):
firewall_resource["server"],
complete=False,
),
applied_to_resources=applied_to_resources,
)
)
elif firewall_resource["type"] == FirewallResource.TYPE_LABEL_SELECTOR:
Expand All @@ -62,8 +82,10 @@ def __init__(self, client: FirewallsClient, data: dict, complete: bool = True):
label_selector=FirewallResourceLabelSelector(
selector=firewall_resource["label_selector"]["selector"]
),
applied_to_resources=applied_to_resources,
)
)

data["applied_to"] = data_applied_to

super().__init__(client, data, complete)
Expand Down
24 changes: 23 additions & 1 deletion hcloud/firewalls/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ class FirewallResource:
Server the Firewall is applied to
:param label_selector: Optional[FirewallResourceLabelSelector]
Label Selector for Servers the Firewall should be applied to
:param applied_to_resources: (read-only) List of effective resources the firewall is
applied to.
"""

__slots__ = ("type", "server", "label_selector")
__slots__ = ("type", "server", "label_selector", "applied_to_resources")

TYPE_SERVER = "server"
"""Firewall Used By Type Server"""
Expand All @@ -148,10 +150,12 @@ def __init__(
type: str,
server: Server | BoundServer | None = None,
label_selector: FirewallResourceLabelSelector | None = None,
applied_to_resources: list[FirewallResourceAppliedToResources] | None = None,
):
self.type = type
self.server = server
self.label_selector = label_selector
self.applied_to_resources = applied_to_resources

def to_payload(self) -> dict[str, Any]:
"""
Expand All @@ -166,6 +170,24 @@ def to_payload(self) -> dict[str, Any]:
return payload


class FirewallResourceAppliedToResources(BaseDomain):
"""Firewall Resource applied to Domain
:param type: Type of resource referenced
:param server: Server the Firewall is applied to
"""

__slots__ = ("type", "server")

def __init__(
self,
type: str,
server: BoundServer | None = None,
):
self.type = type
self.server = server


class FirewallResourceLabelSelector(BaseDomain):
"""FirewallResourceLabelSelector Domain
Expand Down

0 comments on commit 55d2b20

Please sign in to comment.