1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-04 08:01:49 +00:00

feat(primary_ip): add reassign/unassign capabilities to primary_ip

This commit is contained in:
shaerpour 2025-12-29 16:42:23 +03:30
parent 4dae1c7100
commit e4391cef9b
No known key found for this signature in database
GPG key ID: D89285D72B39ED8F
4 changed files with 79 additions and 0 deletions

View file

@ -273,6 +273,24 @@ class AnsiblePrimaryIP(AnsibleHCloud):
need_reload = True
self._mark_as_changed()
if (value := self.module.params.get("server")) is not None:
server: BoundServer = self._client_get_by_name_or_id("servers", value)
server_id = server.id
if self.primary_ip.assignee_id and server_id == self.primary_ip.assignee_id:
return
if not self.module.check_mode:
action = self.primary_ip.assign(server_id, "server")
action.wait_until_finished()
self._mark_as_changed()
need_reload = True
else:
if self.primary_ip.assignee_id:
if not self.module.check_mode:
action = self.primary_ip.unassign()
action.wait_until_finished()
self._mark_as_changed()
need_reload = True
params = {}
if (value := self.module.params.get("auto_delete")) is not None:

View file

@ -4,6 +4,11 @@
name: "{{ hcloud_server_name }}"
state: absent
- name: Cleanup test_server2
hetzner.hcloud.server:
name: "{{ hcloud_server_name }}2"
state: absent
- name: Cleanup test_primary_ip
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"

View file

@ -9,3 +9,14 @@
enable_ipv4: false
enable_ipv6: false
register: test_server
- name: Create test_server2
hetzner.hcloud.server:
name: "{{ hcloud_server_name }}2"
server_type: "{{ hcloud_server_type_name }}"
image: "{{ hcloud_image_name }}"
location: "{{ hcloud_location_name }}"
state: created
enable_ipv4: false
enable_ipv6: false
register: test_server2

View file

@ -99,6 +99,25 @@
that:
- result is not changed
- name: Test unassign if server is null
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"
register: result
- name: Verify unassign if server is null
ansible.builtin.assert:
that:
- result is changed
- result.hcloud_primary_ip.assignee_id is none
- name: Test unassign if server is null idempotency
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"
register: result
- name: Verify unassign if server is null idempotency
ansible.builtin.assert:
that:
- result is not changed
- name: Test update delete protection
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"
@ -169,6 +188,32 @@
that:
- result is not changed
- name: Test assign to new server
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"
type: ipv6
server: "{{ hcloud_server_name }}2"
register: result
- name: Verify assign to new server
ansible.builtin.assert:
that:
- result is changed
- result.hcloud_primary_ip.name == hcloud_primary_ip_name
- result.hcloud_primary_ip.type == "ipv6"
- result.hcloud_primary_ip.assignee_id == test_server2.hcloud_server.id
- result.hcloud_primary_ip.assignee_type == "server"
- name: Test assign to new server idempotency
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"
type: ipv6
server: "{{ hcloud_server_name }}2"
register: result
- name: Verify assign to new server idempotency
ansible.builtin.assert:
that:
- result is not changed
- name: Test delete with server
hetzner.hcloud.primary_ip:
name: "{{ hcloud_primary_ip_name }}"