mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
fix: unassign primary ip and floating ip before deletion
This commit is contained in:
parent
ecf2b2b00d
commit
07dc26c59a
2 changed files with 28 additions and 11 deletions
|
|
@ -281,16 +281,24 @@ class AnsibleFloatingIP(AnsibleHCloud):
|
|||
self.floating_ip = self.floating_ip.update(**params)
|
||||
|
||||
def _delete(self):
|
||||
if self.module.params.get("force") or self.floating_ip.server is None:
|
||||
if self.floating_ip.server is not None:
|
||||
if self.module.params.get("force"):
|
||||
if not self.module.check_mode:
|
||||
action = self.floating_ip.unassign()
|
||||
action.wait_until_finished()
|
||||
|
||||
self.floating_ip.delete()
|
||||
self._mark_as_changed()
|
||||
else:
|
||||
self.module.warn(
|
||||
"Floating IP is currently assigned to server "
|
||||
f"{self.floating_ip.server.name}. You need to "
|
||||
"unassign the Floating IP or use force=true."
|
||||
)
|
||||
else:
|
||||
if not self.module.check_mode:
|
||||
self.floating_ip.delete()
|
||||
self._mark_as_changed()
|
||||
else:
|
||||
self.module.warn(
|
||||
"Floating IP is currently assigned to server "
|
||||
f"{self.floating_ip.server.name}. You need to "
|
||||
"unassign the Floating IP or use force=true."
|
||||
)
|
||||
|
||||
self.floating_ip = None
|
||||
|
||||
|
|
|
|||
|
|
@ -289,6 +289,18 @@ class AnsiblePrimaryIP(AnsibleHCloud):
|
|||
if not self.module.check_mode:
|
||||
self.primary_ip = self.primary_ip.update(**params)
|
||||
|
||||
def _delete(self):
|
||||
if self.primary_ip.assignee_id is not None:
|
||||
if not self.module.check_mode:
|
||||
action = self.primary_ip.unassign()
|
||||
action.wait_until_finished()
|
||||
self._mark_as_changed()
|
||||
|
||||
if not self.module.check_mode:
|
||||
self.primary_ip.delete()
|
||||
self.primary_ip = None
|
||||
self._mark_as_changed()
|
||||
|
||||
def present(self):
|
||||
try:
|
||||
self._get()
|
||||
|
|
@ -303,10 +315,7 @@ class AnsiblePrimaryIP(AnsibleHCloud):
|
|||
try:
|
||||
self._get()
|
||||
if self.primary_ip is not None:
|
||||
if not self.module.check_mode:
|
||||
self.primary_ip.delete()
|
||||
self._mark_as_changed()
|
||||
self.primary_ip = None
|
||||
self._delete()
|
||||
except HCloudException as exception:
|
||||
self.fail_json_hcloud(exception)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue