Public/Get-DuneDeploymentTemplateDefinition.ps1

<#
.SYNOPSIS
Retrieve a deployment template's definition body as YAML or JSON.

.DESCRIPTION
Calls `GET /deploymenttemplates/{Id}/definition` (or `/deploymenttemplates/{Name}/{Version}/definition`) and returns the `Definition` field from the response. Defaults to YAML output; use `-Format Json` to get the JSON representation.

.PARAMETER Id
The GUID of the deployment template.

.PARAMETER Name
The template name. Requires `-Version`.

.PARAMETER Version
The template version. Required when using `-Name`.

.PARAMETER Template
A `DuneDeploymentTemplate` object; returns the definition of the supplied template (pipeline input supported).

.PARAMETER Format
Requested definition format. `Yaml` (default) or `Json`.

.PARAMETER IncludeReferencedObjects
If set, forwards `IncludeReferencedObjects=1` to the API.

.PARAMETER IncludeDeleted
If set, forwards `IncludeDeleted=1` to the API.

.EXAMPLE
PS> Get-DuneDeploymentTemplateDefinition -Id 3d8f6b5a-...
Returns the template's definition as a YAML string.

.EXAMPLE
PS> Get-DuneDeploymentTemplateDefinition -Name "webapp" -Version "1.2.0" -Format Json
Returns the JSON definition via the Name/Version route.

.EXAMPLE
PS> Get-DuneDeploymentTemplate -Name "webapp" | Get-DuneDeploymentTemplateDefinition
Pipeline example; returns the YAML definition of the template.
#>

function Get-DuneDeploymentTemplateDefinition {
    [CmdletBinding(DefaultParameterSetName = 'Id')]
    param (
        [Parameter(ParameterSetName = 'Id', Mandatory, Position = 0)]
        [guid]$Id,

        [Parameter(ParameterSetName = 'Name', Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(ParameterSetName = 'Name', Mandatory, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string]$Version,

        [Parameter(ParameterSetName = 'Template', Mandatory, ValueFromPipeline)]
        [DuneDeploymentTemplate]$Template,

        [Parameter()]
        [ValidateSet('Yaml', 'Json')]
        [string]$Format = 'Yaml',

        [Parameter()]
        [switch]$IncludeReferencedObjects,

        [Parameter()]
        [switch]$IncludeDeleted
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $BaseUri = 'deploymenttemplates'
        $Method  = 'GET'
    }

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

        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id'       { '{0}/{1}/definition'       -f $BaseUri, $Id }
            'Name'     { '{0}/{1}/{2}/definition'   -f $BaseUri, $Name, $Version }
            'Template' { '{0}/{1}/definition'       -f $BaseUri, $Template.Id }
        }

        $Uri = $Uri | Add-UriQueryParam "DefinitionFormat=$Format"
        if ($IncludeReferencedObjects) { $Uri = $Uri | Add-UriQueryParam 'IncludeReferencedObjects=1' }
        if ($IncludeDeleted)           { $Uri = $Uri | Add-UriQueryParam 'IncludeDeleted=1' }

        $Response = Invoke-DuneApiRequest -Uri $Uri -Method $Method
        $Dto      = $Response.Content | ConvertFrom-Json

        $Dto.Definition
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
    }
}