mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-03 23:51:48 +00:00
fix: unassign primary ip and floating ip before deletion (#796)
##### SUMMARY Explicitly unassign Primary IP and Floating IPs before deleting them. This allows us to better handle any errors that happen during the unassign and is consistent with the flow in our Terraform provider. ##### ISSUE TYPE - Bugfix Pull Request ##### COMPONENT NAME <!--- Write the short name of the module, plugin, task or feature below --> - `floating_ip` - `primary_ip` --------- Co-authored-by: jo <ljonas@riseup.net>
This commit is contained in:
parent
ecf2b2b00d
commit
241f61338b
3 changed files with 31 additions and 11 deletions
3
changelogs/fragments/ip-unassign.yaml
Normal file
3
changelogs/fragments/ip-unassign.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
minor_changes:
|
||||
- floating_ip - Unassign Floating IP before deleting it.
|
||||
- primary_ip - Unassign Primary IP before deleting it.
|
||||
|
|
@ -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