cloud_server Improve error handling when using not existing server types (#81)

This commit is contained in:
Lukas Kämmerling 2021-04-22 10:24:41 +02:00 committed by GitHub
parent 04ef10041e
commit c5e8e980f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- hcloud_server Improve error handling when using not existing server types

View file

@ -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":

View file

@ -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 }}"