Compatibility/ZStack.PowerCLI.Compatibility.psm1

Set-StrictMode -Version Latest

$legacyNames = @('Connect-VIServer', 'Disconnect-VIServer', 'Get-VM', 'Start-VM', 'Get-HardDisk', 'Get-NetworkAdapter', 'Get-VMHost', 'Get-Cluster')
# An exact-name lookup can autoload a module even with -ListImported.
foreach ($command in (Get-Command -All -ListImported)) {
    if ($command.Name -in $legacyNames) {
        throw "CloudAutomation.CompatibilityConflict: '$($command.Name)' is already loaded. Use a separate PowerShell session."
    }
}
$nativeManifest = Join-Path (Split-Path -Parent $PSScriptRoot) 'ZStack.PowerCLI.psd1'
Import-Module -Name $nativeManifest -Scope Local -ErrorAction Stop

function Connect-VIServer {
    [CmdletBinding()]
    param([Parameter(Mandatory, Position = 0)] [string] $Server, [Parameter(Mandatory)] [pscredential] $Credential)
    & 'ZStack.PowerCLI\Connect-ZStackServer' @PSBoundParameters
}

function Disconnect-VIServer {
    [CmdletBinding()]
    param([Parameter(Mandatory, Position = 0, ValueFromPipeline)] [object[]] $Server)
    process { & 'ZStack.PowerCLI\Disconnect-ZStackServer' @PSBoundParameters }
}

function Get-VM {
    [CmdletBinding(DefaultParameterSetName = 'Name')]
    param(
        [Parameter(Position = 0, ParameterSetName = 'Name')] [ValidateNotNullOrEmpty()] [string[]] $Name,
        [Parameter(Mandatory, ParameterSetName = 'Id')] [string[]] $Id,
        [object] $Server
    )
    & 'ZStack.PowerCLI\Get-ZStackVM' @PSBoundParameters
}

function Get-HardDisk {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [object[]] $VM,
        [string[]] $Name,
        [string[]] $Id,
        [object] $Server
    )
    begin { $targets = [System.Collections.Generic.List[object]]::new() }
    process { $targets.AddRange($VM) }
    end {
        if ($targets.Count) {
            $PSBoundParameters['VM'] = $targets.ToArray()
            & 'ZStack.PowerCLI\Get-ZStackHardDisk' @PSBoundParameters
        }
    }
}

function Get-NetworkAdapter {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [object[]] $VM,
        [string[]] $Id,
        [object] $Server
    )
    begin { $targets = [System.Collections.Generic.List[object]]::new() }
    process { $targets.AddRange($VM) }
    end {
        if ($targets.Count) {
            $PSBoundParameters['VM'] = $targets.ToArray()
            & 'ZStack.PowerCLI\Get-ZStackNetworkAdapter' -IncludeNetworkName @PSBoundParameters
        }
    }
}

function Get-VMHost {
    [CmdletBinding()]
    param(
        [Parameter(Position = 0)] [string[]] $Name,
        [string[]] $Id,
        [object[]] $Location,
        [object] $Server
    )
    if ($PSBoundParameters.ContainsKey('Location')) {
        $PSBoundParameters['Cluster'] = $Location
        $null = $PSBoundParameters.Remove('Location')
    }
    & 'ZStack.PowerCLI\Get-ZStackVMHost' @PSBoundParameters
}

function Get-Cluster {
    [CmdletBinding()]
    param(
        [Parameter(Position = 0)] [string[]] $Name,
        [string[]] $Id,
        [object] $Server
    )
    & 'ZStack.PowerCLI\Get-ZStackCluster' @PSBoundParameters
}

function Start-VM {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [object[]] $VM,
        [object] $Server,
        [ValidateRange(1, 3600)] [int] $TimeoutSeconds = 300
    )
    process {
        $forward = @{}
        foreach ($key in $PSBoundParameters.Keys) { if ($key -ne 'VM') { $forward[$key] = $PSBoundParameters[$key] } }
        foreach ($item in $VM) {
            if ($item -is [string]) { & 'ZStack.PowerCLI\Start-ZStackVM' -Name $item @forward }
            else { & 'ZStack.PowerCLI\Start-ZStackVM' -VM $item @forward }
        }
    }
}

Export-ModuleMember -Function $legacyNames