Skip to content
Open
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
Empty file added plugins/filter/__init__.py
Empty file.
15 changes: 5 additions & 10 deletions plugins/filter/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from ansible.errors import AnsibleFilterError
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.vendor.hcloud.exp.zone import format_txt_record


# pylint: disable=unused-argument
def load_balancer_status(load_balancer: dict, *args, **kwargs) -> Literal["unknown", "unhealthy", "healthy"]:
Expand Down Expand Up @@ -50,18 +52,11 @@ def targets_status(targets: list) -> Literal["unknown", "unhealthy", "healthy"]:
# pylint: disable=unused-argument
def txt_record(record: str, *args, **kwargs) -> str:
"""
Return the status of a Load Balancer based on its targets.
Format a TXT record by splitting it in quoted strings of 255 characters.
Existing quotes will be escaped.
"""
try:
record = record.replace('"', '\\"')

parts = []
for start in range(0, len(record), 255):
end = min(start + 255, len(record))
parts.append('"' + record[start:end] + '"')
record = " ".join(parts)

return record
return format_txt_record(record)
except Exception as exc:
raise AnsibleFilterError(f"txt_record - {to_native(exc)}", orig_exc=exc) from exc

Expand Down
2 changes: 1 addition & 1 deletion plugins/module_utils/vendor/hcloud/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "2.9.0" # x-releaser-pleaser-version
__version__ = "2.10.0" # x-releaser-pleaser-version
4 changes: 4 additions & 0 deletions plugins/module_utils/vendor/hcloud/exp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
The `exp` module is a namespace that holds experimental features for the `hcloud-python`
library, breaking changes may occur within minor releases.
"""
35 changes: 35 additions & 0 deletions plugins/module_utils/vendor/hcloud/exp/zone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
The `exp.zone` module is a namespace that holds experimental features for the `hcloud-python`
library, breaking changes may occur within minor releases.
"""

from __future__ import annotations


def is_txt_record_quoted(value: str) -> bool:
"""
Check whether a TXT record is already quoted.

- hello world => false
- "hello world" => true
"""
return value.startswith('"') and value.endswith('"')


def format_txt_record(value: str) -> str:
"""
Format a TXT record by splitting it in quoted strings of 255 characters.
Existing quotes will be escaped.

- hello world => "hello world"
- hello "world" => "hello \"world\""
"""
value = value.replace('"', '\\"')

parts = []
for start in range(0, len(value), 255):
end = min(start + 255, len(value))
parts.append('"' + value[start:end] + '"')
value = " ".join(parts)

return value
12 changes: 11 additions & 1 deletion plugins/module_utils/vendor/hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..load_balancer_types import BoundLoadBalancerType
from ..locations import BoundLocation
from ..metrics import Metrics
from ..networks import BoundNetwork
from ..networks import BoundNetwork, Network
from ..servers import BoundServer
from .client import BoundLoadBalancer

Expand Down Expand Up @@ -110,6 +110,16 @@ def __init__(
self.ingoing_traffic = ingoing_traffic
self.included_traffic = included_traffic

def private_net_for(self, network: BoundNetwork | Network) -> PrivateNet | None:
"""
Returns the load balancer's network attachment information in the given Network,
and None if no attachment was found.
"""
for o in self.private_net or []:
if o.network.id == network.id:
return o
return None


class LoadBalancerService(BaseDomain):
"""LoadBalancerService Domain
Expand Down
12 changes: 11 additions & 1 deletion plugins/module_utils/vendor/hcloud/servers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..images import BoundImage
from ..isos import BoundIso
from ..metrics import Metrics
from ..networks import BoundNetwork
from ..networks import BoundNetwork, Network
from ..placement_groups import BoundPlacementGroup
from ..primary_ips import BoundPrimaryIP, PrimaryIP
from ..server_types import BoundServerType
Expand Down Expand Up @@ -157,6 +157,16 @@ def __init__(
self.primary_disk_size = primary_disk_size
self.placement_group = placement_group

def private_net_for(self, network: BoundNetwork | Network) -> PrivateNet | None:
"""
Returns the server's network attachment information in the given Network,
and None if no attachment was found.
"""
for o in self.private_net or []:
if o.network.id == network.id:
return o
return None


class CreateServerResponse(BaseDomain):
"""Create Server Response Domain
Expand Down
19 changes: 6 additions & 13 deletions plugins/modules/load_balancer_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def _prepare_result(self):

def _get_load_balancer_and_network(self):
try:
self.hcloud_network = None
self.hcloud_load_balancer = None
self.hcloud_load_balancer_network = None

self.hcloud_network = self._client_get_by_name_or_id(
"networks",
self.module.params.get("network"),
Expand All @@ -135,17 +139,10 @@ def _get_load_balancer_and_network(self):
"load_balancers",
self.module.params.get("load_balancer"),
)

self.hcloud_load_balancer_network = None
self.hcloud_load_balancer_network = self.hcloud_load_balancer.private_net_for(self.hcloud_network)
except HCloudException as exception:
self.fail_json_hcloud(exception)

def _get_load_balancer_network(self):
self.hcloud_load_balancer_network = None
for private_net in self.hcloud_load_balancer.private_net:
if private_net.network.id == self.hcloud_network.id:
self.hcloud_load_balancer_network = private_net

def _attach(self):
params = {
"network": self.hcloud_network,
Expand Down Expand Up @@ -185,7 +182,6 @@ def _create_load_balancer_network(self):
self._attach()

self._get_load_balancer_and_network()
self._get_load_balancer_network()

def _update_load_balancer_network(self):
ip_range = self.module.params.get("ip_range")
Expand All @@ -202,20 +198,17 @@ def _update_load_balancer_network(self):

# No further updates needed, exit
self._get_load_balancer_and_network()
self._get_load_balancer_network()
return

def present_load_balancer_network(self):
self._get_load_balancer_and_network()
self._get_load_balancer_network()
if self.hcloud_load_balancer_network is None:
self._create_load_balancer_network()
else:
self._update_load_balancer_network()

def delete_load_balancer_network(self):
self._get_load_balancer_and_network()
self._get_load_balancer_network()
if self.hcloud_load_balancer_network is not None and self.hcloud_load_balancer is not None:
self._detach()
self.hcloud_load_balancer_network = None
Expand All @@ -230,7 +223,7 @@ def done(x: PrivateNet | None):
# pylint: disable=disallowed-name
for _ in range(10):
self.hcloud_load_balancer.reload()
self._get_load_balancer_network()
self.hcloud_load_balancer_network = self.hcloud_load_balancer.private_net_for(self.hcloud_network)

if done(self.hcloud_load_balancer_network):
break
Expand Down
16 changes: 5 additions & 11 deletions plugins/modules/server_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ def _prepare_result(self):

def _get_server_and_network(self):
try:
self.hcloud_network = None
self.hcloud_server = None
self.hcloud_server_network = None

self.hcloud_network = self._client_get_by_name_or_id(
"networks",
self.module.params.get("network"),
Expand All @@ -156,15 +160,10 @@ def _get_server_and_network(self):
"servers",
self.module.params.get("server"),
)
self.hcloud_server_network = None
self.hcloud_server_network = self.hcloud_server.private_net_for(self.hcloud_network)
except HCloudException as exception:
self.fail_json_hcloud(exception)

def _get_server_network(self):
for private_net in self.hcloud_server.private_net:
if private_net.network.id == self.hcloud_network.id:
self.hcloud_server_network = private_net

def _attach(self):
params = {
"network": self.hcloud_network,
Expand Down Expand Up @@ -199,7 +198,6 @@ def _detach(self):
def _create_server_network(self):
self._attach()
self._get_server_and_network()
self._get_server_network()

def _update_server_network(self):
ip_range = self.module.params.get("ip_range")
Expand All @@ -216,7 +214,6 @@ def _update_server_network(self):

# No further updates needed, exit
self._get_server_and_network()
self._get_server_network()
return

params = {
Expand All @@ -236,19 +233,16 @@ def _update_server_network(self):
self._mark_as_changed()

self._get_server_and_network()
self._get_server_network()

def present_server_network(self):
self._get_server_and_network()
self._get_server_network()
if self.hcloud_server_network is None:
self._create_server_network()
else:
self._update_server_network()

def delete_server_network(self):
self._get_server_and_network()
self._get_server_network()
if self.hcloud_server_network is not None and self.hcloud_server is not None:
self._detach()
self.hcloud_server_network = None
Expand Down
2 changes: 1 addition & 1 deletion scripts/vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
logger = logging.getLogger("vendor")

HCLOUD_SOURCE_URL = "https://github.com/hetznercloud/hcloud-python"
HCLOUD_VERSION = "v2.9.0"
HCLOUD_VERSION = "v2.10.0"
HCLOUD_VENDOR_PATH = "plugins/module_utils/vendor/hcloud"


Expand Down
Loading