Fix Floating IP assignment is not idempotent (#32)

This commit is contained in:
ohdearaugustin 2020-10-05 11:09:00 +02:00 committed by GitHub
parent fe84174cb6
commit e59b91ca86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- hcloud_floating_ip Fix idempotency when floating ip is assigned to server

View file

@ -259,17 +259,24 @@ class AnsibleHcloudFloatingIP(Hcloud):
self._mark_as_changed()
server = self.module.params.get("server")
if server is not None:
if self.module.params.get("force") or self.hcloud_floating_ip.server is None:
if server is not None and self.hcloud_floating_ip.server is not None:
if self.module.params.get("force") and server != self.hcloud_floating_ip.server.name:
if not self.module.check_mode:
self.hcloud_floating_ip.assign(
self.client.servers.get_by_name(self.module.params.get("server"))
self.client.servers.get_by_name(server)
)
else:
self._mark_as_changed()
elif server != self.hcloud_floating_ip.server.name:
self.module.warn(
"Floating IP is already assigned to server %s. You need to unassign the Floating IP or use force=yes."
"Floating IP is already assigned to another server %s. You need to unassign the Floating IP or use force=yes."
% self.hcloud_floating_ip.server.name
)
self._mark_as_changed()
elif server is not None and self.hcloud_floating_ip.server is None:
if not self.module.check_mode:
self.hcloud_floating_ip.assign(
self.client.servers.get_by_name(server)
)
self._mark_as_changed()
elif server is None and self.hcloud_floating_ip.server is not None:
if not self.module.check_mode:

View file

@ -213,6 +213,18 @@
- floatingIP is changed
- floatingIP.hcloud_floating_ip.server == "{{ main_server.hcloud_server.name }}"
- name: test assign Floating IP idempotency
hcloud_floating_ip:
name: "{{ hcloud_floating_ip_name }}"
description: "changed-description"
type: ipv4
server: "{{ main_server.hcloud_server.name }}"
register: floatingIP
- name: verify test unassign Floating IPidempotency
assert:
that:
- floatingIP is not changed
- name: test unassign Floating IP
hcloud_floating_ip:
name: "{{ hcloud_floating_ip_name }}"