Public/Remove-DuneDeployment.ps1

<#
.SYNOPSIS
Remove a Dune deployment.

.DESCRIPTION
Removes a `DuneDeployment` by `Name`, `Id`, or by passing a `DuneDeployment` object via pipeline. Use `-IncludeChildren` to remove resource groups first, `-Unstage` to unstage instead of deleting, and `-Unlock` to unlock a locked deployment before removal.

.PARAMETER Name
The name of the deployment to remove (Name parameter set).

.PARAMETER Id
The GUID Id of the deployment to remove (Id parameter set).

.PARAMETER Deployment
A `DuneDeployment` object provided via pipeline (Deployment parameter set).

.PARAMETER IncludeChildren
When specified, remove the deployment's resource groups before deleting the deployment.

.PARAMETER Unstage
Unstage the deployment rather than deleting it.

.PARAMETER Unlock
Unlock the deployment prior to deletion.

.EXAMPLE
PS> Remove-DuneDeployment -Id 3d8f6b5a-... -IncludeChildren
Prompts and removes the specified deployment and its resource groups.

.EXAMPLE
PS> Get-DuneDeployment -Name "my-deploy" | Remove-DuneDeployment -Unstage
Pipeline example to unstage a deployment referenced from the pipeline.
#>

function Remove-DuneDeployment {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'High',
        DefaultParameterSetName = 'Id'
    )]
    param (
        [Parameter(Mandatory, ParameterSetName = "Name", Position = 0)]
        [string]$Name,

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

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

        [Parameter()]
        [switch]$IncludeChildren,

        [Parameter()]
        [switch]$Unstage,

        [Parameter()]
        [switch]$Unlock
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $Uri = "deployments"
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"
        switch ($PSCmdlet.ParameterSetName) {
            'Name' {
                Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)|$($Name)"
                $Deployment = Get-DuneDeployment -Name $Name
            }
            'Id' {
                Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)|$($Id)"
                $Deployment = Get-DuneDeployment -Id $Id
            }
            'Deployment' {
                Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)|$($Deployment.Id)"
            }
            Default {
                return
            }
        }
        if ($PSCmdlet.ShouldProcess($Deployment.Name)) {
            If ($Unlock) {
                $Deployment | Unlock-DuneDeployment -Confirm:$false
            }
            If ($Unstage) {
                $Url = $("{0}/{1}/unstage" -f $Uri, $Deployment.Id)
                $Null = Invoke-DuneApiRequest $Url -Method POST
            }
            else {
                $Url = $("{0}/{1}" -f $Uri, $Deployment.Id)
                if ($IncludeChildren) {
                    $Deployment | Get-DuneResourceGroup | Remove-DuneResourceGroup -IncludeChildren
                }
                $Null = Invoke-DuneApiRequest $Url -Method DELETE
            }
        }
    }

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