"""Inventory lookups via official pulumi-vsphere (SOAP/VIM)."""

from __future__ import annotations

import os

import pulumi
import pulumi_vsphere as vsphere

user = os.environ.get("VSPHERE_USER", "administrator@vsphere.local")
password = os.environ.get("VSPHERE_PASSWORD", "VMware1!")
server = os.environ.get("VSPHERE_SERVER", "api-gateway")
datacenter_name = os.environ.get("VSPHERE_DATACENTER", "Datacenter")
datastore_name = os.environ.get("VSPHERE_DATASTORE", "datastore1")
cluster_name = os.environ.get("VSPHERE_CLUSTER", "Cluster")
network_name = os.environ.get("VSPHERE_NETWORK", "VM Network")
vm_name = os.environ.get("VSPHERE_VM_NAME", "web-01")
host_name = os.environ.get("VSPHERE_HOST", "esxi01.lab.local")
folder_path = os.environ.get("VSPHERE_FOLDER_PATH", f"/{datacenter_name}/vm/production")
pool_name = os.environ.get("VSPHERE_RESOURCE_POOL", "Resources")

provider = vsphere.Provider(
    "vsphere",
    user=user,
    password=password,
    vsphere_server=server,
    allow_unverified_ssl=True,
)

invoke_opts = pulumi.InvokeOptions(provider=provider)

dc = vsphere.get_datacenter_output(name=datacenter_name, opts=invoke_opts)
ds = vsphere.get_datastore_output(
    name=datastore_name,
    datacenter_id=dc.id,
    opts=invoke_opts,
)
cluster = vsphere.get_compute_cluster_output(
    name=cluster_name,
    datacenter_id=dc.id,
    opts=invoke_opts,
)
network = vsphere.get_network_output(
    name=network_name,
    datacenter_id=dc.id,
    opts=invoke_opts,
)
vm = vsphere.get_virtual_machine_output(
    name=vm_name,
    datacenter_id=dc.id,
    opts=invoke_opts,
)
host = vsphere.get_host_output(
    name=host_name,
    datacenter_id=dc.id,
    opts=invoke_opts,
)
folder = vsphere.get_folder_output(path=folder_path, opts=invoke_opts)
# Prefer cluster-scoped path so a polluted lab with multiple "Resources" pools
# (e.g. leftover probe datacenters) still resolves uniquely.
pool = vsphere.get_resource_pool_output(
    name=f"{cluster_name}/{pool_name}",
    datacenter_id=dc.id,
    opts=invoke_opts,
)

pulumi.export("datacenter_id", dc.id)
pulumi.export("datacenter_name", dc.name)
pulumi.export("datastore_id", ds.id)
pulumi.export("datastore_name", ds.name)
pulumi.export("cluster_id", cluster.id)
pulumi.export("cluster_name", cluster.name)
pulumi.export("resource_pool_id", pool.id)
pulumi.export("resource_pool_name", pool.name)
pulumi.export("cluster_resource_pool_id", cluster.resource_pool_id)
pulumi.export("network_id", network.id)
pulumi.export("network_name", network.name)
pulumi.export("vm_id", vm.id)
pulumi.export("vm_name", vm.name)
pulumi.export("host_id", host.id)
pulumi.export("host_name", host.name)
pulumi.export("folder_id", folder.id)
pulumi.export("folder_path", folder.path)
