Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions plugins/modules/vm_snapshot_attach_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
description: VM unique identifier.
type: str
sample: e18ec6af-9dd2-41dc-89af-8ce637171524
vm_rebooted:
description:
- Info if reboot of the VM was performed.
returned: success
type: bool
sample: true
"""


Expand All @@ -118,7 +124,7 @@

def attach_disk(
module: AnsibleModule, rest_client: RestClient
) -> Tuple[bool, Optional[Dict[Any, Any]], TypedDiff]:
) -> Tuple[bool, Optional[Dict[Any, Any]], TypedDiff, bool]:
# =============== SAVE PARAMS VALUES ===============
# destination
vm_name = module.params["vm_name"]
Expand Down Expand Up @@ -162,11 +168,12 @@ def attach_disk(
rest_client=rest_client,
)
if before_disk is not None:
# changed, after, diff
# changed, after, diff, vm_rebooted
return (
False,
before_disk,
dict(before=before_disk, after=None),
False, # destination vm wasn't rebooted up to this point
)

# First power off the destination VM
Expand Down Expand Up @@ -218,12 +225,13 @@ def attach_disk(
dict(
before=None, after=created_disk
), # before, we ofcourse, didn't have that new block device, and after we should have it
vm_object.reboot,
)


def run(
module: AnsibleModule, rest_client: RestClient
) -> Tuple[bool, Optional[Dict[Any, Any]], TypedDiff]:
) -> Tuple[bool, Optional[Dict[Any, Any]], TypedDiff, bool]:
return attach_disk(module, rest_client)


Expand Down Expand Up @@ -267,8 +275,8 @@ def main() -> None:
try:
client = Client.get_client(module.params["cluster_instance"])
rest_client = RestClient(client)
changed, record, diff = run(module, rest_client)
module.exit_json(changed=changed, record=record, diff=diff)
changed, record, diff, reboot = run(module, rest_client)
module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot)
except errors.ScaleComputingError as e:
module.fail_json(msg=str(e))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

force_reboot: true # allow forced vm shutdown

# disk size gets rounded by HC3:
# 0.1 GB to 107374182 B
# 0.2 GB to 214958080 B
size_0_1_GB: 107374182
size_0_2_GB: 214958080

block:
- name: Create a stopped VM "{{ vm_2 }}"
scale_computing.hypercore.vm:
Expand Down Expand Up @@ -83,15 +89,26 @@
- ansible.builtin.assert:
that:
- result is changed
- result.vm_rebooted is false
- include_tasks: helper_check_vm_state.yml
vars:
vm_name: "{{ vm_2 }}"
expected_state: stopped
- ansible.builtin.assert: &test-virtio
that:
- result.record.type == "virtio_disk"
- "result.record.keys() | sort == ['disk_slot', 'size', 'type', 'uuid', 'vm_uuid']"
- result.record.disk_slot == slot_a
- result.record.size == size_0_2_GB
- result.record.type == "virtio_disk"
- result.record.uuid | length == 36
- result.record.vm_uuid == vm_2_info.records[0].uuid
# Test returned record is consistent with data from vm_info
- vm_info.records[0].disks | length == 3
- vm_info.records[0].disks[2].disk_slot == slot_a
- vm_info.records[0].disks[2].size == size_0_2_GB
- vm_info.records[0].disks[2].type == "virtio_disk"
- vm_info.records[0].disks[2].uuid == result.record.uuid
- vm_info.records[0].disks[2].vm_uuid == vm_2_info.records[0].uuid

- name: Start VM "{{ vm_2 }}"
scale_computing.hypercore.vm_params:
Expand All @@ -117,6 +134,7 @@
- ansible.builtin.assert:
that:
- result is not changed
- result.vm_rebooted is false
- include_tasks: helper_check_vm_state.yml
vars:
vm_name: "{{ vm_2 }}"
Expand Down Expand Up @@ -146,15 +164,30 @@
- ansible.builtin.assert:
that:
- result is changed
- result.vm_rebooted is true
- include_tasks: helper_check_vm_state.yml
vars:
vm_name: "{{ vm_2 }}"
expected_state: started
- ansible.builtin.assert: &test-not-virtio
that:
- result.record.type == "ide_disk"
- "result.record.keys() | sort == ['disk_slot', 'size', 'type', 'uuid', 'vm_uuid']"
- result.record.disk_slot == slot_b
- result.record.size == size_0_2_GB
- result.record.type == "ide_disk"
- result.record.uuid | length == 36
- result.record.vm_uuid == vm_2_info.records[0].uuid
- vm_info.records[0].disks | length == 4
- vm_info.records[0].disks[2].disk_slot == slot_a
- vm_info.records[0].disks[2].size == size_0_2_GB
- vm_info.records[0].disks[2].type == "virtio_disk"
# - vm_info.records[0].disks[2].uuid == result.record.uuid
- vm_info.records[0].disks[2].vm_uuid == vm_2_info.records[0].uuid
- vm_info.records[0].disks[3].disk_slot == slot_b
- vm_info.records[0].disks[3].size == size_0_2_GB
- vm_info.records[0].disks[3].type == "ide_disk"
- vm_info.records[0].disks[3].uuid == result.record.uuid
- vm_info.records[0].disks[3].vm_uuid == vm_2_info.records[0].uuid

- name: >-
IDEMPOTENCE - Attach "snap-0" from VM "{{ vm_1 }}"
Expand All @@ -174,6 +207,7 @@
- ansible.builtin.assert:
that:
- result is not changed
- result.vm_rebooted is false
- include_tasks: helper_check_vm_state.yml
vars:
vm_name: "{{ vm_2 }}"
Expand Down Expand Up @@ -203,15 +237,26 @@
- ansible.builtin.assert:
that:
- result is changed
- result.vm_rebooted is true
- include_tasks: helper_check_vm_state.yml
vars:
vm_name: "{{ vm_1 }}"
expected_state: started
- ansible.builtin.assert: &test-virtio-2
that:
- result.record.type == "virtio_disk"
- "result.record.keys() | sort == ['disk_slot', 'size', 'type', 'uuid', 'vm_uuid']"
- result.record.disk_slot == slot_vm_1_virtio
- result.record.size == size_0_2_GB
- result.record.type == "virtio_disk"
- result.record.uuid | length == 36
- result.record.vm_uuid == vm_1_info.records[0].uuid
- vm_info.records[0].disks | length == 3
- vm_info.records[0].disks[2].disk_slot == slot_vm_1_virtio
- vm_info.records[0].disks[2].size == size_0_2_GB
- vm_info.records[0].disks[2].type == "virtio_disk"
- vm_info.records[0].disks[2].uuid == result.record.uuid
- vm_info.records[0].disks[2].vm_uuid == vm_1_info.records[0].uuid


- name: >-
IDEMPOTENCE - Attach "snap-0" from VM "{{ vm_1 }}"
Expand All @@ -235,6 +280,7 @@
- ansible.builtin.assert:
that:
- result is not changed
- result.vm_rebooted is false
- ansible.builtin.assert: *test-virtio-2

# ---------- Cleanup ------------
Expand Down
17 changes: 10 additions & 7 deletions tests/unit/plugins/modules/test_vm_snapshot_attach_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ def setup_method(self):
@pytest.mark.parametrize(
("destination_vm_disk_info", "expected_return"),
[
(None, (True, BLOCK_DEVICE, dict(before=None, after=BLOCK_DEVICE))),
(None, (True, BLOCK_DEVICE, dict(before=None, after=BLOCK_DEVICE), True)),
(
BLOCK_DEVICE,
(False, BLOCK_DEVICE, dict(before=BLOCK_DEVICE, after=None)),
(False, BLOCK_DEVICE, dict(before=BLOCK_DEVICE, after=None), False),
),
],
)
Expand Down Expand Up @@ -222,7 +222,9 @@ def test_attach_disk_is_change(
check_mode=False,
)

changed, record, diff = vm_snapshot_attach_disk.attach_disk(module, rest_client)
changed, record, diff, vm_rebooted = vm_snapshot_attach_disk.attach_disk(
module, rest_client
)

if destination_vm_disk_info is None:
rest_client.create_record.assert_any_call(**called_with_dict)
Expand All @@ -235,15 +237,16 @@ def test_attach_disk_is_change(
assert changed == expected_return[0]
assert record == expected_return[1]
assert diff == expected_return[2]
assert vm_rebooted == expected_return[3]


class TestMain:
def test_fail(self, run_main):
success, result = run_main(vm_snapshot_attach_disk)
def test_fail(self, run_main_with_reboot):
success, result = run_main_with_reboot(vm_snapshot_attach_disk)

assert success is False
assert "missing required arguments" in result["msg"]

def test_params(self, run_main):
success, result = run_main(vm_snapshot_attach_disk, PARAMS)
def test_params(self, run_main_with_reboot):
success, result = run_main_with_reboot(vm_snapshot_attach_disk, PARAMS)
assert success is True