"""Pulumi cookbook using official pulumi-vsphere against the simulator."""

from __future__ import annotations

import os

import pulumi
import pulumi_vsphere as vsphere

config = pulumi.Config()
user = config.get("user") or os.environ.get("VSPHERE_USER", "administrator@vsphere.local")
password = config.get_secret("password") or os.environ.get("VSPHERE_PASSWORD", "VMware1!")
server = config.get("server") or os.environ.get("VSPHERE_SERVER", "localhost")
datacenter_name = config.get("datacenter") or "Datacenter"
datastore_name = config.get("datastore") or "datastore1"
cluster_name = config.get("cluster") or "Cluster"
network_name = config.get("network") or "VM Network"
vm_name = config.get("vm_name") or "pulumi-lab-01"

provider = vsphere.Provider(
    "vsphere",
    user=user,
    password=password,
    vsphere_server=server,
    allow_unverified_ssl=True,
)
invoke_opts = pulumi.InvokeOptions(provider=provider)
res_opts = pulumi.ResourceOptions(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.VirtualMachine(
    "cookbook-vm",
    name=vm_name,
    resource_pool_id=cluster.resource_pool_id,
    datastore_id=ds.id,
    num_cpus=1,
    memory=1024,
    guest_id="otherGuest64",
    wait_for_guest_net_timeout=0,
    wait_for_guest_ip_timeout=0,
    network_interfaces=[
        vsphere.VirtualMachineNetworkInterfaceArgs(network_id=network.id),
    ],
    disks=[
        vsphere.VirtualMachineDiskArgs(label="disk0", size=16),
    ],
    opts=res_opts,
)

pulumi.export("datacenter_id", dc.id)
pulumi.export("lab_vm_id", vm.id)
pulumi.export("lab_vm_name", vm.name)
pulumi.export("resource_pool_id", cluster.resource_pool_id)
