Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firewalls): add applied_to_resources to FirewallResource #297

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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