1
0
Fork 0
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 (#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:
Julian Tölle 2026-01-29 11:10:01 +01:00 committed by GitHub
parent ecf2b2b00d
commit 241f61338b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 11 deletions

View file

@ -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

View file

@ -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)