Public/Set-DuneDeploymentLifetime.ps1

<#
.SYNOPSIS
Set or extend a deployment's lifetime.

.DESCRIPTION
Extends or marks a deployment as never expiring. Supports parameter sets for specifying the deployment by `Id` or by passing a `DuneDeployment` object. Use `-ValidUntil` to extend the lifetime or `-NeverExpires` to mark it as never expiring. `ShouldProcess` is observed.

.PARAMETER Id
The GUID of the deployment. Required in the `Id_*` parameter sets.

.PARAMETER Deployment
A `DuneDeployment` object; required in the `Object_*` parameter sets (pipeline input supported).

.PARAMETER ValidUntil
|DateTime to which the deployment lifetime will be extended. Mandatory in `*_ValidUntil` parameter sets.

.PARAMETER NeverExpires
Switch to mark the deployment as never expiring. Mandatory in `*_NeverExpires` parameter sets.

.PARAMETER SuppressOwnerNotification
If set, owner notifications for the lifetime change will be suppressed.

.PARAMETER TxId
Optional transaction id to pass to the API.

.EXAMPLE
PS> Set-DuneDeploymentLifetime -Id 3d8f6b5a-... -ValidUntil (Get-Date).AddDays(30)
Extend the deployment lifetime by 30 days.

.EXAMPLE
PS> Get-DuneDeployment -Name "app" | Set-DuneDeploymentLifetime -NeverExpires
Pipeline example marking a deployment as never expiring.
#>

function Set-DuneDeploymentLifetime {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'None',
        DefaultParameterSetName = "Id_ValidUntil"
    )]
    param (
        [Parameter(Mandatory, ParameterSetName = "Id_ValidUntil")]
        [Parameter(Mandatory, ParameterSetName = "Id_NeverExpires")]
        [guid]$Id,

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

        [Parameter(Mandatory, ParameterSetName = "Id_ValidUntil")]
        [Parameter(Mandatory, ParameterSetName = "Object_ValidUntil")]
        [DateTime]$ValidUntil,

        [Parameter(Mandatory, ParameterSetName = "Id_NeverExpires")]
        [Parameter(Mandatory, ParameterSetName = "Object_NeverExpires")]
        [switch]$NeverExpires,

        [Parameter()]
        [switch]$SuppressOwnerNotification,

        [Parameter()]
        [string]$TxId
    )

    begin {}

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"
        switch ($PSCmdlet.ParameterSetName) {
            {$_ -like "Id_*"} {
                Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)|$($Id)"
                $Deployment = Get-DuneDeployment -Id $Id
            }
            {$_ -like "Object_*"} {
                Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)|$($Deployment.Id)"
            }
            Default {
                return
            }
        }
        if ($ValidUntil) {
            if ($PSCmdlet.ShouldProcess($Deployment.Name)) {
                if ($Deployment.IsNeverExpiring) {
                    $Url = $("deployments/{0}/neverexpires" -f $Deployment.Id)
                    $Body = @{
                        IsNeverExpiring    = $false
                        SendNotifications  = $false
                    }
                    $Null = Invoke-DuneApiRequest $Url -Method PATCH -Body $Body
                }
                $Url = $("deployments/{0}/lifetime/extend" -f $Deployment.Id)
                $Body = @{
                    ValidTo           = $ValidUntil
                    SendNotifications = if($SuppressOwnerNotification){$false}else{$true}
                }
                $Null = Invoke-DuneApiRequest $Url -Method PATCH -Body $Body
            }
        }
        if ($NeverExpires) {
            if ($PSCmdlet.ShouldProcess($Deployment.Name)) {
                $Url = $("deployments/{0}/neverexpires" -f $Deployment.Id)
                $Body = @{
                    IsNeverExpiring    = $true
                    SendNotifications = if($SuppressOwnerNotification){$false}else{$true}
                }
                $Null = Invoke-DuneApiRequest $Url -Method PATCH -Body $Body
            }
        }
    }

    end {}
}