Public/Get-DuneResourceGroupTemplate.ps1

<#
.SYNOPSIS
Retrieve resource group templates.

.DESCRIPTION
Gets resource group templates and supports parameter sets for `Name` (with optional `Version`), `Id`, `ResourceGroup` (pipeline input), and `DeploymentTemplate` (pipeline input). You may request raw API objects or include full definitions.

.PARAMETER Name
Filter templates by name (supports wildcards). Use the `Name` parameter set.

.PARAMETER Version
Filter templates by version (used with the `Name` parameter set).

.PARAMETER Id
The GUID of the resource group template. Use the `Id` parameter set.

.PARAMETER ResourceGroup
A `DuneResourceGroup` object; returns the template referenced by the supplied resource group (pipeline input supported).

.PARAMETER DeploymentTemplate
A `DuneDeploymentTemplate` object; filters templates referenced by the deployment template (pipeline input supported).

.PARAMETER Raw
If set, returns raw API objects.

.PARAMETER IncludeDefinition
If set, include the full template definition in returned objects.

.PARAMETER IncludeDeleted
Include deleted templates in results.

.EXAMPLE
PS> Get-DuneResourceGroupTemplate -Name "rg-template"
Returns resource group templates named `rg-template`.

.EXAMPLE
PS> Get-DuneResourceGroupTemplate -Id 3d8f6b5a-...
Returns the template with the specified `Id`.

.EXAMPLE
PS> Get-DuneDeploymentTemplate -Name "webapp" | Get-DuneResourceGroupTemplate -DeploymentTemplate
Returns templates referenced by the deployment template.
#>

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

        [Parameter(ParameterSetName = "Name", Position = 1)]
        [string]$Version,

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

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

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

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDefinition,

        [Parameter()]
        [switch]$IncludeDeleted
    )

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

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

        # Build Uri
        switch ($PSCmdlet.ParameterSetName) {
            'Id' { $Uri = '{0}/{1}' -f $BaseUri, $Id }
            'Name' {
                $Uri = $BaseUri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
                if ($Version) { $Uri = $Uri | Add-UriQueryParam "VersionILike=$Version" -ConvertWildcards }
            }
            'ResourceGroup' { $Uri = '{0}/{1}' -f $BaseUri, $ResourceGroup.TemplateId }
            Default { $Uri = $BaseUri }
        }
        if ($PSCmdlet.ParameterSetName -in ('Id', 'ResourceGroup')) {
            $Uri = $Uri | Add-UriQueryParam "IncludeDefinition=1"
        }
        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', 'ResourceGroup') { $Results = $Results.Items } # list endpoint returns content in items property
                $ResultObjects = $Results | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneResourceGroupTemplate -InputObject $_
                    }
                }

                # Filters
                if ($PSCmdlet.ParameterSetName -eq 'DeploymentTemplate') {
                    If (-not $DeploymentTemplate.Definition) { $DeploymentTemplate = Get-DuneDeploymentTemplate -Id $DeploymentTemplate.Id -IncludeDefinition }
                    $ResourceGroupsFromDT = ($DeploymentTemplate).definition | ConvertFrom-Json | Select-Object -ExpandProperty resourceGroups
                    foreach ($RG in $ResourceGroupsFromDT) {
                        $ReturnObjects += $ResultObjects | Where-Object { $_.Name -eq $RG.template -and $_.Version -eq $RG.templateVersion }
                    }
                }
                else {
                    $ReturnObjects += $ResultObjects
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        if ($PSCmdlet.ParameterSetName -notin 'Id', 'ResourceGroup' -and $IncludeDefinition -and -not $Raw) {
            $ReturnObjects | ForEach-Object {
                $_.Definition = ((Invoke-DuneApiRequest -Uri "$BaseUri/$($_.Id)/definition" -Method 'GET').Content | ConvertFrom-Json).Definition
            }
        }
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object -Property Name, Version
    }
}