mirror of
https://github.com/ansible-collections/hetzner.hcloud
synced 2025-01-06 01:08:49 +00:00
cloud_server Improve error handling when using not existing server types (#81)
This commit is contained in:
parent
04ef10041e
commit
c5e8e980f6
3 changed files with 31 additions and 4 deletions
2
changelogs/fragments/hcloud-server-server-type.yaml
Normal file
2
changelogs/fragments/hcloud-server-server-type.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- hcloud_server Improve error handling when using not existing server types
|
|
@ -321,9 +321,7 @@ class AnsibleHcloudServer(Hcloud):
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
"name": self.module.params.get("name"),
|
"name": self.module.params.get("name"),
|
||||||
"server_type": self.client.server_types.get_by_name(
|
"server_type": self._get_server_type(),
|
||||||
self.module.params.get("server_type")
|
|
||||||
),
|
|
||||||
"user_data": self.module.params.get("user_data"),
|
"user_data": self.module.params.get("user_data"),
|
||||||
"labels": self.module.params.get("labels"),
|
"labels": self.module.params.get("labels"),
|
||||||
"image": self._get_image()
|
"image": self._get_image()
|
||||||
|
@ -415,6 +413,18 @@ class AnsibleHcloudServer(Hcloud):
|
||||||
) % (image.name, available_until.strftime('%Y-%m-%d')))
|
) % (image.name, available_until.strftime('%Y-%m-%d')))
|
||||||
return image
|
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):
|
def _update_server(self):
|
||||||
try:
|
try:
|
||||||
rescue_mode = self.module.params.get("rescue_mode")
|
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.
|
) # When we upgrade the disk too the resize progress takes some more time.
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
self.hcloud_server.change_type(
|
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"),
|
upgrade_disk=self.module.params.get("upgrade_disk"),
|
||||||
).wait_until_finished(timeout)
|
).wait_until_finished(timeout)
|
||||||
if state == "present" and previous_server_status == Server.STATUS_RUNNING or state == "started":
|
if state == "present" and previous_server_status == Server.STATUS_RUNNING or state == "started":
|
||||||
|
|
|
@ -20,6 +20,21 @@
|
||||||
that:
|
that:
|
||||||
- result is failed
|
- result is failed
|
||||||
- 'result.msg == "missing required arguments: server_type, image"'
|
- '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
|
- name: test create server with not existing image
|
||||||
hcloud_server:
|
hcloud_server:
|
||||||
name: "{{ hcloud_server_name }}"
|
name: "{{ hcloud_server_name }}"
|
||||||
|
|
Loading…
Reference in a new issue