Skip to content

Commit

Permalink
feat: improve hcloud library exceptions handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jul 6, 2023
1 parent 45f1c97 commit aa2ee69
Show file tree
Hide file tree
Showing 32 changed files with 420 additions and 169 deletions.
39 changes: 39 additions & 0 deletions plugins/module_utils/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)


import traceback

from ansible.module_utils.ansible_release import __version__
from ansible.module_utils.basic import env_fallback, missing_required_lib
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor import hcloud

HAS_REQUESTS = True
Expand Down Expand Up @@ -32,6 +35,42 @@ def __init__(self, module, represent):
module.fail_json(msg=missing_required_lib("python-dateutil"))
self._build_client()

def fail_json_hcloud(self, exception, msg=None, params=None, **kwargs):
last_traceback = traceback.format_exc()

failure = {}

if params is not None:
failure["params"] = params

if isinstance(exception, hcloud.APIException):
exception_message = exception.message
failure["message"] = exception.message
failure["code"] = exception.code
failure["details"] = exception.details

elif isinstance(exception, hcloud.actions.domain.ActionException):
if isinstance(exception, hcloud.actions.domain.ActionFailedException):
exception_message = "The action you were waiting for failed"
elif isinstance(exception, hcloud.actions.domain.ActionTimeoutException):
exception_message = "The action you were waiting for timed out"
else:
raise RuntimeError("Unknown action exception") from exception

if exception.action.error is not None:
exception_message += f": {exception.action.error['message']}"

failure["action"] = {k: getattr(exception.action, k) for k in exception.action.__slots__}
else:
exception_message = to_native(exception)

if msg is not None:
msg = f"{msg}: {exception_message}"
else:
msg = exception_message

self.module.fail_json(msg=msg, exception=last_traceback, failure=failure, **kwargs)

def _build_client(self):
self.client = hcloud.Client(
token=self.module.params["api_token"],
Expand Down
23 changes: 13 additions & 10 deletions plugins/modules/hcloud_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudCertificate(Hcloud):
Expand Down Expand Up @@ -164,8 +167,8 @@ def _get_certificate(self):
elif self.module.params.get("name") is not None:
self.hcloud_certificate = self.client.certificates.get_by_name(self.module.params.get("name"))

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

def _create_certificate(self):
self.module.fail_on_missing_params(required_params=["name"])
Expand All @@ -181,17 +184,17 @@ def _create_certificate(self):
if not self.module.check_mode:
try:
self.client.certificates.create(**params)
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)
else:
self.module.fail_on_missing_params(required_params=["domain_names"])
params["domain_names"] = self.module.params.get("domain_names")
if not self.module.check_mode:
try:
resp = self.client.certificates.create_managed(**params)
resp.action.wait_until_finished(max_retries=1000)
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

self._mark_as_changed()
self._get_certificate()
Expand All @@ -210,8 +213,8 @@ def _update_certificate(self):
if not self.module.check_mode:
self.hcloud_certificate.update(labels=labels)
self._mark_as_changed()
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)
self._get_certificate()

def present_certificate(self):
Expand All @@ -227,8 +230,8 @@ def delete_certificate(self):
if not self.module.check_mode:
try:
self.client.certificates.delete(self.hcloud_certificate)
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)
self._mark_as_changed()
self.hcloud_certificate = None

Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/hcloud_certificate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudCertificateInfo(Hcloud):
Expand Down Expand Up @@ -124,8 +127,8 @@ def get_certificates(self):
else:
self.hcloud_certificate_info = self.client.certificates.get_all()

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/hcloud_datacenter_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudDatacenterInfo(Hcloud):
Expand Down Expand Up @@ -108,8 +111,8 @@ def get_datacenters(self):
else:
self.hcloud_datacenter_info = self.client.datacenters.get_all()

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
15 changes: 8 additions & 7 deletions plugins/modules/hcloud_firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
APIException,
HCloudException,
)
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.firewalls.domain import (
FirewallRule,
Expand Down Expand Up @@ -209,8 +210,8 @@ def _get_firewall(self):
elif self.module.params.get("name") is not None:
self.hcloud_firewall = self.client.firewalls.get_by_name(self.module.params.get("name"))

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

def _create_firewall(self):
self.module.fail_on_missing_params(required_params=["name"])
Expand All @@ -234,8 +235,8 @@ def _create_firewall(self):
if not self.module.check_mode:
try:
self.client.firewalls.create(**params)
except Exception as e:
self.module.fail_json(msg=e.message, **params)
except HCloudException as e:
self.fail_json_hcloud(e, params=params)
self._mark_as_changed()
self._get_firewall()

Expand Down Expand Up @@ -292,9 +293,9 @@ def delete_firewall(self):
retry_count = retry_count + 1
time.sleep(0.5 * retry_count)
else:
self.module.fail_json(msg=e.message)
except Exception as e:
self.module.fail_json(msg=e.message)
self.fail_json_hcloud(e)
except HCloudException as e:
self.fail_json_hcloud(e)
self._mark_as_changed()
self.hcloud_firewall = None

Expand Down
19 changes: 11 additions & 8 deletions plugins/modules/hcloud_floating_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudFloatingIP(Hcloud):
Expand Down Expand Up @@ -194,8 +197,8 @@ def _get_floating_ip(self):
self.hcloud_floating_ip = self.client.floating_ips.get_by_id(self.module.params.get("id"))
else:
self.hcloud_floating_ip = self.client.floating_ips.get_by_name(self.module.params.get("name"))
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

def _create_floating_ip(self):
self.module.fail_on_missing_params(required_params=["type"])
Expand All @@ -221,8 +224,8 @@ def _create_floating_ip(self):
delete_protection = self.module.params.get("delete_protection")
if delete_protection is not None:
self.hcloud_floating_ip.change_protection(delete=delete_protection).wait_until_finished()
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)
self._mark_as_changed()
self._get_floating_ip()

Expand Down Expand Up @@ -268,8 +271,8 @@ def _update_floating_ip(self):
self._mark_as_changed()

self._get_floating_ip()
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

def present_floating_ip(self):
self._get_floating_ip()
Expand All @@ -292,8 +295,8 @@ def delete_floating_ip(self):
)
self._mark_as_changed()
self.hcloud_floating_ip = None
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/hcloud_floating_ip_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudFloatingIPInfo(Hcloud):
Expand Down Expand Up @@ -137,8 +140,8 @@ def get_floating_ips(self):
else:
self.hcloud_floating_ip_info = self.client.floating_ips.get_all()

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/hcloud_image_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudImageInfo(Hcloud):
Expand Down Expand Up @@ -170,8 +173,8 @@ def get_images(self):

self.hcloud_image_info = self.client.images.get_all(**params)

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
19 changes: 11 additions & 8 deletions plugins/modules/hcloud_load_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudLoadBalancer(Hcloud):
Expand Down Expand Up @@ -176,8 +179,8 @@ def _get_load_balancer(self):
self.hcloud_load_balancer = self.client.load_balancers.get_by_id(self.module.params.get("id"))
else:
self.hcloud_load_balancer = self.client.load_balancers.get_by_name(self.module.params.get("name"))
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

def _create_load_balancer(self):
self.module.fail_on_missing_params(required_params=["name", "load_balancer_type"])
Expand Down Expand Up @@ -205,8 +208,8 @@ def _create_load_balancer(self):
if delete_protection is not None:
self._get_load_balancer()
self.hcloud_load_balancer.change_protection(delete=delete_protection).wait_until_finished()
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)
self._mark_as_changed()
self._get_load_balancer()

Expand Down Expand Up @@ -251,8 +254,8 @@ def _update_load_balancer(self):

self._mark_as_changed()
self._get_load_balancer()
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

def present_load_balancer(self):
self._get_load_balancer()
Expand All @@ -269,8 +272,8 @@ def delete_load_balancer(self):
self.client.load_balancers.delete(self.hcloud_load_balancer)
self._mark_as_changed()
self.hcloud_load_balancer = None
except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/hcloud_load_balancer_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import (
HCloudException,
)


class AnsibleHcloudLoadBalancerInfo(Hcloud):
Expand Down Expand Up @@ -360,8 +363,8 @@ def get_load_balancers(self):

self.hcloud_load_balancer_info = self.client.load_balancers.get_all(**params)

except Exception as e:
self.module.fail_json(msg=e.message)
except HCloudException as e:
self.fail_json_hcloud(e)

@staticmethod
def define_module():
Expand Down
Loading

0 comments on commit aa2ee69

Please sign in to comment.