Public/Get-DuneResourceGroup.ps1

<#
.SYNOPSIS
Retrieve resource groups.

.DESCRIPTION
Gets resource groups and supports filtering by name, display name, id, resource group template, deployment, resource, and job. Returns `DuneResourceGroup` objects by default; use `-Raw` for raw API output.

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

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

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

.PARAMETER ResourceGroupTemplate
A `DuneResourceGroupTemplate` object; returns resource groups based on the template (pipeline input supported).

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

.PARAMETER Resource
A `DuneResource` object; returns the resource group's parent of the supplied resource (pipeline input supported).

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

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

.PARAMETER IncludeDeleted
Include deleted resource groups in results.

.EXAMPLE
PS> Get-DuneResourceGroup -Name "rg-*"
Returns resource groups whose names match the pattern.

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

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

        [Parameter()]
        [string]$DisplayName,

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

        [Parameter(ParameterSetName = "ResourceGroupTemplate", ValueFromPipeline)]
        [DuneResourceGroupTemplate]$ResourceGroupTemplate,

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

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

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

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDeleted
    )

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

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

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { '{0}/{1}' -f $BaseUri, $Id }
            'Deployment' { '{0}?ParentId={1}' -f $BaseUri, $Deployment.Id }
            'Resource' { '{0}/{1}' -f $BaseUri, $Resource.ParentId }
            'ResourceGroupTemplate' { '{0}?TemplateId={1}' -f $BaseUri, $ResourceGroupTemplate.Id }
            'Job' {
                if ($Job.ConfigItemType -eq "ResourceGroup") {
                    $BaseUri, $Job.ConfigItemId -join '/'
                }
                else {
                    Write-Warning "Wrong ConfigItemType $($Job.ConfigItemId) for Job $($Job.Name) (id: $($Job.Id))"
                    return
                }
            }
            Default { $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"
        }

        # 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 }
                if ($PSCmdlet.ParameterSetName -notin 'Id', 'Resource', 'Job') { $Results = $Results.Items } # list endpoint returns content in items property
                $ReturnObjects += $Results | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneResourceGroup -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end|$($PSCmdlet.ParameterSetName)"
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object Name
    }
}