Improve Error handling on SSH Key creation (#57)

This commit is contained in:
Lukas Kämmerling 2021-02-23 10:05:32 +01:00 committed by GitHub
parent 9af840105a
commit f59c76de2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

View file

@ -156,7 +156,7 @@ class AnsibleHcloudSSHKey(Hcloud):
self.module.params.get("name")
)
except APIException as e:
except Exception as e:
self.module.fail_json(msg=e.message)
def _create_ssh_key(self):
@ -170,7 +170,10 @@ class AnsibleHcloudSSHKey(Hcloud):
}
if not self.module.check_mode:
self.client.ssh_keys.create(**params)
try:
self.client.ssh_keys.create(**params)
except Exception as e:
self.module.fail_json(msg=e.message)
self._mark_as_changed()
self._get_ssh_key()

View file

@ -2,7 +2,7 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
hcloud_prefix: "tests"
hcloud_server_name: "{{hcloud_prefix}}-integration"
hcloud_server_name: "{{hcloud_prefix}}"
hcloud_ssh_key_name: "{{hcloud_prefix}}-integration"
hcloud_ssh_key_public_key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDnaTPfKaX1QKcRLOfr34buVLh5FhJAThI9NYB0xNdXsMd4Y0zLyyCQzHbx4eWCVZxym/s6csWSeLaAhO1GOHeAw3hQFMqf1oTBx6Y8g0pKpeotKPa/PDSUzdZF9Lc+DadtpQd8kFVHAu1Kd3zoEUnk1u6kP7I4qu4Z/6F9qBDF+M3aobiPVxdS7GwaVRW3nZu+FcQDLiBiNOjuRDyjHcDfEUkoh2SOu25RrFtGPzFu5mGmBJwotKpWAocLGfHzyn/fAHxgw3jKZVH/t+XWQFnl82Ie8yE3Z1EZ7oDkNRqFQT9AdXEQOLycTTYTQMJZpgeFTv3sAo6lPRCusiFmmLcf ci@ansible.hetzner.cloud"
hcloud_ssh_key_fingerprint: "56:89:c4:d6:a7:4a:79:82:f4:c2:58:9c:e1:d2:2d:4e"

View file

@ -113,7 +113,7 @@
hcloud_server:
name: "{{ hcloud_server_name }}"
server_type: cx11
image: "ubuntu-18.04"
image: "ubuntu-20.04"
ssh_keys:
- "{{ hcloud_ssh_key_name }}"
state: started
@ -128,11 +128,23 @@
id: "{{ sshKey.hcloud_ssh_key.id }}"
state: absent
register: result
- name: verify absent server
- name: verify absent sshkey
assert:
that:
- result is success
- name: test fail cleanly on double created ssh key
hcloud_ssh_key:
name: "{{ hcloud_ssh_key_name }}othername"
public_key: "{{ hcloud_ssh_key_public_key }}"
register: result
ignore_errors: yes
- name: verify failed correctly
assert:
that:
- result is failed
- 'result.msg == "SSH key with the same fingerprint already exists"'
- name: cleanup
hcloud_server:
name: "{{ hcloud_server_name }}"

View file

@ -43,5 +43,5 @@ pip install pylint
# shellcheck disable=SC2086
ansible-test sanity --color -v --junit ${COVERAGE:+"$COVERAGE"} ${CHANGED:+"$CHANGED"} \
--base-branch "${base_branch}" \
--exclude shippable.yml --exclude tests/utils/ \
--exclude tests/utils/ \
"${options[@]}" --allow-disabled