From e4391cef9b5e35bc25d0cc0ba15eab6d5847a583 Mon Sep 17 00:00:00 2001 From: shaerpour Date: Mon, 29 Dec 2025 16:42:23 +0330 Subject: [PATCH] feat(primary_ip): add reassign/unassign capabilities to primary_ip --- plugins/modules/primary_ip.py | 18 ++++++++ .../targets/primary_ip/tasks/cleanup.yml | 5 +++ .../targets/primary_ip/tasks/prepare.yml | 11 +++++ .../targets/primary_ip/tasks/test.yml | 45 +++++++++++++++++++ 4 files changed, 79 insertions(+) diff --git a/plugins/modules/primary_ip.py b/plugins/modules/primary_ip.py index f19bf24..e1e4d88 100644 --- a/plugins/modules/primary_ip.py +++ b/plugins/modules/primary_ip.py @@ -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: diff --git a/tests/integration/targets/primary_ip/tasks/cleanup.yml b/tests/integration/targets/primary_ip/tasks/cleanup.yml index d8c44b9..d559fcf 100644 --- a/tests/integration/targets/primary_ip/tasks/cleanup.yml +++ b/tests/integration/targets/primary_ip/tasks/cleanup.yml @@ -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 }}" diff --git a/tests/integration/targets/primary_ip/tasks/prepare.yml b/tests/integration/targets/primary_ip/tasks/prepare.yml index ee2d237..c9cd0c3 100644 --- a/tests/integration/targets/primary_ip/tasks/prepare.yml +++ b/tests/integration/targets/primary_ip/tasks/prepare.yml @@ -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 diff --git a/tests/integration/targets/primary_ip/tasks/test.yml b/tests/integration/targets/primary_ip/tasks/test.yml index 6828453..8f477db 100644 --- a/tests/integration/targets/primary_ip/tasks/test.yml +++ b/tests/integration/targets/primary_ip/tasks/test.yml @@ -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 }}"