Public/Get-DuneDeployment.ps1

<#
.SYNOPSIS
Retrieve Dune deployments.

.DESCRIPTION
Gets one or more deployments. Supports filtering by name, display name, id, collection, deployment template, resource group, resource, job, environment and flags to include templates or deleted objects. Returns `DuneDeployment` objects by default; use `-Raw` for raw API responses.

.PARAMETER Name
Filter deployments by name (supports wildcards). Position 0 in the default parameter set.

.PARAMETER DisplayName
Filter by display name (supports wildcards).

.PARAMETER Id
The GUID of a deployment. Use the `Id` parameter set for a single deployment.

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

.PARAMETER DeploymentTemplate
A `DuneDeploymentTemplate` object; filter deployments by template (pipeline input supported).

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

.PARAMETER Resource
A `DuneResource` object; returns the deployment associated with the resource (pipeline input supported).

.PARAMETER Job
A `DuneJob` object; returns deployments referenced by a job (pipeline input supported).

.PARAMETER Environment
Filter by `Environments` enum.

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

.PARAMETER IncludeDeploymentTemplate
If set, ensures deployment template objects are included/attached to returned deployments.

.PARAMETER IncludeDeleted
Include deleted deployments in results.

.EXAMPLE
PS> Get-DuneDeployment -Name "webapp"
Returns deployments whose name matches `webapp`.

.EXAMPLE
PS> Get-DuneDeployment -Id 3d8f6b5a-...
Returns the deployment with the specified `Id`.

.EXAMPLE
PS> Get-DuneCollection -Name "prod" | Get-DuneDeployment
Pipeline example using the `Collection` parameter set.
#>

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

        [Parameter()]
        [string]$DisplayName,

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

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

        [Parameter(ParameterSetName = "DeploymentTemplate", ValueFromPipeline)]
        [DuneDeploymentTemplate]$DeploymentTemplate,

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

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

        [Parameter(ParameterSetName = "Job", ValueFromPipeline)]
        [DuneJob]$Job,

        [Parameter()]
        [Environments]$Environment,

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDeploymentTemplate,

        [Parameter()]
        [switch]$IncludeDeleted
    )

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

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

        # Build Uri
        switch ($PSCmdlet.ParameterSetName) {
            'Id' { $Uri = '{0}/{1}' -f $BaseUri, $Id }
            'Collection' { $Uri = '{0}?ParentId={1}' -f $BaseUri, $Collection.Id }
            'ResourceGroup' { $Uri = '{0}/{1}' -f $BaseUri, $ResourceGroup.ParentId }
            'Resource' {
                if ($Resource.Deployment) {
                    return $Resource.Deployment
                }
                else {
                    $Uri = '{0}/{1}' -f $BaseUri, $Resource.DeploymentId
                }
            }
            'DeploymentTemplate' { $Uri = '{0}?TemplateId={1}' -f $BaseUri, $DeploymentTemplate.Id }
            'Job' {
                if ($Job.ConfigItemType -eq "Deployment") {
                    $Uri = '{0}/{1}' -f $BaseUri, $Job.ConfigItemId
                }
                else {
                    Write-Warning "Wrong ConfigItemType $($Job.ConfigItemId) for Job $($Job.Name) (id: $($Job.Id))"
                    return
                }
            }
            Default { $Uri = $BaseUri }
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($DisplayName) {
            $Uri = $Uri | Add-UriQueryParam "DisplayNameILike=$DisplayName" -ConvertWildcards
        }
        if ($IncludeDeleted) {
            $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1"
        }
        if ($PSBoundParameters.ContainsKey('Environment')) {
            $Uri = $Uri | Add-UriQueryParam "Environment=$Environment"
        }

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

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        # if($IncludeDeploymentTemplate){
        # $DeploymentTemplates = Get-DuneDeploymentTemplate
        # foreach ($Object in $ReturnObjects) {
        # $Object.Template = $DeploymentTemplates | Where-Object Id -EQ $Object.TemplateId
        # }
        # }
        if ($IncludeDeploymentTemplate -and ($ReturnObjects | Where-Object { -not $_.Template })) {
            $DeploymentTemplates = if (($ReturnObjects.Count -eq 1) -and (-not $ReturnObjects.Template)) { $ReturnObjects | Get-DuneDeploymentTemplate }else { Get-DuneDeploymentTemplate }
            foreach ($Object in ($ReturnObjects | Where-Object { -not $_.Template })) {
                $Object.Template = $DeploymentTemplates | Where-Object Id -EQ $Object.TemplateId
            }
        }
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object created
    }
}