From 1423840fba32dbc7133e0be36eb23ac84e1eacf2 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Wed, 6 Jan 2021 15:21:42 +0100 Subject: [PATCH] Fixup volume creation functionality --- CHANGELOG.md | 3 +- README.md | 11 ++++-- integration/molecule/default/molecule.yml | 5 ++- molecule_hetznercloud/playbooks/create.yml | 35 ++++++++++--------- molecule_hetznercloud/playbooks/destroy.yml | 7 ++-- .../unit/model/v2/test_platforms_section.py | 4 +-- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25bc4dd..8db02ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,8 @@ new major version of Molecule. If you use the `volumes:` key in your `molecule.yml` then this change will break your configuration. Please see the section on "Volume Handling" in the README.md on how to upgrade successfully. You will now need to install Ansible yourself as Molecule does not do it for -you. +you. If there are any other breaking changes, please report them on the issue +tracker so that we can mention them here. - Support Python 3.9. - Support Molecule 3.2.1 diff --git a/README.md b/README.md index d4d8030..92ce195 100644 --- a/README.md +++ b/README.md @@ -82,11 +82,16 @@ after use. ```yaml volumes: - name: "molecule-hetznercloud-volume-${INSTANCE_UUID}" - size: 11 - create: true + - name: "molecule-hetznercloud-volume-1-${INSTANCE_UUID}" + - name: "molecule-hetznercloud-volume-2-${INSTANCE_UUID}" ``` +Supported keys are: + +- **name** (required): name of volume +- **size** (optional, default: `10GB`): size of volume +- **location** (optional, default: `omitted`): path for volume + ## Only use `molecule.yml` for configuration It is being worked on that it is possible to remove all the files except the diff --git a/integration/molecule/default/molecule.yml b/integration/molecule/default/molecule.yml index 4754c90..18234ad 100644 --- a/integration/molecule/default/molecule.yml +++ b/integration/molecule/default/molecule.yml @@ -8,9 +8,8 @@ platforms: server_type: cx11 image: debian-10 volumes: - name: "molecule-hetznercloud-volume-${INSTANCE_UUID}" - size: 11 - create: true + - name: "molecule-hetznercloud-volume-1-${INSTANCE_UUID}" + # - name: "molecule-hetznercloud-volume-2-${INSTANCE_UUID}" provisioner: name: ansible verifier: diff --git a/molecule_hetznercloud/playbooks/create.yml b/molecule_hetznercloud/playbooks/create.yml index 23ee263..b5fa86e 100644 --- a/molecule_hetznercloud/playbooks/create.yml +++ b/molecule_hetznercloud/playbooks/create.yml @@ -53,16 +53,13 @@ - name: Create volume(s) hcloud_volume: - name: "{{ item.volumes.name | default(item.name) }}" - server: "{{ item.name }}" - location: "{{ item.volumes.location | default(omit) }}" - size: "{{ item.volumes.size | default(10) }}" + name: "{{ item.1.name | default(item.0.name) }}" + server: "{{ item.0.name }}" + location: "{{ item.1.location | default(omit) }}" + size: "{{ item.1.size | default(10) }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: "present" - with_items: "{{ molecule_yml.platforms }}" - when: - - item.volumes is defined - - item.volumes.create | default(False) | bool + loop: "{{ molecule_yml.platforms|subelements('volumes', skip_missing=True)}}" register: volumes async: 7200 poll: 0 @@ -73,19 +70,23 @@ register: hetzner_volumes until: hetzner_volumes.finished retries: 300 - when: volumes.changed + when: + - volumes is defined + - volumes.changed with_items: "{{ volumes.results }}" - name: Populate instance config dict set_fact: - instance_conf_dict: { - 'instance': "{{ item.hcloud_server.name }}", - 'ssh_key_name': "{{ ssh_key_name }}", - 'address': "{{ item.hcloud_server.ipv4_address }}", - 'user': "{{ ssh_user }}", - 'port': "{{ ssh_port }}", - 'identity_file': "{{ ssh_path }}", - 'volumes': "{{ item.item.item.volumes | default({}) }}", } + instance_conf_dict: + { + "instance": "{{ item.hcloud_server.name }}", + "ssh_key_name": "{{ ssh_key_name }}", + "address": "{{ item.hcloud_server.ipv4_address }}", + "user": "{{ ssh_user }}", + "port": "{{ ssh_port }}", + "identity_file": "{{ ssh_path }}", + "volumes": "{{ item.item.item.volumes | default({}) }}", + } with_items: "{{ hetzner_jobs.results }}" register: instance_config_dict when: server.changed | bool diff --git a/molecule_hetznercloud/playbooks/destroy.yml b/molecule_hetznercloud/playbooks/destroy.yml index 16eb0e5..6d52679 100644 --- a/molecule_hetznercloud/playbooks/destroy.yml +++ b/molecule_hetznercloud/playbooks/destroy.yml @@ -41,12 +41,9 @@ name: "{{ item.volumes.name | default(item.instance) }}" server: "{{ item.instance }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" - state: "absent" + state: absent register: volumes with_items: "{{ instance_conf }}" - when: - - item.volumes is defined - - item.volumes.create | default(False) | bool async: 7200 poll: 0 @@ -66,7 +63,7 @@ state: absent when: - not skip_instances - - instance_conf | length # must contain at least one instance + - instance_conf | length # must contain at least one instance - name: Populate instance config set_fact: diff --git a/molecule_hetznercloud/test/unit/model/v2/test_platforms_section.py b/molecule_hetznercloud/test/unit/model/v2/test_platforms_section.py index b3f6df0..e610248 100644 --- a/molecule_hetznercloud/test/unit/model/v2/test_platforms_section.py +++ b/molecule_hetznercloud/test/unit/model/v2/test_platforms_section.py @@ -10,7 +10,7 @@ def _model_platform_hetznercloud_section_data(): { "name": "instance", "server_type": "", - "volumes": {}, + "volumes": [], "image": "", "location": "", "datacenter": "", @@ -57,7 +57,7 @@ def test_platforms_hetznercloud_has_errors(_config): { "name": ["must be of string type"], "server_type": ["must be of string type"], - "volumes": ["must be of dict type"], + "volumes": ["must be of list type"], "image": ["must be of string type"], "location": ["must be of string type"], "datacenter": ["must be of string type"],