From 55d2b2043ec1e3a040eb9e360ca0dc0c299ad60f Mon Sep 17 00:00:00 2001 From: Jonas L Date: Mon, 25 Sep 2023 12:09:27 +0200 Subject: [PATCH] feat(firewalls): add `applied_to_resources` to `FirewallResource` (#297) Add the read-only `applied_to_resources` field to `FirewallResource` https://docs.hetzner.cloud/#firewalls-get-a-firewall --- hcloud/firewalls/__init__.py | 1 + hcloud/firewalls/client.py | 22 ++++++++++++++++++++++ hcloud/firewalls/domain.py | 24 +++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/hcloud/firewalls/__init__.py b/hcloud/firewalls/__init__.py index 42bde36..5205d76 100644 --- a/hcloud/firewalls/__init__.py +++ b/hcloud/firewalls/__init__.py @@ -5,6 +5,7 @@ CreateFirewallResponse, Firewall, FirewallResource, + FirewallResourceAppliedToResources, FirewallResourceLabelSelector, FirewallRule, ) diff --git a/hcloud/firewalls/client.py b/hcloud/firewalls/client.py index b77c066..48505ea 100644 --- a/hcloud/firewalls/client.py +++ b/hcloud/firewalls/client.py @@ -8,6 +8,7 @@ CreateFirewallResponse, Firewall, FirewallResource, + FirewallResourceAppliedToResources, FirewallResourceLabelSelector, FirewallRule, ) @@ -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( @@ -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: @@ -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) diff --git a/hcloud/firewalls/domain.py b/hcloud/firewalls/domain.py index 44bd7c9..d294fb8 100644 --- a/hcloud/firewalls/domain.py +++ b/hcloud/firewalls/domain.py @@ -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""" @@ -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]: """ @@ -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