Public/diagnostics.ps1

# Diagnostics on system VMs: run network-utility commands and collect diagnostics
# files from the console proxy, secondary storage, and virtual router VMs.

function Invoke-CSDiagnostics {
    <#
    .SYNOPSIS
        Runs a network-utility command on a system VM.

    .DESCRIPTION
        Wraps runDiagnostics, executing ping, traceroute, or arping from a system VM
        (console proxy, secondary storage, or virtual router) against a target
        address. This is an asynchronous job; use -Wait to block and return the
        command output. Accepts system VM objects on the pipeline.

    .PARAMETER TargetId
        The system VM to run the command from. Binds from a piped system VM's id.

    .PARAMETER Type
        The command to run: ping, traceroute, or arping

    .PARAMETER IpAddress
        The address to run the command against

    .PARAMETER Params
        Extra command-line parameters to pass to the utility

    .PARAMETER Wait
        Wait for the async job to finish and return the command output

    .EXAMPLE
        Invoke-CSDiagnostics -TargetId $systemVmId -Type ping -IpAddress '8.8.8.8' -Wait
        Pings an address from a system VM and returns the output.

    .EXAMPLE
        Get-CSSystemVM -Name 'v-1-VM' | Invoke-CSDiagnostics -Type traceroute -IpAddress '10.0.0.1' -Wait
        Runs a traceroute from a system VM piped in by object.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Id')]
        [string]$TargetId,

        [Parameter(Mandatory = $true)]
        [ValidateSet('ping', 'traceroute', 'arping')]
        [string]$Type,

        [Parameter(Mandatory = $true)]
        [string]$IpAddress,

        [string]$Params,

        [switch]$Wait
    )

    process {
        $apiParams = @{ targetid = $TargetId; type = $Type; ipaddress = $IpAddress }
        if ($PSBoundParameters.ContainsKey('Params')) { $apiParams['params'] = $Params }
        if ($PSCmdlet.ShouldProcess("system VM $TargetId", "Run $Type against $IpAddress")) {
            Invoke-CSAsyncApiRequest -Command 'runDiagnostics' -Parameters $apiParams -Wait:$Wait
        }
    }
}

function Get-CSDiagnosticsData {
    <#
    .SYNOPSIS
        Collects diagnostics files from a system VM.

    .DESCRIPTION
        Wraps getDiagnosticsData, gathering log and configuration files from a system
        VM into an archive and returning a URL to download it. With no -Files, the
        provider's default set is collected. This is an asynchronous job; use -Wait to
        block and return the download URL. Accepts system VM objects on the pipeline.

    .PARAMETER TargetId
        The system VM to collect from. Binds from a piped system VM's id.

    .PARAMETER Files
        Specific file paths to collect instead of the default set

    .PARAMETER Wait
        Wait for the async job to finish and return the download URL

    .EXAMPLE
        Get-CSDiagnosticsData -TargetId $systemVmId -Wait
        Collects the default diagnostics set and returns a download URL.

    .EXAMPLE
        Get-CSSystemVM -Name 'v-1-VM' | Get-CSDiagnosticsData -Files '/var/log/cloud.log','/etc/cloudstack-release' -Wait
        Collects specific files from a system VM piped in by object.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Id')]
        [string]$TargetId,

        [string[]]$Files,

        [switch]$Wait
    )

    process {
        $apiParams = @{ targetid = $TargetId }
        if ($PSBoundParameters.ContainsKey('Files')) { $apiParams['files'] = $Files -join ',' }
        Invoke-CSAsyncApiRequest -Command 'getDiagnosticsData' -Parameters $apiParams -Wait:$Wait
    }
}