diff --git a/plugins/module_utils/hcloud.py b/plugins/module_utils/hcloud.py index 69082f5..17419e2 100644 --- a/plugins/module_utils/hcloud.py +++ b/plugins/module_utils/hcloud.py @@ -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 @@ -32,6 +35,30 @@ class Hcloud: 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): + failure["message"] = exception.message + failure["code"] = exception.code + failure["details"] = exception.details + + elif isinstance(exception, hcloud.actions.domain.ActionException): + failure["action"] = {k: getattr(exception.action, k) for k in exception.action.__slots__} + + exception_message = to_native(exception) + if msg is not None: + msg = f"{exception_message}: {msg}" + 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"], diff --git a/plugins/modules/hcloud_certificate.py b/plugins/modules/hcloud_certificate.py index f44af77..6d907c4 100644 --- a/plugins/modules/hcloud_certificate.py +++ b/plugins/modules/hcloud_certificate.py @@ -137,6 +137,9 @@ hcloud_certificate: 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): @@ -164,8 +167,8 @@ class AnsibleHcloudCertificate(Hcloud): 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"]) @@ -181,8 +184,8 @@ class AnsibleHcloudCertificate(Hcloud): 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") @@ -190,8 +193,8 @@ class AnsibleHcloudCertificate(Hcloud): 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() @@ -210,8 +213,8 @@ class AnsibleHcloudCertificate(Hcloud): 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): @@ -227,8 +230,8 @@ class AnsibleHcloudCertificate(Hcloud): 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 diff --git a/plugins/modules/hcloud_certificate_info.py b/plugins/modules/hcloud_certificate_info.py index dcfa322..3766ef2 100644 --- a/plugins/modules/hcloud_certificate_info.py +++ b/plugins/modules/hcloud_certificate_info.py @@ -85,6 +85,9 @@ hcloud_certificate_info: 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): @@ -124,8 +127,8 @@ class AnsibleHcloudCertificateInfo(Hcloud): 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(): diff --git a/plugins/modules/hcloud_datacenter_info.py b/plugins/modules/hcloud_datacenter_info.py index a2abbb5..9001dd5 100644 --- a/plugins/modules/hcloud_datacenter_info.py +++ b/plugins/modules/hcloud_datacenter_info.py @@ -76,6 +76,9 @@ hcloud_datacenter_info: 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): @@ -108,8 +111,8 @@ class AnsibleHcloudDatacenterInfo(Hcloud): 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(): diff --git a/plugins/modules/hcloud_firewall.py b/plugins/modules/hcloud_firewall.py index c22d9b8..6cc38d9 100644 --- a/plugins/modules/hcloud_firewall.py +++ b/plugins/modules/hcloud_firewall.py @@ -173,6 +173,7 @@ 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 ( APIException, + HCloudException, ) from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.firewalls.domain import ( FirewallRule, @@ -209,8 +210,8 @@ class AnsibleHcloudFirewall(Hcloud): 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"]) @@ -234,8 +235,8 @@ class AnsibleHcloudFirewall(Hcloud): 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() @@ -292,9 +293,9 @@ class AnsibleHcloudFirewall(Hcloud): 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 diff --git a/plugins/modules/hcloud_floating_ip.py b/plugins/modules/hcloud_floating_ip.py index 292d1fd..a71a7ba 100644 --- a/plugins/modules/hcloud_floating_ip.py +++ b/plugins/modules/hcloud_floating_ip.py @@ -164,6 +164,9 @@ hcloud_floating_ip: 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): @@ -194,8 +197,8 @@ class AnsibleHcloudFloatingIP(Hcloud): 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"]) @@ -221,8 +224,8 @@ class AnsibleHcloudFloatingIP(Hcloud): 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() @@ -268,8 +271,8 @@ class AnsibleHcloudFloatingIP(Hcloud): 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() @@ -292,8 +295,8 @@ class AnsibleHcloudFloatingIP(Hcloud): ) 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(): diff --git a/plugins/modules/hcloud_floating_ip_info.py b/plugins/modules/hcloud_floating_ip_info.py index e9cd283..d976ce8 100644 --- a/plugins/modules/hcloud_floating_ip_info.py +++ b/plugins/modules/hcloud_floating_ip_info.py @@ -95,6 +95,9 @@ hcloud_floating_ip_info: 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): @@ -137,8 +140,8 @@ class AnsibleHcloudFloatingIPInfo(Hcloud): 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(): diff --git a/plugins/modules/hcloud_image_info.py b/plugins/modules/hcloud_image_info.py index 5699121..a6c4f25 100644 --- a/plugins/modules/hcloud_image_info.py +++ b/plugins/modules/hcloud_image_info.py @@ -111,6 +111,9 @@ hcloud_image_info: 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): @@ -170,8 +173,8 @@ class AnsibleHcloudImageInfo(Hcloud): 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(): diff --git a/plugins/modules/hcloud_load_balancer.py b/plugins/modules/hcloud_load_balancer.py index 87a9efa..556be45 100644 --- a/plugins/modules/hcloud_load_balancer.py +++ b/plugins/modules/hcloud_load_balancer.py @@ -144,6 +144,9 @@ hcloud_load_balancer: 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): @@ -176,8 +179,8 @@ class AnsibleHcloudLoadBalancer(Hcloud): 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"]) @@ -205,8 +208,8 @@ class AnsibleHcloudLoadBalancer(Hcloud): 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() @@ -251,8 +254,8 @@ class AnsibleHcloudLoadBalancer(Hcloud): 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() @@ -269,8 +272,8 @@ class AnsibleHcloudLoadBalancer(Hcloud): 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(): diff --git a/plugins/modules/hcloud_load_balancer_info.py b/plugins/modules/hcloud_load_balancer_info.py index 6b23904..74bf964 100644 --- a/plugins/modules/hcloud_load_balancer_info.py +++ b/plugins/modules/hcloud_load_balancer_info.py @@ -259,6 +259,9 @@ hcloud_load_balancer_info: 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): @@ -360,8 +363,8 @@ class AnsibleHcloudLoadBalancerInfo(Hcloud): 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(): diff --git a/plugins/modules/hcloud_load_balancer_network.py b/plugins/modules/hcloud_load_balancer_network.py index 6b0ee6b..50b29e6 100644 --- a/plugins/modules/hcloud_load_balancer_network.py +++ b/plugins/modules/hcloud_load_balancer_network.py @@ -94,6 +94,9 @@ hcloud_load_balancer_network: 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 AnsibleHcloudLoadBalancerNetwork(Hcloud): @@ -123,8 +126,8 @@ class AnsibleHcloudLoadBalancerNetwork(Hcloud): self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name) self.hcloud_load_balancer_network = None - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _get_load_balancer_network(self): for privateNet in self.hcloud_load_balancer.private_net: @@ -132,9 +135,7 @@ class AnsibleHcloudLoadBalancerNetwork(Hcloud): self.hcloud_load_balancer_network = privateNet def _create_load_balancer_network(self): - params = { - "network": self.hcloud_network, - } + params = {"network": self.hcloud_network} if self.module.params.get("ip") is not None: params["ip"] = self.module.params.get("ip") @@ -142,8 +143,8 @@ class AnsibleHcloudLoadBalancerNetwork(Hcloud): if not self.module.check_mode: try: self.hcloud_load_balancer.attach_to_network(**params).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_and_network() @@ -165,8 +166,8 @@ class AnsibleHcloudLoadBalancerNetwork(Hcloud): self.hcloud_load_balancer_network.network ).wait_until_finished() 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.hcloud_load_balancer_network = None diff --git a/plugins/modules/hcloud_load_balancer_service.py b/plugins/modules/hcloud_load_balancer_service.py index 316a475..61ff51e 100644 --- a/plugins/modules/hcloud_load_balancer_service.py +++ b/plugins/modules/hcloud_load_balancer_service.py @@ -284,6 +284,7 @@ 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 ( APIException, + HCloudException, ) from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.load_balancers.domain import ( LoadBalancerHealtCheckHttp, @@ -347,8 +348,8 @@ class AnsibleHcloudLoadBalancerService(Hcloud): self.module.fail_json(msg="Load balancer does not exist: %s" % load_balancer_name) self._get_load_balancer_service() - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _create_load_balancer_service(self): self.module.fail_on_missing_params(required_params=["protocol"]) @@ -377,8 +378,8 @@ class AnsibleHcloudLoadBalancerService(Hcloud): self.hcloud_load_balancer.add_service(LoadBalancerService(**params)).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_load_balancer() self._get_load_balancer_service() @@ -403,8 +404,8 @@ class AnsibleHcloudLoadBalancerService(Hcloud): hcloud_cert = self.client.certificates.get_by_name(certificate) except Exception: hcloud_cert = self.client.certificates.get_by_id(certificate) - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) service_http.certificates.append(hcloud_cert) return service_http @@ -473,8 +474,8 @@ class AnsibleHcloudLoadBalancerService(Hcloud): self.hcloud_load_balancer.update_service(LoadBalancerService(**params)).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._get_load_balancer() if changed: @@ -501,12 +502,12 @@ class AnsibleHcloudLoadBalancerService(Hcloud): self.hcloud_load_balancer.delete_service(self.hcloud_load_balancer_service).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.hcloud_load_balancer_service = None except APIException as e: - self.module.fail_json(msg=e.message) + self.fail_json_hcloud(e) @staticmethod def define_module(): diff --git a/plugins/modules/hcloud_load_balancer_target.py b/plugins/modules/hcloud_load_balancer_target.py index 266903b..bd2c411 100644 --- a/plugins/modules/hcloud_load_balancer_target.py +++ b/plugins/modules/hcloud_load_balancer_target.py @@ -138,6 +138,10 @@ hcloud_load_balancer_target: 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 ( + APIException, + HCloudException, +) from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.load_balancers.domain import ( LoadBalancerTarget, LoadBalancerTargetIP, @@ -181,8 +185,8 @@ class AnsibleHcloudLoadBalancerTarget(Hcloud): self.module.fail_json(msg="Server not found: %s" % server_name) self.hcloud_load_balancer_target = None - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _get_load_balancer_target(self): for target in self.hcloud_load_balancer.targets: @@ -224,11 +228,13 @@ class AnsibleHcloudLoadBalancerTarget(Hcloud): if not self.module.check_mode: try: self.hcloud_load_balancer.add_target(**params).wait_until_finished() - except Exception as e: + except APIException as e: if e.code == "locked" or e.code == "conflict": self._create_load_balancer_target() else: - 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._get_load_balancer_and_target() @@ -267,8 +273,8 @@ class AnsibleHcloudLoadBalancerTarget(Hcloud): ) try: self.hcloud_load_balancer.remove_target(target).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.hcloud_load_balancer_target = None diff --git a/plugins/modules/hcloud_load_balancer_type_info.py b/plugins/modules/hcloud_load_balancer_type_info.py index f74a870..85afb45 100644 --- a/plugins/modules/hcloud_load_balancer_type_info.py +++ b/plugins/modules/hcloud_load_balancer_type_info.py @@ -87,6 +87,9 @@ hcloud_load_balancer_type_info: 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 AnsibleHcloudLoadBalancerTypeInfo(Hcloud): @@ -125,8 +128,8 @@ class AnsibleHcloudLoadBalancerTypeInfo(Hcloud): else: self.hcloud_load_balancer_type_info = self.client.load_balancer_types.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(): diff --git a/plugins/modules/hcloud_location_info.py b/plugins/modules/hcloud_location_info.py index ad32729..196aa33 100644 --- a/plugins/modules/hcloud_location_info.py +++ b/plugins/modules/hcloud_location_info.py @@ -77,6 +77,9 @@ hcloud_location_info: 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 AnsibleHcloudLocationInfo(Hcloud): @@ -109,8 +112,8 @@ class AnsibleHcloudLocationInfo(Hcloud): else: self.hcloud_location_info = self.client.locations.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(): diff --git a/plugins/modules/hcloud_network.py b/plugins/modules/hcloud_network.py index a3d10fb..7571e5b 100644 --- a/plugins/modules/hcloud_network.py +++ b/plugins/modules/hcloud_network.py @@ -119,6 +119,9 @@ hcloud_network: 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 AnsibleHcloudNetwork(Hcloud): @@ -142,8 +145,8 @@ class AnsibleHcloudNetwork(Hcloud): self.hcloud_network = self.client.networks.get_by_id(self.module.params.get("id")) else: self.hcloud_network = self.client.networks.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_network(self): self.module.fail_on_missing_params(required_params=["name", "ip_range"]) @@ -165,8 +168,8 @@ class AnsibleHcloudNetwork(Hcloud): if delete_protection is not None: self._get_network() self.hcloud_network.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_network() @@ -198,8 +201,8 @@ class AnsibleHcloudNetwork(Hcloud): if not self.module.check_mode: self.hcloud_network.change_protection(delete=delete_protection).wait_until_finished() 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_network() def present_network(self): @@ -216,8 +219,8 @@ class AnsibleHcloudNetwork(Hcloud): if not self.module.check_mode: self.client.networks.delete(self.hcloud_network) 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.hcloud_network = None @staticmethod diff --git a/plugins/modules/hcloud_network_info.py b/plugins/modules/hcloud_network_info.py index 17d4ff8..251a431 100644 --- a/plugins/modules/hcloud_network_info.py +++ b/plugins/modules/hcloud_network_info.py @@ -184,6 +184,9 @@ hcloud_network_info: 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 AnsibleHcloudNetworkInfo(Hcloud): @@ -259,8 +262,8 @@ class AnsibleHcloudNetworkInfo(Hcloud): else: self.hcloud_network_info = self.client.networks.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(): diff --git a/plugins/modules/hcloud_placement_group.py b/plugins/modules/hcloud_placement_group.py index ffe324d..25ce1b8 100644 --- a/plugins/modules/hcloud_placement_group.py +++ b/plugins/modules/hcloud_placement_group.py @@ -110,6 +110,9 @@ hcloud_placement_group: 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 AnsibleHcloudPlacementGroup(Hcloud): @@ -132,8 +135,8 @@ class AnsibleHcloudPlacementGroup(Hcloud): self.hcloud_placement_group = self.client.placement_groups.get_by_id(self.module.params.get("id")) elif self.module.params.get("name") is not None: self.hcloud_placement_group = self.client.placement_groups.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_placement_group(self): self.module.fail_on_missing_params(required_params=["name"]) @@ -145,8 +148,8 @@ class AnsibleHcloudPlacementGroup(Hcloud): if not self.module.check_mode: try: self.client.placement_groups.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_placement_group() diff --git a/plugins/modules/hcloud_primary_ip.py b/plugins/modules/hcloud_primary_ip.py index b5b6866..d0f553a 100644 --- a/plugins/modules/hcloud_primary_ip.py +++ b/plugins/modules/hcloud_primary_ip.py @@ -134,6 +134,9 @@ hcloud_primary_ip: 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 AnsibleHcloudPrimaryIP(Hcloud): @@ -158,8 +161,8 @@ class AnsibleHcloudPrimaryIP(Hcloud): self.hcloud_primary_ip = self.client.primary_ips.get_by_id(self.module.params.get("id")) else: self.hcloud_primary_ip = self.client.primary_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_primary_ip(self): self.module.fail_on_missing_params(required_params=["type", "datacenter"]) @@ -179,8 +182,8 @@ class AnsibleHcloudPrimaryIP(Hcloud): delete_protection = self.module.params.get("delete_protection") if delete_protection is not None: self.hcloud_primary_ip.change_protection(delete=delete_protection).wait_until_finished() - except Exception as e: - self.module.fail_json(msg=e) + except HCloudException as e: + self.fail_json_hcloud(e) self._mark_as_changed() self._get_primary_ip() @@ -199,8 +202,8 @@ class AnsibleHcloudPrimaryIP(Hcloud): self._mark_as_changed() self._get_primary_ip() - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def present_primary_ip(self): self._get_primary_ip() @@ -217,8 +220,8 @@ class AnsibleHcloudPrimaryIP(Hcloud): self.client.primary_ips.delete(self.hcloud_primary_ip) self._mark_as_changed() self.hcloud_primary_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(): diff --git a/plugins/modules/hcloud_primary_ip_info.py b/plugins/modules/hcloud_primary_ip_info.py index ec3217a..224c0b7 100644 --- a/plugins/modules/hcloud_primary_ip_info.py +++ b/plugins/modules/hcloud_primary_ip_info.py @@ -120,6 +120,9 @@ hcloud_primary_ip_info: 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 AnsibleHcloudPrimaryIPInfo(Hcloud): @@ -167,8 +170,8 @@ class AnsibleHcloudPrimaryIPInfo(Hcloud): else: self.hcloud_primary_ip_info = self.client.primary_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(): diff --git a/plugins/modules/hcloud_rdns.py b/plugins/modules/hcloud_rdns.py index 8b9763b..57a11fc 100644 --- a/plugins/modules/hcloud_rdns.py +++ b/plugins/modules/hcloud_rdns.py @@ -140,6 +140,9 @@ from ansible_collections.ansible.netcommon.plugins.module_utils.network.common i utils, ) from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud +from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import ( + HCloudException, +) class AnsibleHcloudReverseDNS(Hcloud): @@ -185,8 +188,8 @@ class AnsibleHcloudReverseDNS(Hcloud): self.hcloud_resource = self.client.load_balancers.get_by_name(self.module.params.get("load_balancer")) if self.hcloud_resource is None: self.module.fail_json(msg="The selected Load Balancer does not exist") - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _get_rdns(self): ip_address = self.module.params.get("ip_address") @@ -266,8 +269,8 @@ class AnsibleHcloudReverseDNS(Hcloud): if not self.module.check_mode: try: self.hcloud_resource.change_dns_ptr(**params).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_resource() self._get_rdns() @@ -283,8 +286,8 @@ class AnsibleHcloudReverseDNS(Hcloud): if not self.module.check_mode: try: self.hcloud_resource.change_dns_ptr(**params).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_resource() self._get_rdns() @@ -304,8 +307,8 @@ class AnsibleHcloudReverseDNS(Hcloud): if not self.module.check_mode: try: self.hcloud_resource.change_dns_ptr(ip=self.hcloud_rdns["ip_address"], dns_ptr=None) - 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_rdns = None diff --git a/plugins/modules/hcloud_route.py b/plugins/modules/hcloud_route.py index ee73d16..b506d27 100644 --- a/plugins/modules/hcloud_route.py +++ b/plugins/modules/hcloud_route.py @@ -90,6 +90,9 @@ hcloud_route: 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, +) from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.networks.domain import ( NetworkRoute, ) @@ -112,8 +115,8 @@ class AnsibleHcloudRoute(Hcloud): try: self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("network")) self.hcloud_route = None - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _get_route(self): destination = self.module.params.get("destination") @@ -130,8 +133,8 @@ class AnsibleHcloudRoute(Hcloud): if not self.module.check_mode: try: self.hcloud_network.add_route(route=route).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_network() @@ -150,8 +153,8 @@ class AnsibleHcloudRoute(Hcloud): if not self.module.check_mode: try: self.hcloud_network.delete_route(self.hcloud_route).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.hcloud_route = None diff --git a/plugins/modules/hcloud_server.py b/plugins/modules/hcloud_server.py index 0cf7932..b0d3060 100644 --- a/plugins/modules/hcloud_server.py +++ b/plugins/modules/hcloud_server.py @@ -333,6 +333,9 @@ from datetime import datetime, timedelta, timezone 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, +) from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.firewalls.domain import ( FirewallResource, ) @@ -393,8 +396,8 @@ class AnsibleHcloudServer(Hcloud): self.hcloud_server = self.client.servers.get_by_id(self.module.params.get("id")) else: self.hcloud_server = self.client.servers.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_server(self): self.module.fail_on_missing_params(required_params=["name", "server_type", "image"]) @@ -486,8 +489,8 @@ class AnsibleHcloudServer(Hcloud): delete=delete_protection, rebuild=rebuild_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_server() @@ -505,8 +508,8 @@ class AnsibleHcloudServer(Hcloud): else: try: image = self.client.images.get_by_id(self.module.params.get("image")) - except Exception: - self.module.fail_json(msg="Image %s was not found" % self.module.params.get("image")) + except HCloudException as e: + self.fail_json_hcloud(e, msg="Image %s was not found" % self.module.params.get("image")) if image.deprecated is not None: available_until = image.deprecated + timedelta(days=90) if self.module.params.get("allow_deprecated_image"): @@ -529,8 +532,11 @@ class AnsibleHcloudServer(Hcloud): if server_type is None: try: server_type = self.client.server_types.get_by_id(self.module.params.get("server_type")) - except Exception: - self.module.fail_json(msg="server_type %s was not found" % self.module.params.get("server_type")) + except HCloudException as e: + self.fail_json_hcloud( + e, + msg="server_type %s was not found" % self.module.params.get("server_type"), + ) self._check_and_warn_deprecated_server(server_type) @@ -565,9 +571,10 @@ class AnsibleHcloudServer(Hcloud): if placement_group is None: try: placement_group = self.client.placement_groups.get_by_id(self.module.params.get("placement_group")) - except Exception: - self.module.fail_json( - msg="placement_group %s was not found" % self.module.params.get("placement_group") + except HCloudException as e: + self.fail_json_hcloud( + e, + msg="placement_group %s was not found" % self.module.params.get("placement_group"), ) return placement_group @@ -580,8 +587,8 @@ class AnsibleHcloudServer(Hcloud): if primary_ip is None: try: primary_ip = self.client.primary_ips.get_by_id(self.module.params.get(field)) - except Exception as e: - self.module.fail_json(msg="primary_ip %s was not found" % self.module.params.get(field)) + except HCloudException as e: + self.fail_json_hcloud(e, msg="primary_ip %s was not found" % self.module.params.get(field)) return primary_ip @@ -775,8 +782,8 @@ class AnsibleHcloudServer(Hcloud): ).wait_until_finished() self._mark_as_changed() self._get_server() - except Exception as e: - self.module.fail_json(msg=e) + except HCloudException as e: + self.fail_json_hcloud(e) def _set_rescue_mode(self, rescue_mode): if self.module.params.get("ssh_keys"): @@ -800,8 +807,8 @@ class AnsibleHcloudServer(Hcloud): self.client.servers.power_on(self.hcloud_server).wait_until_finished() self._mark_as_changed() self._get_server() - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def stop_server(self): try: @@ -811,8 +818,8 @@ class AnsibleHcloudServer(Hcloud): self.client.servers.power_off(self.hcloud_server).wait_until_finished() self._mark_as_changed() self._get_server() - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def stop_server_if_forced(self): previous_server_status = self.hcloud_server.status @@ -842,8 +849,8 @@ class AnsibleHcloudServer(Hcloud): self._mark_as_changed() self._get_server() - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def present_server(self): self._get_server() @@ -860,8 +867,8 @@ class AnsibleHcloudServer(Hcloud): self.client.servers.delete(self.hcloud_server).wait_until_finished() self._mark_as_changed() self.hcloud_server = 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(): diff --git a/plugins/modules/hcloud_server_info.py b/plugins/modules/hcloud_server_info.py index 6980808..43755ae 100644 --- a/plugins/modules/hcloud_server_info.py +++ b/plugins/modules/hcloud_server_info.py @@ -140,6 +140,9 @@ hcloud_server_info: 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 AnsibleHcloudServerInfo(Hcloud): @@ -195,8 +198,8 @@ class AnsibleHcloudServerInfo(Hcloud): else: self.hcloud_server_info = self.client.servers.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(): diff --git a/plugins/modules/hcloud_server_network.py b/plugins/modules/hcloud_server_network.py index fb276e0..fdca2c7 100644 --- a/plugins/modules/hcloud_server_network.py +++ b/plugins/modules/hcloud_server_network.py @@ -117,6 +117,7 @@ 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 ( APIException, + HCloudException, ) @@ -140,8 +141,8 @@ class AnsibleHcloudServerNetwork(Hcloud): self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("network")) self.hcloud_server = self.client.servers.get_by_name(self.module.params.get("server")) self.hcloud_server_network = None - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _get_server_network(self): for privateNet in self.hcloud_server.private_net: @@ -161,8 +162,8 @@ class AnsibleHcloudServerNetwork(Hcloud): if not self.module.check_mode: try: self.hcloud_server.attach_to_network(**params).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_server_and_network() @@ -180,7 +181,7 @@ class AnsibleHcloudServerNetwork(Hcloud): try: self.hcloud_server.change_alias_ips(**params).wait_until_finished() except APIException as e: - self.module.fail_json(msg=e.message) + self.fail_json_hcloud(e) self._mark_as_changed() self._get_server_and_network() @@ -201,8 +202,8 @@ class AnsibleHcloudServerNetwork(Hcloud): if not self.module.check_mode: try: self.hcloud_server.detach_from_network(self.hcloud_server_network.network).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.hcloud_server_network = None diff --git a/plugins/modules/hcloud_server_type_info.py b/plugins/modules/hcloud_server_type_info.py index 9f2ba00..c65202d 100644 --- a/plugins/modules/hcloud_server_type_info.py +++ b/plugins/modules/hcloud_server_type_info.py @@ -123,6 +123,9 @@ hcloud_server_type_info: 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 AnsibleHcloudServerTypeInfo(Hcloud): @@ -166,8 +169,8 @@ class AnsibleHcloudServerTypeInfo(Hcloud): else: self.hcloud_server_type_info = self.client.server_types.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(): diff --git a/plugins/modules/hcloud_ssh_key.py b/plugins/modules/hcloud_ssh_key.py index 3a96fc4..4f5bfdf 100644 --- a/plugins/modules/hcloud_ssh_key.py +++ b/plugins/modules/hcloud_ssh_key.py @@ -113,6 +113,9 @@ hcloud_ssh_key: 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 AnsibleHcloudSSHKey(Hcloud): @@ -138,8 +141,8 @@ class AnsibleHcloudSSHKey(Hcloud): elif self.module.params.get("name") is not None: self.hcloud_ssh_key = self.client.ssh_keys.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_ssh_key(self): self.module.fail_on_missing_params(required_params=["name", "public_key"]) @@ -152,8 +155,8 @@ class AnsibleHcloudSSHKey(Hcloud): if not self.module.check_mode: try: self.client.ssh_keys.create(**params) - 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_ssh_key() @@ -186,8 +189,8 @@ class AnsibleHcloudSSHKey(Hcloud): if not self.module.check_mode: try: self.client.ssh_keys.delete(self.hcloud_ssh_key) - 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_ssh_key = None diff --git a/plugins/modules/hcloud_ssh_key_info.py b/plugins/modules/hcloud_ssh_key_info.py index bb54590..f40b629 100644 --- a/plugins/modules/hcloud_ssh_key_info.py +++ b/plugins/modules/hcloud_ssh_key_info.py @@ -77,6 +77,9 @@ hcloud_ssh_key_info: 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 AnsibleHcloudSSHKeyInfo(Hcloud): @@ -117,8 +120,8 @@ class AnsibleHcloudSSHKeyInfo(Hcloud): else: self.hcloud_ssh_key_info = self.client.ssh_keys.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(): diff --git a/plugins/modules/hcloud_subnetwork.py b/plugins/modules/hcloud_subnetwork.py index b4cb12f..355f1d5 100644 --- a/plugins/modules/hcloud_subnetwork.py +++ b/plugins/modules/hcloud_subnetwork.py @@ -127,6 +127,9 @@ hcloud_subnetwork: 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, +) from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.networks.domain import ( NetworkSubnet, ) @@ -152,8 +155,8 @@ class AnsibleHcloudSubnetwork(Hcloud): try: self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("network")) self.hcloud_subnetwork = None - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def _get_subnetwork(self): subnet_ip_range = self.module.params.get("ip_range") @@ -174,8 +177,8 @@ class AnsibleHcloudSubnetwork(Hcloud): if not self.module.check_mode: try: self.hcloud_network.add_subnet(subnet=NetworkSubnet(**params)).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_network() @@ -194,8 +197,8 @@ class AnsibleHcloudSubnetwork(Hcloud): if not self.module.check_mode: try: self.hcloud_network.delete_subnet(self.hcloud_subnetwork).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.hcloud_subnetwork = None diff --git a/plugins/modules/hcloud_volume.py b/plugins/modules/hcloud_volume.py index aa68c18..bad7ef4 100644 --- a/plugins/modules/hcloud_volume.py +++ b/plugins/modules/hcloud_volume.py @@ -160,6 +160,9 @@ hcloud_volume: 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 AnsibleHcloudVolume(Hcloud): @@ -189,8 +192,8 @@ class AnsibleHcloudVolume(Hcloud): self.hcloud_volume = self.client.volumes.get_by_id(self.module.params.get("id")) else: self.hcloud_volume = self.client.volumes.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_volume(self): self.module.fail_on_missing_params(required_params=["name", "size"]) @@ -217,8 +220,8 @@ class AnsibleHcloudVolume(Hcloud): if delete_protection is not None: self._get_volume() self.hcloud_volume.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_volume() @@ -260,8 +263,8 @@ class AnsibleHcloudVolume(Hcloud): self._mark_as_changed() self._get_volume() - except Exception as e: - self.module.fail_json(msg=e.message) + except HCloudException as e: + self.fail_json_hcloud(e) def present_volume(self): self._get_volume() @@ -280,8 +283,8 @@ class AnsibleHcloudVolume(Hcloud): self.client.volumes.delete(self.hcloud_volume) self._mark_as_changed() self.hcloud_volume = 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(): diff --git a/plugins/modules/hcloud_volume_info.py b/plugins/modules/hcloud_volume_info.py index 3ffe9b5..7b110c6 100644 --- a/plugins/modules/hcloud_volume_info.py +++ b/plugins/modules/hcloud_volume_info.py @@ -94,6 +94,9 @@ hcloud_volume_info: 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 AnsibleHcloudVolumeInfo(Hcloud): @@ -137,8 +140,8 @@ class AnsibleHcloudVolumeInfo(Hcloud): else: self.hcloud_volume_info = self.client.volumes.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(): diff --git a/tests/unit/module_utils/test_hcloud.py b/tests/unit/module_utils/test_hcloud.py new file mode 100644 index 0000000..798a3ad --- /dev/null +++ b/tests/unit/module_utils/test_hcloud.py @@ -0,0 +1,123 @@ +import traceback +from datetime import datetime, timezone +from unittest.mock import MagicMock + +from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud +from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud import ( + APIException, +) +from ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud.actions.domain import ( + Action, + ActionException, + ActionFailedException, + ActionTimeoutException, +) + + +def test_hcloud_fail_json_hcloud(): + module = MagicMock() + module.params = { + "api_token": "fake_token", + "endpoint": "https://api.hetzner.cloud/v1", + } + + hcloud = Hcloud(module, "hcloud_test") + + try: + raise APIException( + code="invalid_input", + message="invalid input in fields 'server', 'home_location'", + details={ + "fields": [ + {"messages": ["either server or home_location must be provided"], "name": "server"}, + {"messages": ["either server or home_location must be provided"], "name": "home_location"}, + ] + }, + ) + except APIException as exception: + hcloud.fail_json_hcloud(exception) + + module.fail_json.assert_called_with( + msg="invalid input in fields 'server', 'home_location'", + exception=traceback.format_exc(), + failure={ + "message": "invalid input in fields 'server', 'home_location'", + "code": "invalid_input", + "details": { + "fields": [ + {"messages": ["either server or home_location must be provided"], "name": "server"}, + {"messages": ["either server or home_location must be provided"], "name": "home_location"}, + ] + }, + }, + ) + + try: + raise ActionFailedException( + action=Action( + **{ + "id": 1084730887, + "command": "change_server_type", + "status": "error", + "progress": 100, + "resources": [{"id": 34574042, "type": "server"}], + "error": {"code": "server_does_not_exist_anymore", "message": "Server does not exist anymore"}, + "started": "2023-07-06T14:52:42+00:00", + "finished": "2023-07-06T14:53:08+00:00", + } + ) + ) + except ActionException as exception: + hcloud.fail_json_hcloud(exception) + + module.fail_json.assert_called_with( + msg="The pending action failed: Server does not exist anymore", + exception=traceback.format_exc(), + failure={ + "action": { + "id": 1084730887, + "command": "change_server_type", + "status": "error", + "progress": 100, + "resources": [{"id": 34574042, "type": "server"}], + "error": {"code": "server_does_not_exist_anymore", "message": "Server does not exist anymore"}, + "started": datetime(2023, 7, 6, 14, 52, 42, tzinfo=timezone.utc), + "finished": datetime(2023, 7, 6, 14, 53, 8, tzinfo=timezone.utc), + } + }, + ) + + try: + raise ActionTimeoutException( + action=Action( + **{ + "id": 1084659545, + "command": "create_server", + "status": "running", + "progress": 50, + "started": "2023-07-06T13:58:38+00:00", + "finished": None, + "resources": [{"id": 34572291, "type": "server"}], + "error": None, + } + ) + ) + except ActionException as exception: + hcloud.fail_json_hcloud(exception) + + module.fail_json.assert_called_with( + msg="The pending action timed out", + exception=traceback.format_exc(), + failure={ + "action": { + "id": 1084659545, + "command": "create_server", + "status": "running", + "progress": 50, + "resources": [{"id": 34572291, "type": "server"}], + "error": None, + "started": datetime(2023, 7, 6, 13, 58, 38, tzinfo=timezone.utc), + "finished": None, + } + }, + )