This commit is contained in:
2025-07-21 13:31:14 +03:00
parent 4113719334
commit 63073bffd3
201 changed files with 2228 additions and 80456 deletions

View File

@@ -4,7 +4,7 @@ DOCUMENTATION = r'''
---
module: decort_snapshot
description: See L(Module Documentation,https://repository.basistech.ru/BASIS/decort-ansible/wiki/Home).
description: See L(Module Documentation,https://repository.basistech.ru/BASIS/decort-ansible/wiki/Home). # noqa: E501
'''
import time
@@ -17,33 +17,24 @@ class DecortSnapshot(DecortController):
super().__init__(AnsibleModule(**self.amodule_init_args))
self.check_amodule_args()
self.vm_id: int
self.vm_facts: dict
self.aparams_label = self.aparams['label']
self.aparams_vm_id = self.aparams['vm_id']
vm_id, vm_facts, _ = self._compute_get_by_id(
self.vm_id, self.vm_facts, _ = self._compute_get_by_id(
comp_id=self.aparams_vm_id,
)
if not vm_id:
if not self.vm_id:
self.message(f'VM {self.aparams_vm_id} not found')
self.exit(fail=True)
self.vm_name = vm_facts['name']
self.vm_snapshots = vm_facts['snapSets']
self.vm_name = self.vm_facts['name']
self.vm_snapshots = self.vm_facts['snapSets']
self.vm_snapshot_labels = [
snapshot['label'] for snapshot in self.vm_snapshots
]
if (
self.aparams_label is not None
and self.aparams_label not in self.vm_snapshot_labels
and self.aparams['state'] is None
):
self.message(
f'Snapshot {self.aparams_label} '
f'not found for VM {self.aparams_vm_id}'
)
self.exit(fail=True)
self.new_snapshot_label = None
if self.aparams['state'] == 'present':
if self.aparams_label is None:
@@ -53,6 +44,17 @@ class DecortSnapshot(DecortController):
elif self.aparams_label not in self.vm_snapshot_labels:
self.new_snapshot_label = self.aparams_label
if (
self.new_snapshot_label is None
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)
@property
def amodule_init_args(self) -> dict:
return self.pack_amodule_init_args(
@@ -65,6 +67,7 @@ class DecortSnapshot(DecortController):
choices=(
'absent',
'present',
'merge_aborted',
),
),
usage=dict(
@@ -101,6 +104,7 @@ class DecortSnapshot(DecortController):
def run(self):
self.get_info(first_run=True)
self.check_amodule_args_for_change()
self.change()
self.exit()
@@ -126,6 +130,8 @@ class DecortSnapshot(DecortController):
case 'absent':
if self.aparams_label in self.vm_snapshot_labels:
self.delete()
case 'merge_aborted':
self.abort_merge()
def create(self):
self.snapshot_create(
@@ -141,6 +147,13 @@ class DecortSnapshot(DecortController):
)
self.facts = {}
def abort_merge(self):
self.snapshot_abort_merge(
vm_id=self.aparams_vm_id,
label=self.aparams_label,
)
self.get_info()
def get_snapshot_usage(self) -> int:
label = self.new_snapshot_label or self.aparams_label
common_snapshots_usage_info, _ = self.snapshot_usage(
@@ -148,6 +161,24 @@ class DecortSnapshot(DecortController):
label=label,
)
return common_snapshots_usage_info['stored']
def check_amodule_args_for_change(self):
check_errors = False
if (
self.aparams['state'] == 'merge_aborted'
and self.vm_facts['techStatus'] != 'MERGE'
):
check_errors = True
self.message(
f'Check for parameter "state" failed: '
'Merge can be aborted only for VM in "MERGE" tech status.'
)
if check_errors:
self.exit(fail=True)
def main():
DecortSnapshot().run()