Public/Get-DunePackage.ps1

<#
.SYNOPSIS
Retrieve resource addon packages.

.DESCRIPTION
Gets package information for resource addons. Supports filtering by `Name`, `Id`, `Resource`, `DisplayName`, `Version`, and `Vendor`. Returns `DunePackage` objects by default; use `-Raw` for raw API output.

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

.PARAMETER Id
The GUID of a package. Use the `Id` parameter set to retrieve a specific package.

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

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

.PARAMETER Version
Filter by package version.

.PARAMETER Vendor
Filter by vendor (supports wildcards).

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

.EXAMPLE
PS> Get-DunePackage -Name "chef-client"
Returns packages with names matching `chef-client`.

.EXAMPLE
PS> Get-DunePackage -Id 3d8f6b5a-...
Returns the package with the specified `Id`.

.EXAMPLE
PS> Get-DuneResource -Name "webvm" | Get-DunePackage
Pipeline example using the `Resource` parameter set.
#>

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

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

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

        [Parameter()]
        [string]$DisplayName,

        [Parameter()]
        [string]$Version,

        [Parameter()]
        [string]$Vendor,

        [Parameter()]
        [switch]$Raw
    )

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

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

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { '{0}?Id={1}' -f $BaseUri, $Id }
            'Resource' { '{0}?ResourceId={1}' -f $BaseUri, $Resource.Id }
            Default { $BaseUri }
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($DisplayName) {
            $Uri = $Uri | Add-UriQueryParam "DisplayNameILike=$DisplayName" -ConvertWildcards
        }
        if ($Version) {
            $Uri = $Uri | Add-UriQueryParam "VersionLike=$Version" -ConvertWildcards
        }
        if ($Vendor) {
            $Uri = $Uri | Add-UriQueryParam "VendorILike=$Vendor" -ConvertWildcards
        }

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                # ApiCall and Object conversion
                $ResultItems = Invoke-DuneApiRequest -Uri $Uri -Method $Method -ExtractItems
                $ProcessedUrls += $Uri
                $ReturnObjects += $ResultItems | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DunePackage -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
    }
}