Public/Set-DunePermission.ps1

<#
.SYNOPSIS
Change the role of an existing permission.

.DESCRIPTION
Updates an existing permission by assigning it a new role (PATCH request). The permission can be supplied as a `DunePermission` object (pipeline input supported) or by id. The new role can be supplied as a `DuneRole` object or by id; its `AppliesTo` must match the permission's config item type.

.PARAMETER Permission
A `DunePermission` object to update (pipeline input supported).

.PARAMETER Id
The GUID of the permission to update.

.PARAMETER Role
A `DuneRole` object to assign to the permission.

.PARAMETER RoleId
The GUID of the role to assign to the permission.

.EXAMPLE
PS> Get-DuneDeployment -Name "webapp" | Get-DunePermission | Set-DunePermission -Role $OperatorRole
Changes all permissions on the deployment `webapp` to the Operator role.

.EXAMPLE
PS> Set-DunePermission -Id $PermissionId -RoleId $RoleId
Assigns a new role to the permission with the specified id.
#>

function Set-DunePermission {
    [CmdletBinding(
        SupportsShouldProcess,
        DefaultParameterSetName = 'Id'
    )]
    param(
        [Parameter(ValueFromPipeline, ParameterSetName = 'Object')]
        [DunePermission]$Permission,

        [Parameter(ParameterSetName = 'Id')]
        [guid]$Id,

        [Parameter()]
        [DuneRole]$Role,

        [Parameter()]
        [guid]$RoleId
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        if ($Role) { $RoleId = $Role.Id }
        if ($RoleId -eq [guid]::Empty) { throw 'Provide -Role or -RoleId.' }
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"
        if ($Permission) { $Id = $Permission.Id }

        $Target = if ($Permission) { $Permission.ToString() } else { $Id }
        if ($PSCmdlet.ShouldProcess($Target, "Set Role")) {
            $Uri = "authorization/permissions/{0}/role/{1}" -f $Id, $RoleId
            $null = Invoke-DuneApiRequest -Uri $Uri -Method PATCH -ErrorAction Stop
        }
    }

    end {}
}