Public/New-Vm.ps1

<#
    .DESCRIPTION
    Wrapper for Nutanix API version 0.3.
 
    .NOTES
    Author: Timothy Rasiah
 
    .EXAMPLE
    New-NutanixV3Vm -Name VM-01
 
    .EXAMPLE
    New-NutanixV3Vm -Name VM-02 -Description "VM 2" -NumSockets 1 -NumVcpusPerSocket 2 -MemorySizeMib (4GB/1MB) -BootType UEFI -ClusterUuid 00000000-0000-0000-0000-000000000000
#>


function New-Vm {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [String]$Name,
        [String]$Description,
        [Int]$NumSockets,
        [Int]$NumVcpusPerSocket,
        [Int]$NumThreadsPerCore,
        [Int]$MemorySizeMib,
        [ValidateSet("PC", "PSERIES", "Q35")]
        [String]$MachineType,
        [ValidateSet("UEFI", "LEGACY", "SECURE_BOOT")]
        [String]$BootType,
        [String]$AvailabilityZoneUuid,
        [String]$ClusterUuid
    )

    $data = @{
        "spec" = @{
            "name" = $Name
            "resources" = @{}
        }
        "metadata" = @{
            "kind" = "vm"
        }
    }

    if ($Description) {
        $data.spec["description"] = $Description
    }

    if ($NumSockets) {
        $data.spec.resources["num_sockets"] = $NumSockets
    }

    if ($NumVcpusPerSocket) {
        $data.spec.resources["num_vcpus_per_socket"] = $NumVcpusPerSocket
    }

    if ($NumThreadsPerCore) {
        $data.spec.resources["num_threads_per_core"] = $NumThreadsPerCore
    }

    if ($MemorySizeMib) {
        $data.spec.resources["memory_size_mib"] = $MemorySizeMib
    }

    if ($MachineType) {
        $data.spec.resources["machine_type"] = $MachineType
    }

    if ($BootType) {
        $data.spec.resources["boot_config"] = @{
            "boot_type" = $BootType
        }
    }

    if ($AvailabilityZoneUuid) {
        $data.spec["availability_zone_reference"] = @{
            "kind" = "availability_zone"
            "uuid" = $AvailabilityZoneUuid
        }
    }
    
    if ($ClusterUuid) {
        $data.spec["cluster_reference"] = @{
            "kind" = "cluster"
            "uuid" = $ClusterUuid
        }
    }

    $response = Send-Request -method "POST" -endpoint "/vms" -data $data
    return $response
}