From f2b9ccdacb7054a87e33b0e64f6bbcdbf682fce6 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 28 Apr 2025 10:47:37 +0200 Subject: [PATCH] rename server_volume to volume attachment --- ...{server_volume.py => volume_attachment.py} | 35 ++++++++------- .../aliases | 0 .../defaults/main/common.yml | 0 .../defaults/main/main.yml | 0 .../tasks/cleanup.yml | 0 .../tasks/main.yml | 0 .../tasks/test.yml | 44 +++++++++---------- 7 files changed, 41 insertions(+), 38 deletions(-) rename plugins/modules/{server_volume.py => volume_attachment.py} (82%) rename tests/integration/targets/{server_volume => volume_attachment}/aliases (100%) rename tests/integration/targets/{server_volume => volume_attachment}/defaults/main/common.yml (100%) rename tests/integration/targets/{server_volume => volume_attachment}/defaults/main/main.yml (100%) rename tests/integration/targets/{server_volume => volume_attachment}/tasks/cleanup.yml (100%) rename tests/integration/targets/{server_volume => volume_attachment}/tasks/main.yml (100%) rename tests/integration/targets/{server_volume => volume_attachment}/tasks/test.yml (81%) diff --git a/plugins/modules/server_volume.py b/plugins/modules/volume_attachment.py similarity index 82% rename from plugins/modules/server_volume.py rename to plugins/modules/volume_attachment.py index 99fd32e..9906044 100644 --- a/plugins/modules/server_volume.py +++ b/plugins/modules/volume_attachment.py @@ -8,7 +8,7 @@ from __future__ import annotations DOCUMENTATION = """ --- -module: server_volume +module: volume_attachment short_description: Manage the relationship between Hetzner Cloud Volumes and Servers @@ -48,18 +48,18 @@ extends_documentation_fragment: EXAMPLES = """ - name: Attach my-volume to my-server - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: my-volume server: my-server - name: Detach my-volume from my-server - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: my-volume server: my-server state: absent - name: Attach my-volume using id to my-server with automount enabled - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: 123456 server: my-server automount: true @@ -67,7 +67,7 @@ EXAMPLES = """ """ RETURN = """ -hcloud_server_volume: +hcloud_volume_attachment: description: The relationship between a Server and a Volume returned: always type: complex @@ -92,21 +92,24 @@ from ..module_utils.vendor.hcloud.servers import BoundServer from ..module_utils.vendor.hcloud.volumes import BoundVolume -class AnsibleHCloudServerVolume(AnsibleHCloud): - represent = "hcloud_server_volume" +class AnsibleHcloudVolumeAttachment(AnsibleHCloud): + represent = "hcloud_volume_attachment" + hcloud_volume: BoundVolume | None = None hcloud_server: BoundServer | None = None - hcloud_server_volume: BoundVolume | None = None def _prepare_result(self): return { - "volume": self.hcloud_server_volume.name, + "volume": self.hcloud_volume.name, "server": self.hcloud_server.name, } def _get_server_and_volume(self): try: - self.hcloud_server_volume = self._client_get_by_name_or_id("volumes", self.module.params.get("volume")) + self.hcloud_volume = self._client_get_by_name_or_id( + "volumes", + self.module.params.get("volume"), + ) self.hcloud_server = self._client_get_by_name_or_id( "servers", @@ -120,10 +123,10 @@ class AnsibleHCloudServerVolume(AnsibleHCloud): self._get_server_and_volume() server_name = self.module.params.get("server") server = self.client.servers.get_by_name(server_name) - if self.hcloud_server_volume.server is None or self.hcloud_server.name != server.name: + if self.hcloud_volume.server is None or self.hcloud_server.name != server.name: if not self.module.check_mode: automount = self.module.params.get("automount", False) - action = self.hcloud_server_volume.attach(server=server, automount=automount) + action = self.hcloud_volume.attach(server=server, automount=automount) action.wait_until_finished() self._mark_as_changed() except HCloudException as exception: @@ -132,9 +135,9 @@ class AnsibleHCloudServerVolume(AnsibleHCloud): def detach_volume(self): try: self._get_server_and_volume() - if self.hcloud_server_volume.server is not None: + if self.hcloud_volume.server is not None: if not self.module.check_mode: - action = self.hcloud_server_volume.detach() + action = self.hcloud_volume.detach() action.wait_until_finished() self._mark_as_changed() except HCloudException as exception: @@ -158,9 +161,9 @@ class AnsibleHCloudServerVolume(AnsibleHCloud): def main(): - module = AnsibleHCloudServerVolume.define_module() + module = AnsibleHcloudVolumeAttachment.define_module() - hcloud = AnsibleHCloudServerVolume(module) + hcloud = AnsibleHcloudVolumeAttachment(module) state = module.params["state"] if state == "present": hcloud.attach_volume() diff --git a/tests/integration/targets/server_volume/aliases b/tests/integration/targets/volume_attachment/aliases similarity index 100% rename from tests/integration/targets/server_volume/aliases rename to tests/integration/targets/volume_attachment/aliases diff --git a/tests/integration/targets/server_volume/defaults/main/common.yml b/tests/integration/targets/volume_attachment/defaults/main/common.yml similarity index 100% rename from tests/integration/targets/server_volume/defaults/main/common.yml rename to tests/integration/targets/volume_attachment/defaults/main/common.yml diff --git a/tests/integration/targets/server_volume/defaults/main/main.yml b/tests/integration/targets/volume_attachment/defaults/main/main.yml similarity index 100% rename from tests/integration/targets/server_volume/defaults/main/main.yml rename to tests/integration/targets/volume_attachment/defaults/main/main.yml diff --git a/tests/integration/targets/server_volume/tasks/cleanup.yml b/tests/integration/targets/volume_attachment/tasks/cleanup.yml similarity index 100% rename from tests/integration/targets/server_volume/tasks/cleanup.yml rename to tests/integration/targets/volume_attachment/tasks/cleanup.yml diff --git a/tests/integration/targets/server_volume/tasks/main.yml b/tests/integration/targets/volume_attachment/tasks/main.yml similarity index 100% rename from tests/integration/targets/server_volume/tasks/main.yml rename to tests/integration/targets/volume_attachment/tasks/main.yml diff --git a/tests/integration/targets/server_volume/tasks/test.yml b/tests/integration/targets/volume_attachment/tasks/test.yml similarity index 81% rename from tests/integration/targets/server_volume/tasks/test.yml rename to tests/integration/targets/volume_attachment/tasks/test.yml index 2ba7e7e..00912e3 100644 --- a/tests/integration/targets/server_volume/tasks/test.yml +++ b/tests/integration/targets/volume_attachment/tasks/test.yml @@ -26,7 +26,7 @@ - vol_volume is changed - name: Test missing volume name # noqa: args[module] - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: server: "{{ hcloud_server_name }}" register: result ignore_errors: true @@ -37,7 +37,7 @@ - 'result.msg == "missing required arguments: volume"' - name: Test missing server name # noqa: args[module] - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" register: result ignore_errors: true @@ -48,7 +48,7 @@ - 'result.msg == "missing required arguments: server"' - name: Test attach Volume with check mode (Volume) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" server: "{{ hcloud_server_name }}" register: result @@ -59,7 +59,7 @@ - result is changed - name: Test attach Volume (Volume) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" server: "{{ hcloud_server_name }}" register: volume @@ -67,11 +67,11 @@ ansible.builtin.assert: that: - volume is changed - - volume.hcloud_server_volume.volume == hcloud_volume_name - - volume.hcloud_server_volume.server == hcloud_server_name + - volume.hcloud_volume_attachment.volume == hcloud_volume_name + - volume.hcloud_volume_attachment.server == hcloud_server_name - name: Test attach Volume idempotence (Volume) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" server: "{{ hcloud_server_name }}" register: volume @@ -81,7 +81,7 @@ - volume is not changed - name: Test detach Volume with checkmode (Volume) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" server: "{{ hcloud_server_name }}" state: "absent" @@ -91,10 +91,10 @@ ansible.builtin.assert: that: - volume is changed - - volume.hcloud_server_volume.server == hcloud_server_name + - volume.hcloud_volume_attachment.server == hcloud_server_name - name: Test detach Volume (Volume) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" server: "{{ hcloud_server_name }}" state: "absent" @@ -103,10 +103,10 @@ ansible.builtin.assert: that: - volume is changed - - volume.hcloud_server_volume.server == hcloud_server_name + - volume.hcloud_volume_attachment.server == hcloud_server_name - name: Test detach Volume idempotency (Volume) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ hcloud_volume_name }}" server: "{{ hcloud_server_name }}" state: "absent" @@ -117,7 +117,7 @@ - volume is not changed - name: Test attach Volume with check mode (ID) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ vol_volume.hcloud_volume.id }}" server: "{{ hcloud_server_name }}" register: result @@ -128,7 +128,7 @@ - result is changed - name: Test attach Volume (ID) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ vol_volume.hcloud_volume.id }}" server: "{{ hcloud_server_name }}" register: volume @@ -136,11 +136,11 @@ ansible.builtin.assert: that: - volume is changed - - volume.hcloud_server_volume.volume == hcloud_volume_name - - volume.hcloud_server_volume.server == hcloud_server_name + - volume.hcloud_volume_attachment.volume == hcloud_volume_name + - volume.hcloud_volume_attachment.server == hcloud_server_name - name: Test attach Volume idempotence (ID) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ vol_volume.hcloud_volume.id }}" server: "{{ hcloud_server_name }}" register: volume @@ -150,7 +150,7 @@ - volume is not changed - name: Test detach Volume with checkmode (ID) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ vol_volume.hcloud_volume.id }}" server: "{{ hcloud_server_name }}" state: "absent" @@ -160,10 +160,10 @@ ansible.builtin.assert: that: - volume is changed - - volume.hcloud_server_volume.server == hcloud_server_name + - volume.hcloud_volume_attachment.server == hcloud_server_name - name: Test detach Volume (ID) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ vol_volume.hcloud_volume.id }}" server: "{{ hcloud_server_name }}" state: "absent" @@ -172,10 +172,10 @@ ansible.builtin.assert: that: - volume is changed - - volume.hcloud_server_volume.server == hcloud_server_name + - volume.hcloud_volume_attachment.server == hcloud_server_name - name: Test detach Volume idempotency (ID) - hetzner.hcloud.server_volume: + hetzner.hcloud.volume_attachment: volume: "{{ vol_volume.hcloud_volume.id }}" server: "{{ hcloud_server_name }}" state: "absent"