1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-03 23:51:48 +00:00
This commit is contained in:
Amirhossein Shaerpour 2026-02-01 16:08:03 +03:30 committed by GitHub
commit 50f265fced
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,3 @@
minor_changes:
- primary_ip - Unassign primary_ip if ``server`` is null.
- primary_ip - Assign to new server if ``server`` is changed.

View file

@ -273,6 +273,25 @@ class AnsiblePrimaryIP(AnsibleHCloud):
need_reload = True
self._mark_as_changed()
if self.module.param_is_defined("server"):
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 }}"