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

refactor: remove unneeded to_native calls #477

Merged
merged 3 commits into from
Mar 27, 2024
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
40 changes: 20 additions & 20 deletions plugins/inventory/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,43 +336,43 @@ def _fetch_servers(self) -> list[Server]:
def _build_inventory_server(self, server: Server) -> InventoryServer:
server_dict: InventoryServer = {}
server_dict["id"] = server.id
server_dict["name"] = to_native(server.name)
server_dict["status"] = to_native(server.status)
server_dict["name"] = server.name
server_dict["status"] = server.status

# Server Type
server_dict["type"] = to_native(server.server_type.name)
server_dict["server_type"] = to_native(server.server_type.name)
server_dict["architecture"] = to_native(server.server_type.architecture)
server_dict["type"] = server.server_type.name
server_dict["server_type"] = server.server_type.name
server_dict["architecture"] = server.server_type.architecture

# Network
if server.public_net.ipv4:
server_dict["ipv4"] = to_native(server.public_net.ipv4.ip)
server_dict["ipv4"] = server.public_net.ipv4.ip

if server.public_net.ipv6:
server_dict["ipv6"] = to_native(first_ipv6_address(server.public_net.ipv6.ip))
server_dict["ipv6_network"] = to_native(server.public_net.ipv6.network)
server_dict["ipv6_network_mask"] = to_native(server.public_net.ipv6.network_mask)
server_dict["ipv6"] = first_ipv6_address(server.public_net.ipv6.ip)
server_dict["ipv6_network"] = server.public_net.ipv6.network
server_dict["ipv6_network_mask"] = server.public_net.ipv6.network_mask

server_dict["private_networks"] = [
{"id": v.network.id, "name": to_native(v.network.name), "ip": to_native(v.ip)} for v in server.private_net
{"id": v.network.id, "name": v.network.name, "ip": v.ip} for v in server.private_net
]

if self.get_option("network"):
for private_net in server.private_net:
# Set private_ipv4 if user filtered for one network
if private_net.network.id == self.network.id:
server_dict["private_ipv4"] = to_native(private_net.ip)
server_dict["private_ipv4"] = private_net.ip
break

# Datacenter
server_dict["datacenter"] = to_native(server.datacenter.name)
server_dict["location"] = to_native(server.datacenter.location.name)
server_dict["datacenter"] = server.datacenter.name
server_dict["location"] = server.datacenter.location.name

# Image
if server.image is not None:
server_dict["image_id"] = server.image.id
server_dict["image_os_flavor"] = to_native(server.image.os_flavor)
server_dict["image_name"] = to_native(server.image.name or server.image.description)
server_dict["image_os_flavor"] = server.image.os_flavor
server_dict["image_name"] = server.image.name or server.image.description

# Labels
server_dict["labels"] = dict(server.labels)
Expand All @@ -391,28 +391,28 @@ def _build_inventory_server(self, server: Server) -> InventoryServer:
def _get_server_ansible_host(self, server: Server):
if self.get_option("connect_with") == "public_ipv4":
if server.public_net.ipv4:
return to_native(server.public_net.ipv4.ip)
return server.public_net.ipv4.ip
raise AnsibleError("Server has no public ipv4, but connect_with=public_ipv4 was specified")

if self.get_option("connect_with") == "public_ipv6":
if server.public_net.ipv6:
return to_native(first_ipv6_address(server.public_net.ipv6.ip))
return first_ipv6_address(server.public_net.ipv6.ip)
raise AnsibleError("Server has no public ipv6, but connect_with=public_ipv6 was specified")

if self.get_option("connect_with") == "hostname":
# every server has a name, no need to guard this
return to_native(server.name)
return server.name

if self.get_option("connect_with") == "ipv4_dns_ptr":
if server.public_net.ipv4:
return to_native(server.public_net.ipv4.dns_ptr)
return server.public_net.ipv4.dns_ptr
raise AnsibleError("Server has no public ipv4, but connect_with=ipv4_dns_ptr was specified")

if self.get_option("connect_with") == "private_ipv4":
if self.get_option("network"):
for private_net in server.private_net:
if private_net.network.id == self.network.id:
return to_native(private_net.ip)
return private_net.ip

else:
raise AnsibleError("You can only connect via private IPv4 if you specify a network")
Expand Down
17 changes: 8 additions & 9 deletions plugins/modules/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
Expand All @@ -160,14 +159,14 @@ class AnsibleHCloudCertificate(AnsibleHCloud):

def _prepare_result(self):
return {
"id": to_native(self.hcloud_certificate.id),
"name": to_native(self.hcloud_certificate.name),
"type": to_native(self.hcloud_certificate.type),
"fingerprint": to_native(self.hcloud_certificate.fingerprint),
"certificate": to_native(self.hcloud_certificate.certificate),
"not_valid_before": to_native(self.hcloud_certificate.not_valid_before.isoformat()),
"not_valid_after": to_native(self.hcloud_certificate.not_valid_after.isoformat()),
"domain_names": [to_native(domain) for domain in self.hcloud_certificate.domain_names],
"id": str(self.hcloud_certificate.id),
"name": self.hcloud_certificate.name,
"type": self.hcloud_certificate.type,
"fingerprint": self.hcloud_certificate.fingerprint,
"certificate": self.hcloud_certificate.certificate,
"not_valid_before": self.hcloud_certificate.not_valid_before.isoformat(),
"not_valid_after": self.hcloud_certificate.not_valid_after.isoformat(),
"domain_names": self.hcloud_certificate.domain_names,
"labels": self.hcloud_certificate.labels,
}

Expand Down
33 changes: 17 additions & 16 deletions plugins/modules/certificate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
Expand All @@ -100,23 +99,25 @@ class AnsibleHCloudCertificateInfo(AnsibleHCloud):
hcloud_certificate_info: list[BoundCertificate] | None = None

def _prepare_result(self):
certificates = []
tmp = []

for certificate in self.hcloud_certificate_info:
if certificate:
certificates.append(
{
"id": to_native(certificate.id),
"name": to_native(certificate.name),
"fingerprint": to_native(certificate.fingerprint),
"certificate": to_native(certificate.certificate),
"not_valid_before": to_native(certificate.not_valid_before.isoformat()),
"not_valid_after": to_native(certificate.not_valid_after.isoformat()),
"domain_names": [to_native(domain) for domain in certificate.domain_names],
"labels": certificate.labels,
}
)
return certificates
if certificate is None:
continue

tmp.append(
{
"id": str(certificate.id),
"name": certificate.name,
"fingerprint": certificate.fingerprint,
"certificate": certificate.certificate,
"not_valid_before": certificate.not_valid_before.isoformat(),
"not_valid_after": certificate.not_valid_after.isoformat(),
"domain_names": certificate.domain_names,
"labels": certificate.labels,
}
)
return tmp

def get_certificates(self):
try:
Expand Down
9 changes: 4 additions & 5 deletions plugins/modules/datacenter_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
Expand All @@ -139,10 +138,10 @@ def _prepare_result(self):

tmp.append(
{
"id": to_native(datacenter.id),
"name": to_native(datacenter.name),
"description": to_native(datacenter.description),
"location": to_native(datacenter.location.name),
"id": str(datacenter.id),
"name": datacenter.name,
"description": datacenter.description,
"location": datacenter.location.name,
"server_types": {
"available": [o.id for o in datacenter.server_types.available],
"available_for_migration": [o.id for o in datacenter.server_types.available_for_migration],
Expand Down
29 changes: 13 additions & 16 deletions plugins/modules/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@
import time

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import APIException, HCloudException
Expand All @@ -237,36 +236,34 @@ class AnsibleHCloudFirewall(AnsibleHCloud):

def _prepare_result(self):
return {
"id": to_native(self.hcloud_firewall.id),
"name": to_native(self.hcloud_firewall.name),
"id": str(self.hcloud_firewall.id),
"name": self.hcloud_firewall.name,
"rules": [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules],
"labels": self.hcloud_firewall.labels,
"applied_to": [self._prepare_result_applied_to(resource) for resource in self.hcloud_firewall.applied_to],
}

def _prepare_result_rule(self, rule: FirewallRule):
return {
"direction": to_native(rule.direction),
"protocol": to_native(rule.protocol),
"port": to_native(rule.port) if rule.port is not None else None,
"source_ips": [to_native(cidr) for cidr in rule.source_ips],
"destination_ips": [to_native(cidr) for cidr in rule.destination_ips],
"description": to_native(rule.description) if rule.description is not None else None,
"direction": rule.direction,
"protocol": rule.protocol,
"port": rule.port,
"source_ips": rule.source_ips,
"destination_ips": rule.destination_ips,
"description": rule.description,
}

def _prepare_result_applied_to(self, resource: FirewallResource):
result = {
"type": to_native(resource.type),
"server": to_native(resource.server.id) if resource.server is not None else None,
"label_selector": (
to_native(resource.label_selector.selector) if resource.label_selector is not None else None
),
"type": resource.type,
"server": str(resource.server.id) if resource.server is not None else None,
"label_selector": resource.label_selector.selector if resource.label_selector is not None else None,
}
if resource.applied_to_resources is not None:
result["applied_to_resources"] = [
{
"type": to_native(item.type),
"server": to_native(item.server.id) if item.server is not None else None,
"type": item.type,
"server": str(item.server.id) if item.server is not None else None,
}
for item in resource.applied_to_resources
]
Expand Down
29 changes: 13 additions & 16 deletions plugins/modules/firewall_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
Expand All @@ -167,8 +166,8 @@ def _prepare_result(self):

tmp.append(
{
"id": to_native(firewall.id),
"name": to_native(firewall.name),
"id": str(firewall.id),
"name": firewall.name,
"labels": firewall.labels,
"rules": [self._prepare_result_rule(rule) for rule in firewall.rules],
"applied_to": [self._prepare_result_applied_to(resource) for resource in firewall.applied_to],
Expand All @@ -179,27 +178,25 @@ def _prepare_result(self):

def _prepare_result_rule(self, rule: FirewallRule):
return {
"description": to_native(rule.description) if rule.description is not None else None,
"direction": to_native(rule.direction),
"protocol": to_native(rule.protocol),
"port": to_native(rule.port) if rule.port is not None else None,
"source_ips": [to_native(cidr) for cidr in rule.source_ips],
"destination_ips": [to_native(cidr) for cidr in rule.destination_ips],
"description": rule.description,
"direction": rule.direction,
"protocol": rule.protocol,
"port": rule.port,
"source_ips": rule.source_ips,
"destination_ips": rule.destination_ips,
}

def _prepare_result_applied_to(self, resource: FirewallResource):
result = {
"type": to_native(resource.type),
"server": to_native(resource.server.id) if resource.server is not None else None,
"label_selector": (
to_native(resource.label_selector.selector) if resource.label_selector is not None else None
),
"type": resource.type,
"server": str(resource.server.id) if resource.server is not None else None,
"label_selector": resource.label_selector.selector if resource.label_selector is not None else None,
}
if resource.applied_to_resources is not None:
result["applied_to_resources"] = [
{
"type": to_native(item.type),
"server": to_native(item.server.id) if item.server is not None else None,
"type": item.type,
"server": str(item.server.id) if item.server is not None else None,
}
for item in resource.applied_to_resources
]
Expand Down
7 changes: 3 additions & 4 deletions plugins/modules/firewall_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
Expand All @@ -125,12 +124,12 @@ def _prepare_result(self):
label_selectors = []
for resource in self.hcloud_firewall_resource.applied_to:
if resource.type == FirewallResource.TYPE_SERVER:
servers.append(to_native(resource.server.name))
servers.append(resource.server.name)
elif resource.type == FirewallResource.TYPE_LABEL_SELECTOR:
label_selectors.append(to_native(resource.label_selector.selector))
label_selectors.append(resource.label_selector.selector)

return {
"firewall": to_native(self.hcloud_firewall_resource.name),
"firewall": self.hcloud_firewall_resource.name,
"servers": servers,
"label_selectors": label_selectors,
}
Expand Down
19 changes: 7 additions & 12 deletions plugins/modules/floating_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
Expand All @@ -173,19 +172,15 @@ class AnsibleHCloudFloatingIP(AnsibleHCloud):
hcloud_floating_ip: BoundFloatingIP | None = None

def _prepare_result(self):
server = None

if self.hcloud_floating_ip.server is not None:
server = to_native(self.hcloud_floating_ip.server.name)
return {
"id": to_native(self.hcloud_floating_ip.id),
"name": to_native(self.hcloud_floating_ip.name),
"description": to_native(self.hcloud_floating_ip.description),
"ip": to_native(self.hcloud_floating_ip.ip),
"type": to_native(self.hcloud_floating_ip.type),
"home_location": to_native(self.hcloud_floating_ip.home_location.name),
"id": str(self.hcloud_floating_ip.id),
"name": self.hcloud_floating_ip.name,
"description": self.hcloud_floating_ip.description,
"ip": self.hcloud_floating_ip.ip,
"type": self.hcloud_floating_ip.type,
"home_location": self.hcloud_floating_ip.home_location.name,
"labels": self.hcloud_floating_ip.labels,
"server": server,
"server": self.hcloud_floating_ip.server.name if self.hcloud_floating_ip.server is not None else None,
"delete_protection": self.hcloud_floating_ip.protection["delete"],
}

Expand Down
Loading
Loading