mirror of
https://github.com/ansible-collections/hetzner.hcloud
synced 2024-12-12 21:42:35 +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 = {
|
||||
"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":
|
||||
|
|
|
@ -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 }}"
|
||||
|
|
Loading…
Reference in a new issue