Public/Remove-DuneVariable.ps1

<#
.SYNOPSIS
Remove variables from a tenant, collection, deployment, resource group, or resource.

.DESCRIPTION
Removes one or more variables from the specified scope. The scope is determined using parameter sets: `Tenant`, `Collection`, `Deployment`, `ResourceGroup`, or `Resource`. The `Name` parameter accepts one or more variable names. Supports `ShouldProcess` semantics.

.PARAMETER Tenant
A `DuneTenant` object; removes variables at tenant scope (pipeline input supported).

.PARAMETER Collection
A `DuneCollection` object; removes variables at collection scope (pipeline input supported).

.PARAMETER Deployment
A `DuneDeployment` object; removes variables at deployment scope (pipeline input supported).

.PARAMETER ResourceGroup
A `DuneResourceGroup` object; removes variables at the resource group scope (pipeline input supported).

.PARAMETER Resource
A `DuneResource` object; removes variables at the resource scope (pipeline input supported).

.PARAMETER Name
One or more variable names to remove. Mandatory.

.EXAMPLE
PS> Remove-DuneVariable -Tenant (Get-DuneTenant -Name "contoso") -Name "MY_VAR"
Removes `MY_VAR` from tenant-scoped variables.

.EXAMPLE
PS> Get-DuneDeployment -Name "app" | Remove-DuneVariable -Name "MY_VAR"
Pipeline example removing a variable for a deployment.
#>

function Remove-DuneVariable {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'None'
    )]
    param (
        [Parameter(ValueFromPipeline, ParameterSetName = "Tenant")]
        [DuneTenant]$Tenant,

        [Parameter(ValueFromPipeline, ParameterSetName = "Collection")]
        [DuneCollection]$Collection,

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

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

        [Parameter(ValueFromPipeline, ParameterSetName = "Resource")]
        [DuneResource]$Resource,

        [Parameter(Mandatory, Position = 0)]
        [String[]]$Name
    )

    begin {
        $Method = 'PATCH'
        if ($Name -is [Array] -and $Name.Count -gt 1) {
            $RemoveSingleVarParam = $PSBoundParameters
            $Name | ForEach-Object {
                $RemoveSingleVarParam.Name = $_
                Remove-DuneVariable @RemoveSingleVarParam
            }
        }
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"
        switch ($PSCmdlet.ParameterSetName) {
            'Tenant' {
                $Uri = 'tenants/variables'
                $Tenant = Get-DuneTenant
                $Variables = $Tenant.Variables
            }
            'Collection' {
                $Uri = 'collections/{0}/variables' -f $Collection.Id
                $Collection = Get-DuneCollection -Id $Collection.Id
                $Variables = $Collection.Variables
            }
            'Deployment' {
                $Uri = 'deployments/{0}/variables' -f $Deployment.Id
                $Deployment = Get-DuneDeployment -Id $Deployment.Id
                $Variables = $Deployment.Variables
            }
            'ResourceGroup' {
                $Uri = 'resourcegroups/{0}/variables' -f $ResourceGroup.Id
                $ResourceGroup = Get-DuneResourceGroup -Id $ResourceGroup.Id
                $Variables = $ResourceGroup.Variables
            }
            'Resource' {
                $Uri = 'resources/{0}/variables' -f $Resource.Id
                $Resource = Get-DuneResource -Id $Resource.Id
                $Variables = $Resource.Variables
            }
            Default {
                return 'Type has no variables.'
            }
        }
        $Body = @{variables = ($Variables.ToPropertiesHashtableJsonValue() | ? Name -ne $Name)}
        $Null = Invoke-DuneApiRequest -Uri $Uri -Method $Method -Body $Body
    }

    end {}
}