Public/Get-DuneVariable.ps1

<#
.SYNOPSIS
Retrieve Dune variables for a tenant, deployment, resource group, or resource.

.DESCRIPTION
Gets variables scoped to a tenant, deployment, resource group, or resource. Supports pipeline input for `Deployment`, `ResourceGroup`, and `Resource`. Use `-ExpandValue` to return only values and `-Raw` for raw API objects.

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

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

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

.PARAMETER Tenant
Switch to return tenant-level variables.

.PARAMETER Name
Filter variable results by name (supports wildcard matching).

.PARAMETER ExpandValue
If set, return only variable values rather than full variable objects.

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

.EXAMPLE
PS> Get-DuneVariable -Tenant
Returns tenant-scoped variables.

.EXAMPLE
PS> Get-DuneDeployment -Name "app" | Get-DuneVariable
Pipeline example using the `Deployment` parameter set.

.EXAMPLE
PS> Get-DuneVariable -Name "DB_CONN" -ExpandValue
Returns the value of the `DB_CONN` variable.
#>

function Get-DuneVariable {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '',
        Justification = 'The parameter is indirectly used on ParameterSetName switch.')]
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline, ParameterSetName = "Deployment")]
        [DuneDeployment]$Deployment,

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

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

        [Parameter(ParameterSetName = "Tenant")]
        [switch]$Tenant,

        [Parameter()]
        [string]$Name,

        [Parameter()]
        [switch]$ExpandValue,

        [Parameter()]
        [Switch]$Raw
    )

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

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

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Deployment' { 'deployments/{0}/variables?IncludeInherited=true' -f $Deployment.Id }
            'ResourceGroup' { 'resourcegroups/{0}/variables?IncludeInherited=true' -f $ResourceGroup.Id }
            'Resource' { 'resources/{0}/variables?IncludeInherited=true' -f $Resource.Id }
            'Tenant' { 'tenants/variables?IncludeInherited=true' }
        }

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                # ApiCall and Object conversion
                $Response = Invoke-DuneApiRequest -Uri $Uri -Method $Method
                $ProcessedUrls += $Uri
                $Results = if ($Response.Content) { $Response.Content | ConvertFrom-Json }
                $ReturnObjects += $Results.items | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneVariable -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        if ($Name) {
            $ReturnObjects = $ReturnObjects | Where-Object Name -Like $Name
        }
        if ($ExpandValue) {
            $ReturnObjects.Value
        }
        else {
            return $ReturnObjects
        }
    }
}