This commit is contained in:
2026-09-18 12:19:28 +03:00
parent b2cb33e9a8
commit 1c0e5cfdb3
27 changed files with 1045 additions and 753 deletions

View File

@@ -10,6 +10,7 @@ description: See L(Module Documentation,https://repository.basistech.ru/BASIS/de
import time
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.decort_utils import DecortController
import dynamix_sdk.types as sdk_types
class DecortVMSnapshot(DecortController):
@@ -21,6 +22,7 @@ class DecortVMSnapshot(DecortController):
self.vm_facts: dict
self.aparams_label = self.aparams['label']
self.aparams_vm_id = self.aparams['vm_id']
self.vm_snapshot: sdk_types.SnapshotSetAPIResultNM | None = None
self.vm_id, self.vm_facts, _ = self._compute_get_by_id(
comp_id=self.aparams_vm_id,
@@ -50,11 +52,14 @@ class DecortVMSnapshot(DecortController):
and self.aparams_label is not None
and self.aparams_label not in self.vm_snapshot_labels
):
self.message(
f'Snapshot {self.aparams_label} '
f'not found for VM {self.aparams_vm_id}'
)
self.exit(fail=True)
if self.aparams['state'] == 'absent':
self.exit()
else:
self.message(
f'Snapshot {self.aparams_label} '
f'not found for VM {self.aparams_vm_id}'
)
self.exit(fail=True)
@property
def amodule_init_args(self) -> dict:
@@ -79,6 +84,9 @@ class DecortVMSnapshot(DecortController):
type='int',
required=True,
),
with_memory=dict(
type='bool',
),
),
supports_check_mode=True,
required_if=[
@@ -119,6 +127,7 @@ class DecortVMSnapshot(DecortController):
label = self.new_snapshot_label or self.aparams_label
for snapshot in self.vm_snapshots:
if snapshot.label == label:
self.vm_snapshot = snapshot
self.facts = snapshot.model_dump()
if self.aparams['usage']:
self.facts['stored'] = self.get_snapshot_usage()
@@ -137,9 +146,22 @@ class DecortVMSnapshot(DecortController):
self.abort_merge()
def create(self):
with_memory = self.aparams['with_memory']
if with_memory is None:
with_memory = False
self.message(
msg=self.MESSAGES.default_value_used(
param_name='with_memory',
default_value=with_memory
),
warning=True,
)
self.sdk_checkmode(self.api.cloudapi.compute.snapshot_create)(
vm_id=self.aparams_vm_id,
label=self.new_snapshot_label,
with_memory=with_memory,
)
self.get_info(update_vm_snapshots=True)
@@ -151,10 +173,16 @@ class DecortVMSnapshot(DecortController):
self.facts = {}
def abort_merge(self):
self.snapshot_abort_merge(
self.sdk_checkmode(
self.api.cloudapi.compute.abort_shared_snapshot_merge
)(
vm_id=self.aparams_vm_id,
label=self.aparams_label,
)
self.message(
f'Merge aborted for snapshot {self.aparams_label} of VM ID '
f'{self.aparams_vm_id}.'
)
self.get_info()
def get_snapshot_usage(self) -> int:
@@ -178,7 +206,33 @@ class DecortVMSnapshot(DecortController):
'Merge can be aborted only for VM in "MERGE" tech status.'
)
if (
self.aparams['with_memory'] is not None
and self.vm_snapshot
):
if self.aparams['with_memory'] != bool(
self.vm_snapshot.memory_dump_image_id
):
check_errors = True
self.message(
'Check for parameter "with_memory" failed: '
'Changing the "with_memory" parameter is not '
'allowed for an existing snapshot.'
)
if (
self.vm_facts['techStatus'] != 'STARTED'
and self.aparams['with_memory']
):
check_errors = True
self.message(
'Check for parameter "with_memory" failed: '
f'VM ID {self.aparams_vm_id} must be started '
'to create snapshot with memory dump.'
)
if check_errors:
self.facts = {}
self.exit(fail=True)