from netlab.client import Client
from netlab.enums import VirtualMachineRole

client = Client(system="stan")


datacenters = client.vm_datacenter_list()

for datacenter in datacenters:
    print(f"""
    vdc_id:   {datacenter["vdc_id"]}
    vdc_name: {datacenter["vdc_name"]}

    """)

vdc_id = int(input("Enter datacenter vdc_id: ").strip())

for host in client.vm_host_list(vdc_id=vdc_id):
    print(f"""
    vh_id:  {host["vh_id"]}
    vh_name:  {host["vh_name"]}

    """)

vh_id = int(input("Enter host vh_id: ").strip())

children_to_remove = []
masters_to_remove = []

for vm in client.vm_inventory_list(vdc_id=vdc_id):
    if vm["vh_id"] == vh_id:
        if vm["vm_role"] is not VirtualMachineRole.MASTER:
            children_to_remove.append(vm["vm_id"])
        if vm["vm_role"] is VirtualMachineRole.MASTER:
            masters_to_remove.append(vm["vm_id"])

print(f"""
preparing to delete')
  total:    {len(masters_to_remove) + len(children_to_remove)}
  masters:  {len(masters_to_remove)}
  children: {len(children_to_remove)}
""")

for vm_id in children_to_remove:
    print(f"child:  {vm_id}")

for vm_id in masters_to_remove:
    print(f"master: {vm_id}")

print("\n\n")

confirmed = input("Continue?  [N/y]").lower() == "y"

if confirmed:
    for vm_id in children_to_remove + masters_to_remove:
        print(f"Deleting {vm_id}")
        client.vm_inventory_remove_local(vm_id=vm["vm_id"])
