Public/Remove-DuneTag.ps1

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

.DESCRIPTION
Removes one or more tags from the specified scope. Scope is determined by parameter sets: `Tenant`, `Deployment`, `ResourceGroup`, or `Resource`. The `Name` parameter accepts one or more tag names. Supports `ShouldProcess` semantics.

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

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

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

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

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

.EXAMPLE
PS> Remove-DuneTag -Tenant (Get-DuneTenant -Name "contoso") -Name "env"
Removes the `env` tag from tenant-scoped tags.

.EXAMPLE
PS> Get-DuneDeployment -Name "app" | Remove-DuneTag -Name "obsolete"
Pipeline example removing a tag from a deployment.
#>

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

        [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) {
            $RemoveSingleTagParam = $PSBoundParameters
            $Name | ForEach-Object {
                $RemoveSingleTagParam.Name = $_
                Remove-DuneTag @RemoveSingleTagParam
            }
        }
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"
        switch ($PSCmdlet.ParameterSetName) {
            'Tenant' {
                $Uri = 'tenants/tags'
                $Tenant = Get-DuneTenant
                $Tags = $Tenant.Tags
            }
            'Deployment' {
                $Uri = 'deployments/{0}/tags' -f $Deployment.Id
                $Deployment = Get-DuneDeployment -Id $Deployment.Id
                $Tags = $Deployment.Tags
            }
            'ResourceGroup' {
                $Uri = 'resourcegroups/{0}/tags' -f $ResourceGroup.Id
                $ResourceGroup = Get-DuneResourceGroup -Id $ResourceGroup.Id
                $Tags = $ResourceGroup.Tags
            }
            'Resource' {
                $Uri = 'resources/{0}/tags' -f $Resource.Id
                $Resource = Get-DuneResource -Id $Resource.Id
                $Tags = $Resource.Tags
            }
            Default {
                return 'Type has no tags.'
            }
        }
        $Body = @{tags = @() }
        if ($Tags | Where-Object Name -NE $Name) { $Body.tags += ($Tags | Where-Object Name -NE $Name).ToPropertiesHashtable() }
        $Null = Invoke-DuneApiRequest -Uri $Uri -Method $Method -Body $Body
    }

    end {}
}