Public/Get-DuneComputeNode.ps1

<#
.SYNOPSIS
Retrieve Dune compute node objects.

.DESCRIPTION
Gets one or more Dune compute nodes. Supports filtering by hostname, id, collection, deployment, resource group, resource, resource provider, external id, FQDN, name, object type and state. Returns `DuneComputeNode` objects by default; use `-Raw` to get raw API output.

.PARAMETER Hostname
Filter compute nodes by hostname (supports wildcards). Position 0 in the default parameter set.

.PARAMETER Id
The GUID of a compute node. Use the `Id` parameter set to return a single compute node.

.PARAMETER Collection
A `DuneCollection` object; returns compute nodes in the supplied collection (pipeline input supported).

.PARAMETER Deployment
A `DuneDeployment` object; returns compute nodes for the supplied deployment (pipeline input supported).

.PARAMETER ResourceGroup
A `DuneResourceGroup` object; returns compute nodes in the supplied resource group (pipeline input supported).

.PARAMETER Resource
A `DuneResource` object; returns the compute node for the supplied resource (pipeline input supported).

.PARAMETER ResourceProvider
A `DuneResourceProvider` object; returns compute nodes for the supplied resource provider (pipeline input supported).

.PARAMETER ExtId
Filter by external identifier.

.PARAMETER Fqdn
Filter by fully-qualified domain name (supports wildcards).

.PARAMETER Name
Filter results by name (supports wildcards).

.PARAMETER ObjectType
Filter by object type; valid values `VirtualMachine` or `PhysicalMachine`.

.PARAMETER State
Filter by operational state.

.PARAMETER Raw
If set, returns raw API objects instead of `DuneComputeNode` objects.

.PARAMETER IncludeDeleted
Include deleted compute nodes in results.

.EXAMPLE
PS> Get-DuneComputeNode
Returns all compute nodes.

.EXAMPLE
PS> Get-DuneComputeNode -Id 3d8f6b5a-...
Returns the compute node with the specified `Id`.

.EXAMPLE
PS> Get-DuneCollection -Name "web-tier" | Get-DuneComputeNode
Pipeline example using the `Collection` parameter set.

.EXAMPLE
PS> Get-DuneComputeNode -Hostname "web*"
Find compute nodes with hostnames matching `web*`.
#>

function Get-DuneComputeNode {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(Position = 0)]
        [string]$Hostname,

        [Parameter(ParameterSetName = "Id")]
        [guid]$Id,

        [Parameter(ParameterSetName = "Collection", ValueFromPipeline)]
        [DuneCollection]$Collection,

        [Parameter(ParameterSetName = "Deployment", ValueFromPipeline)]
        [DuneDeployment]$Deployment,

        [Parameter(ParameterSetName = "ResourceGroup", ValueFromPipeline)]
        [DuneResourceGroup]$ResourceGroup,

        [Parameter(ParameterSetName = "Resource", ValueFromPipeline)]
        [DuneResource]$Resource,

        [Parameter(ParameterSetName = "ResourceProvider", ValueFromPipeline)]
        [DuneResourceProvider]$ResourceProvider,

        [Parameter(ParameterSetName = "ExtId")]
        [string]$ExtId,

        [Parameter()]
        [string]$Fqdn,

        [Parameter()]
        [string]$Name,

        [Parameter()]
        [ValidateSet('VirtualMachine', 'PhysicalMachine')]
        [string]$ObjectType,

        [Parameter()]
        [OperationalStates]$State,

        [Parameter()]
        [switch]$Raw = $False,

        [Parameter()]
        [switch]$IncludeDeleted
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $ProcessedUrls = @()
        $BaseUri = 'computenodes'
        $Method = "GET"
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { $BaseUri, $Id -join '/' }
            'Collection' { '{0}?CollectionId={1}' -f $BaseUri, $Collection.Id }
            'Deployment' { $BaseUri, "DeploymentId=$($Deployment.Id)" -join '?' }
            'ResourceGroup' { $BaseUri, "ParentId=$($ResourceGroup.Id)" -join '?' }
            'ResourceProvider' { $BaseUri, "ResourceProviderId=$($ResourceProvider.Id)" -join '?' }
            'ExtId' { '{0}?ExtIdIEquals={1}' -f $BaseUri, $ExtId }
            'Resource' {
                if ($Resource.ObjectType -in 'VirtualMachine', 'PhysicalMachine') {
                    $BaseUri, $Resource.Id -join '/'
                }
                else {
                    Write-Warning "Wrong ObjectType $($Resource.ObjectType) for $($Resource.Name) (id: $($Resource.Id))"
                    return
                }
            }
            Default { $BaseUri }
        }
        If ($Hostname) {
            $Uri = $Uri | Add-UriQueryParam "HostnameILike=$Hostname" -ConvertWildcards
        }
        if ($Fqdn) {
            $Uri = $Uri | Add-UriQueryParam "FqdnILike=$Fqdn" -ConvertWildcards
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($PSBoundParameters.ContainsKey('State')) {
            $Uri = $Uri | Add-UriQueryParam "State=$($State.ToString())"
        }
        if ($ObjectType) {
            $Uri = $Uri | Add-UriQueryParam "ObjectType=$ObjectType"
        }
        if ($IncludeDeleted) {
            $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1"
        }
        $Uri = $Uri | Add-UriQueryParam "IncludeReferencedObjects=1"

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                $ResultItems = Invoke-DuneApiRequest -Uri $Uri -Method $Method -ExtractItems
                $ProcessedUrls += $Uri
                $ReturnObjects += $ResultItems | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneComputeNode -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"

        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object Hostname
    }
}