From c5e8e980f6d67fac132347cf93eac1c5436b6515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 22 Apr 2021 10:24:41 +0200 Subject: [PATCH] cloud_server Improve error handling when using not existing server types (#81) --- .../fragments/hcloud-server-server-type.yaml | 2 ++ plugins/modules/hcloud_server.py | 18 ++++++++++++++---- .../targets/hcloud_server/tasks/main.yml | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/hcloud-server-server-type.yaml diff --git a/changelogs/fragments/hcloud-server-server-type.yaml b/changelogs/fragments/hcloud-server-server-type.yaml new file mode 100644 index 0000000..2e0d192 --- /dev/null +++ b/changelogs/fragments/hcloud-server-server-type.yaml @@ -0,0 +1,2 @@ +bugfixes: + - hcloud_server Improve error handling when using not existing server types diff --git a/plugins/modules/hcloud_server.py b/plugins/modules/hcloud_server.py index ff5a464..9b447cf 100644 --- a/plugins/modules/hcloud_server.py +++ b/plugins/modules/hcloud_server.py @@ -321,9 +321,7 @@ class AnsibleHcloudServer(Hcloud): params = { "name": self.module.params.get("name"), - "server_type": self.client.server_types.get_by_name( - self.module.params.get("server_type") - ), + "server_type": self._get_server_type(), "user_data": self.module.params.get("user_data"), "labels": self.module.params.get("labels"), "image": self._get_image() @@ -415,6 +413,18 @@ class AnsibleHcloudServer(Hcloud): ) % (image.name, available_until.strftime('%Y-%m-%d'))) return image + def _get_server_type(self): + server_type = self.client.server_types.get_by_name( + self.module.params.get("server_type") + ) + 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')) + + return server_type + def _update_server(self): try: rescue_mode = self.module.params.get("rescue_mode") @@ -491,7 +501,7 @@ class AnsibleHcloudServer(Hcloud): ) # When we upgrade the disk too the resize progress takes some more time. if not self.module.check_mode: self.hcloud_server.change_type( - server_type=self.client.server_types.get_by_name(server_type), + server_type=self._get_server_type(), upgrade_disk=self.module.params.get("upgrade_disk"), ).wait_until_finished(timeout) if state == "present" and previous_server_status == Server.STATUS_RUNNING or state == "started": diff --git a/tests/integration/targets/hcloud_server/tasks/main.yml b/tests/integration/targets/hcloud_server/tasks/main.yml index 783dede..526f74b 100644 --- a/tests/integration/targets/hcloud_server/tasks/main.yml +++ b/tests/integration/targets/hcloud_server/tasks/main.yml @@ -20,6 +20,21 @@ that: - result is failed - 'result.msg == "missing required arguments: server_type, image"' + +- name: test create server with not existing server type + hcloud_server: + name: "{{ hcloud_server_name }}" + server_type: not-existing-server-type + image: ubuntu-20.04 + state: present + register: result + ignore_errors: yes +- name: verify fail test create server with not existing server type + assert: + that: + - result is failed + - 'result.msg == "server_type not-existing-server-type was not found"' + - name: test create server with not existing image hcloud_server: name: "{{ hcloud_server_name }}"