mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
feat: add volume_attachment module (#622)
##### SUMMARY Added `volume_attachment` as centralized module for attaching and detaching volumes from servers. Fixes #490 ##### ISSUE TYPE - New Module Pull Request ##### COMPONENT NAME `volume_attachment` --------- Co-authored-by: jo <ljonas@riseup.net>
This commit is contained in:
parent
b1fd9aa919
commit
c37cdf0bc6
9 changed files with 444 additions and 0 deletions
3
tests/integration/targets/volume_attachment/aliases
Normal file
3
tests/integration/targets/volume_attachment/aliases
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
cloud/hcloud
|
||||
gather_facts/no
|
||||
azp/group2
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#
|
||||
# DO NOT EDIT THIS FILE! Please edit the files in tests/integration/common instead.
|
||||
#
|
||||
---
|
||||
# Azure Pipelines will configure this value to something similar to
|
||||
# "azp-84824-1-hetzner-2-13-test-2-13-hcloud-3-9-1-default-i"
|
||||
hcloud_prefix: "tests"
|
||||
|
||||
# Used to namespace resources created by concurrent test pipelines/targets
|
||||
hcloud_run_ns: "{{ hcloud_prefix | md5 }}"
|
||||
hcloud_role_ns: "{{ role_name | split('_') | map('batch', 2) | map('first') | flatten() | join() }}"
|
||||
hcloud_ns: "ansible-{{ hcloud_run_ns }}-{{ hcloud_role_ns }}"
|
||||
|
||||
# Used to easily update the server types and images across all our tests.
|
||||
hcloud_server_type_name: cax11
|
||||
hcloud_server_type_id: 45
|
||||
|
||||
hcloud_server_type_upgrade_name: cax21
|
||||
hcloud_server_type_upgrade_id: 93
|
||||
|
||||
hcloud_image_name: debian-12
|
||||
hcloud_image_id: 114690389 # architecture=arm
|
||||
|
||||
hcloud_location_name: hel1
|
||||
hcloud_location_id: 3
|
||||
hcloud_datacenter_name: hel1-dc2
|
||||
hcloud_datacenter_id: 3
|
||||
|
||||
hcloud_network_zone_name: eu-central
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Copyright: (c) 2025, Hetzner Cloud GmbH <info@hetzner-cloud.de>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
---
|
||||
hcloud_volume_name: "{{ hcloud_ns }}"
|
||||
hcloud_server_name: "{{ hcloud_ns }}"
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
---
|
||||
- name: Cleanup test_server
|
||||
hetzner.hcloud.server:
|
||||
name: "{{ hcloud_server_name }}"
|
||||
state: absent
|
||||
|
||||
- name: Cleanup test_server2
|
||||
hetzner.hcloud.server:
|
||||
name: "{{ hcloud_server_name }}2"
|
||||
state: absent
|
||||
register: test_server2
|
||||
|
||||
- name: Cleanup test_volume
|
||||
hetzner.hcloud.volume:
|
||||
name: "{{ hcloud_volume_name }}"
|
||||
state: absent
|
||||
31
tests/integration/targets/volume_attachment/tasks/main.yml
Normal file
31
tests/integration/targets/volume_attachment/tasks/main.yml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#
|
||||
# DO NOT EDIT THIS FILE! Please edit the files in tests/integration/common instead.
|
||||
#
|
||||
---
|
||||
- name: Check if cleanup.yml exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ role_path }}/tasks/cleanup.yml"
|
||||
register: cleanup_file
|
||||
|
||||
- name: Check if prepare.yml exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ role_path }}/tasks/prepare.yml"
|
||||
register: prepare_file
|
||||
|
||||
- name: Include cleanup tasks
|
||||
ansible.builtin.include_tasks: "{{ role_path }}/tasks/cleanup.yml"
|
||||
when: cleanup_file.stat.exists
|
||||
|
||||
- name: Include prepare tasks
|
||||
ansible.builtin.include_tasks: "{{ role_path }}/tasks/prepare.yml"
|
||||
when: prepare_file.stat.exists
|
||||
|
||||
- name: Run tests
|
||||
block:
|
||||
- name: Include test tasks
|
||||
ansible.builtin.include_tasks: "{{ role_path }}/tasks/test.yml"
|
||||
|
||||
always:
|
||||
- name: Include cleanup tasks
|
||||
ansible.builtin.include_tasks: "{{ role_path }}/tasks/cleanup.yml"
|
||||
when: cleanup_file.stat.exists
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
---
|
||||
- name: Create test_server
|
||||
hetzner.hcloud.server:
|
||||
name: "{{ hcloud_server_name }}"
|
||||
server_type: "{{ hcloud_server_type_name }}"
|
||||
image: "{{ hcloud_image_name }}"
|
||||
location: "{{ hcloud_location_name }}"
|
||||
state: created
|
||||
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
|
||||
register: test_server2
|
||||
|
||||
- name: Create test_volume
|
||||
hetzner.hcloud.volume:
|
||||
name: "{{ hcloud_volume_name }}"
|
||||
size: 10
|
||||
location: "{{ hcloud_location_name }}"
|
||||
register: test_volume
|
||||
130
tests/integration/targets/volume_attachment/tasks/test.yml
Normal file
130
tests/integration/targets/volume_attachment/tasks/test.yml
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
# Copyright: (c) 2025, Hetzner Cloud GmbH <info@hetzner-cloud.de>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
---
|
||||
- name: Test missing required parameters # noqa: args[module]
|
||||
hetzner.hcloud.volume_attachment:
|
||||
server: "{{ hcloud_server_name }}"
|
||||
register: result
|
||||
ignore_errors: true
|
||||
- name: Verify missing required parameters
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is failed
|
||||
- 'result.msg == "missing required arguments: volume"'
|
||||
|
||||
- name: Test missing required parameters # noqa: args[module]
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
register: result
|
||||
ignore_errors: true
|
||||
- name: Verify missing required parameters
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is failed
|
||||
- 'result.msg == "missing required arguments: server"'
|
||||
|
||||
- name: Test attach with check mode
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
server: "{{ hcloud_server_name }}"
|
||||
register: result
|
||||
check_mode: true
|
||||
- name: Verify attach with check mode
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.hcloud_volume_attachment.volume == hcloud_volume_name
|
||||
- result.hcloud_volume_attachment.server == hcloud_server_name
|
||||
|
||||
- name: Test attach volume not found
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "invalid"
|
||||
server: "{{ hcloud_server_name }}"
|
||||
register: result
|
||||
ignore_errors: true
|
||||
- name: Verify attach volume not found
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is failed
|
||||
- 'result.msg == "resource (volume) does not exist: invalid"'
|
||||
|
||||
- name: Test attach server not found
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
server: "invalid"
|
||||
register: result
|
||||
ignore_errors: true
|
||||
- name: Verify attach server not found
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is failed
|
||||
- 'result.msg == "resource (server) does not exist: invalid"'
|
||||
|
||||
- name: Test attach
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
server: "{{ hcloud_server_name }}"
|
||||
register: result
|
||||
- name: Verify attach
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.hcloud_volume_attachment.volume == hcloud_volume_name
|
||||
- result.hcloud_volume_attachment.server == hcloud_server_name
|
||||
|
||||
- name: Test attach idempotency
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
server: "{{ hcloud_server_name }}"
|
||||
register: result
|
||||
- name: Verify attach idempotency
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is not changed
|
||||
|
||||
- name: Test attach to another server
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
server: "{{ hcloud_server_name }}2"
|
||||
register: result
|
||||
- name: Verify attach
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.hcloud_volume_attachment.volume == hcloud_volume_name
|
||||
- result.hcloud_volume_attachment.server == hcloud_server_name + '2'
|
||||
|
||||
- name: Test detach with check mode
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
state: "absent"
|
||||
check_mode: true
|
||||
register: result
|
||||
- name: Verify detach with check mode
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.hcloud_volume_attachment.volume == hcloud_volume_name
|
||||
- result.hcloud_volume_attachment.server is none
|
||||
|
||||
- name: Test detach
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
state: "absent"
|
||||
register: result
|
||||
- name: Verify detach
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.hcloud_volume_attachment.volume == hcloud_volume_name
|
||||
- result.hcloud_volume_attachment.server is none
|
||||
|
||||
- name: Test detach idempotency
|
||||
hetzner.hcloud.volume_attachment:
|
||||
volume: "{{ hcloud_volume_name }}"
|
||||
state: "absent"
|
||||
register: result
|
||||
- name: Verify detach idempotency
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- result is not changed
|
||||
Loading…
Add table
Add a link
Reference in a new issue