fix(snapshot): _find_vm matching tolerant (court + FQDN) + log samples pour debug si VM non trouvee

This commit is contained in:
Pierre & Lumière 2026-05-04 16:21:45 +02:00
parent 9996757e4b
commit f1baae9c8e

View File

@ -40,17 +40,47 @@ def _connect_vcenter(endpoint, user, password):
def _find_vm(si, vm_name):
"""Cherche une VM par nom dans le vCenter. Retourne l'objet VM ou None."""
"""Cherche une VM par nom dans le vCenter.
Matching tolérant :
1. nom exact (insensible à la casse)
2. partie courte avant le 1er '.' (ex: 'vrexpbtex1' == 'vrexpbtex1.sanef.groupe')
Logue un échantillon de VMs vues pour faciliter le debug en cas d'échec.
"""
target_full = (vm_name or "").lower().strip()
short = target_full.split(".")[0]
if not short:
return None
content = si.RetrieveContent()
container = content.viewManager.CreateContainerView(
content.rootFolder, [vim.VirtualMachine], True)
total = 0
exact_match = None
short_match = None
samples = []
try:
for vm in container.view:
if vm.name.lower() == vm_name.lower():
return vm
total += 1
if not vm.name:
continue
n = vm.name.lower().strip()
if exact_match is None and n == target_full:
exact_match = vm
elif short_match is None and n.split(".")[0] == short:
short_match = vm
if len(samples) < 5 and n.startswith(short[:3]):
samples.append(vm.name)
finally:
container.Destroy()
return None
found = exact_match or short_match
if found is None:
log.info(f"_find_vm({vm_name}): no match parmi {total} VMs ; "
f"samples startswith '{short[:3]}': {samples}")
else:
kind = "exact" if exact_match else "short"
log.info(f"_find_vm({vm_name}): {kind} match → {found.name}")
return found
def _take_snapshot(vm, snap_name, description=""):