From e461a890fa6c941d10f92d9039c66bb1041073ef Mon Sep 17 00:00:00 2001 From: Jonas L Date: Wed, 16 Aug 2023 16:14:55 +0200 Subject: [PATCH] fix: consistently fail on invalid ID in `*_info` modules (#301) * fix: consistently fail on invalid ID in `*_info` modules * remove unused import --- .../fragments/consistently-fail-on-invalid-id.yml | 2 ++ .../fix-error-on-location_info-invalid-id.yml | 2 -- plugins/modules/hcloud_certificate_info.py | 1 + plugins/modules/hcloud_datacenter_info.py | 1 + plugins/modules/hcloud_floating_ip_info.py | 1 + plugins/modules/hcloud_image_info.py | 1 + plugins/modules/hcloud_iso_info.py | 1 + plugins/modules/hcloud_load_balancer_info.py | 1 + plugins/modules/hcloud_load_balancer_type_info.py | 1 + plugins/modules/hcloud_location_info.py | 10 +++------- plugins/modules/hcloud_network_info.py | 1 + plugins/modules/hcloud_primary_ip_info.py | 1 + plugins/modules/hcloud_server_info.py | 1 + plugins/modules/hcloud_server_type_info.py | 1 + plugins/modules/hcloud_ssh_key_info.py | 1 + plugins/modules/hcloud_volume_info.py | 1 + .../targets/hcloud_location_info/tasks/test.yml | 3 ++- .../targets/hcloud_network_info/tasks/test.yml | 5 +++-- .../targets/hcloud_server_info/tasks/test.yml | 5 +++-- .../targets/hcloud_volume_info/tasks/test.yml | 5 +++-- 20 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 changelogs/fragments/consistently-fail-on-invalid-id.yml delete mode 100644 changelogs/fragments/fix-error-on-location_info-invalid-id.yml diff --git a/changelogs/fragments/consistently-fail-on-invalid-id.yml b/changelogs/fragments/consistently-fail-on-invalid-id.yml new file mode 100644 index 0000000..6ec403e --- /dev/null +++ b/changelogs/fragments/consistently-fail-on-invalid-id.yml @@ -0,0 +1,2 @@ +bugfixes: + - "`*_info` - Consistently fail on invalid ID in `*_info` modules." diff --git a/changelogs/fragments/fix-error-on-location_info-invalid-id.yml b/changelogs/fragments/fix-error-on-location_info-invalid-id.yml deleted file mode 100644 index da609a9..0000000 --- a/changelogs/fragments/fix-error-on-location_info-invalid-id.yml +++ /dev/null @@ -1,2 +0,0 @@ -bugfixes: - - hcloud_location_info - Do not error when querying a location using an invalid id. diff --git a/plugins/modules/hcloud_certificate_info.py b/plugins/modules/hcloud_certificate_info.py index 89a98a2..b379126 100644 --- a/plugins/modules/hcloud_certificate_info.py +++ b/plugins/modules/hcloud_certificate_info.py @@ -16,6 +16,7 @@ options: id: description: - The ID of the certificate you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_datacenter_info.py b/plugins/modules/hcloud_datacenter_info.py index 97a5f7c..a319346 100644 --- a/plugins/modules/hcloud_datacenter_info.py +++ b/plugins/modules/hcloud_datacenter_info.py @@ -20,6 +20,7 @@ options: id: description: - The ID of the datacenter you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_floating_ip_info.py b/plugins/modules/hcloud_floating_ip_info.py index 20b9a82..0cc120e 100644 --- a/plugins/modules/hcloud_floating_ip_info.py +++ b/plugins/modules/hcloud_floating_ip_info.py @@ -20,6 +20,7 @@ options: id: description: - The ID of the Floating IP you want to get. + - The module will fail if the provided ID is invalid. type: int label_selector: description: diff --git a/plugins/modules/hcloud_image_info.py b/plugins/modules/hcloud_image_info.py index 6fae6df..5eabee1 100644 --- a/plugins/modules/hcloud_image_info.py +++ b/plugins/modules/hcloud_image_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the image you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_iso_info.py b/plugins/modules/hcloud_iso_info.py index db289c4..2f405ab 100644 --- a/plugins/modules/hcloud_iso_info.py +++ b/plugins/modules/hcloud_iso_info.py @@ -22,6 +22,7 @@ options: id: description: - The ID of the ISO image you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_load_balancer_info.py b/plugins/modules/hcloud_load_balancer_info.py index 56a16c0..9c7994c 100644 --- a/plugins/modules/hcloud_load_balancer_info.py +++ b/plugins/modules/hcloud_load_balancer_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the Load Balancers you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_load_balancer_type_info.py b/plugins/modules/hcloud_load_balancer_type_info.py index 44f8f83..716bd92 100644 --- a/plugins/modules/hcloud_load_balancer_type_info.py +++ b/plugins/modules/hcloud_load_balancer_type_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the Load Balancer type you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_location_info.py b/plugins/modules/hcloud_location_info.py index 15ae305..764f8f9 100644 --- a/plugins/modules/hcloud_location_info.py +++ b/plugins/modules/hcloud_location_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the location you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: @@ -78,7 +79,7 @@ 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 +from ..module_utils.vendor.hcloud import HCloudException class AnsibleHCloudLocationInfo(AnsibleHCloud): @@ -105,12 +106,7 @@ class AnsibleHCloudLocationInfo(AnsibleHCloud): def get_locations(self): try: if self.module.params.get("id") is not None: - try: - self.hcloud_location_info = [self.client.locations.get_by_id(self.module.params.get("id"))] - except APIException as exception: - self.hcloud_location_info = [] - if exception.code != "not_found": - raise exception + self.hcloud_location_info = [self.client.locations.get_by_id(self.module.params.get("id"))] elif self.module.params.get("name") is not None: self.hcloud_location_info = [self.client.locations.get_by_name(self.module.params.get("name"))] else: diff --git a/plugins/modules/hcloud_network_info.py b/plugins/modules/hcloud_network_info.py index 6c5c654..db96da4 100644 --- a/plugins/modules/hcloud_network_info.py +++ b/plugins/modules/hcloud_network_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the network you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_primary_ip_info.py b/plugins/modules/hcloud_primary_ip_info.py index c450b9e..1e88c7d 100644 --- a/plugins/modules/hcloud_primary_ip_info.py +++ b/plugins/modules/hcloud_primary_ip_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the Primary IP you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_server_info.py b/plugins/modules/hcloud_server_info.py index 60d586b..06d9cad 100644 --- a/plugins/modules/hcloud_server_info.py +++ b/plugins/modules/hcloud_server_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the server you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_server_type_info.py b/plugins/modules/hcloud_server_type_info.py index d901b27..26e74bf 100644 --- a/plugins/modules/hcloud_server_type_info.py +++ b/plugins/modules/hcloud_server_type_info.py @@ -21,6 +21,7 @@ options: id: description: - The ID of the server type you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_ssh_key_info.py b/plugins/modules/hcloud_ssh_key_info.py index f23e7f2..36766f8 100644 --- a/plugins/modules/hcloud_ssh_key_info.py +++ b/plugins/modules/hcloud_ssh_key_info.py @@ -16,6 +16,7 @@ options: id: description: - The ID of the ssh key you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/plugins/modules/hcloud_volume_info.py b/plugins/modules/hcloud_volume_info.py index c172dda..17fd197 100644 --- a/plugins/modules/hcloud_volume_info.py +++ b/plugins/modules/hcloud_volume_info.py @@ -20,6 +20,7 @@ options: id: description: - The ID of the Volume you want to get. + - The module will fail if the provided ID is invalid. type: int name: description: diff --git a/tests/integration/targets/hcloud_location_info/tasks/test.yml b/tests/integration/targets/hcloud_location_info/tasks/test.yml index d7e3a18..d3d0eb8 100644 --- a/tests/integration/targets/hcloud_location_info/tasks/test.yml +++ b/tests/integration/targets/hcloud_location_info/tasks/test.yml @@ -49,7 +49,8 @@ hetzner.hcloud.hcloud_location_info: id: 4711 register: result + ignore_errors: true - name: Verify hcloud_location_info with wrong id ansible.builtin.assert: that: - - result.hcloud_location_info | list | count == 0 + - result is failed diff --git a/tests/integration/targets/hcloud_network_info/tasks/test.yml b/tests/integration/targets/hcloud_network_info/tasks/test.yml index 6ed5259..505e48e 100644 --- a/tests/integration/targets/hcloud_network_info/tasks/test.yml +++ b/tests/integration/targets/hcloud_network_info/tasks/test.yml @@ -103,12 +103,13 @@ - name: test gather hcloud network info with wrong id hetzner.hcloud.hcloud_network_info: - name: "4711" + id: "4711" register: hcloud_network + ignore_errors: true - name: verify test gather hcloud network with wrong id assert: that: - - hcloud_network.hcloud_network_info | list | count == 0 + - hcloud_network is failed - name: cleanup hetzner.hcloud.hcloud_network: diff --git a/tests/integration/targets/hcloud_server_info/tasks/test.yml b/tests/integration/targets/hcloud_server_info/tasks/test.yml index 7a01a1a..abc8c32 100644 --- a/tests/integration/targets/hcloud_server_info/tasks/test.yml +++ b/tests/integration/targets/hcloud_server_info/tasks/test.yml @@ -82,12 +82,13 @@ - name: test gather hcloud server infos with wrong id hetzner.hcloud.hcloud_server_info: - name: "4711" + id: "4711" register: server + ignore_errors: true - name: verify test gather hcloud server infos with wrong id assert: that: - - server.hcloud_server_info | list | count == 0 + - server is failed - name: cleanup hetzner.hcloud.hcloud_server: diff --git a/tests/integration/targets/hcloud_volume_info/tasks/test.yml b/tests/integration/targets/hcloud_volume_info/tasks/test.yml index e08605b..513f334 100644 --- a/tests/integration/targets/hcloud_volume_info/tasks/test.yml +++ b/tests/integration/targets/hcloud_volume_info/tasks/test.yml @@ -83,12 +83,13 @@ - name: test gather hcloud volume infos with wrong id hetzner.hcloud.hcloud_volume_info: - name: "4711" + id: "4711" register: hcloud_volume + ignore_errors: true - name: verify test gather hcloud volume infos with wrong id assert: that: - - hcloud_volume.hcloud_volume_info | list | count == 0 + - hcloud_volume is failed - name: cleanup hetzner.hcloud.hcloud_volume: