Public/Get-DuneResourceGroupTemplate.ps1
|
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 } } |