Public/Set-DuneActionAssignment.ps1

<#
.SYNOPSIS
Update an action assignment.

.DESCRIPTION
PATCHes the specified action-assignment on the target resource path. Use `-AssignmentId` to target a specific assignment.
#>

function Set-DuneActionAssignment {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact='None',
        DefaultParameterSetName='Id'
    )]
    param(
        [Parameter(ParameterSetName='Object',ValueFromPipeline)]
        [DuneActionAssignment]$ActionAssignment,

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

        [Parameter()]
        [guid]$ActionId,

        [Parameter()]
        [object[]]$Parameters,

        [Parameter()]
        [string]$CronExpression,

        [Parameter()]
        [switch]$Raw
    )

    begin {
        # These properties if will be filtered out and not handled at all.
        $FilteredProperties = @(
            'Action'
        )
    }

    process {
        # $BaseUri = switch ($ActionAssignment.ConfigItemType) {
        # 'Deployment' { "deployments/$($Config)/action-assignments" }
        # 'ResourceGroup' { "resourcegroups/$($ResourceGroup.Id)/action-assignments" }
        # 'Resource' { "resources/$($Resource.Id)/action-assignments" }
        # default {Write-Warning "Wrong ConfigItemType: $($ActionAssignment.ConfigItemType)"}
        # }
        # if ($PSCmdlet.ParameterSetName -eq 'Deployment') { $base = "deployments/$($Deployment.Id)/action-assignments" }
        # elseif ($PSCmdlet.ParameterSetName -eq 'ResourceGroup') { $base = "resourcegroups/$($ResourceGroup.Id)/action-assignments" }
        # else { $base = "resources/$($Resource.Id)/action-assignments" }

        # $Uri = "{0}/{1}" -f $BaseUri, $AssignmentId
        if ($PSCmdlet.ParameterSetName -eq 'Id') {
            $ActionAssignment = Get-DuneActionAssignment -Id $ActionAssignmentId
        }

        $Uri = 'action-assignments/{0}' -f $ActionAssignment.Id

        $PropertyUpdates = @()
        $ModifiedProperties = $PSBoundParameters.Keys | Where-Object { $_ -notin $FilteredProperties }
        if (-not $ModifiedProperties) {
            return
        }

        $ActionAssignmentHT = ConvertTo-Hashtable $ActionAssignment
        foreach ($Key in $ModifiedProperties) {
            $Value = $PSBoundParameters.$Key
            if ($Key -in $SpecialProperties) { continue }
            if ($Key -in $DoNotOverridePropertiesIfEmpty -and (($Value -is [string] -and [string]::IsNullOrEmpty($Value)) -or ($Value -is [int] -and (-not $Value)))) { continue }
            if (($ActionAssignment | Get-Member -MemberType Property).Name -notcontains $Key) { continue }
            if ($Value -is [string] -and $Value -eq $ActionAssignment.$Key) { continue }
            if ($Value -is [string] -and [string]::IsNullOrEmpty($Value)) { continue }
            if ($Value -is [array] -and (($Value | Sort-Object | ConvertTo-Json -Compress) -eq ($ActionAssignment.$Key | Sort-Object | ConvertTo-Json -Compress))) { continue }
            if ($Value -is [array] -and $Value.Count -eq 0) {
                Write-Debug "Modify input value (Value: $Value, Type: $($Value.GetType().FullName)) and set it to empty array."
                $Value = @()
            }
            if ($ActionAssignment.$Key -is [DateTime]) {
                Write-Debug "Modifiying input value (Value: $Value, Type: $($Value.GetType().FullName)) and change it to UTC."
                $Value = $Value.ToUniversalTime()
            }
            $ActionAssignmentHT.$Key = $Value
            $PropertyUpdates += @{$Key = $Value }
        }
        Foreach ($Parameter in $ActionAssignmentHT.Parameters) {
            $Parameter.Value = ConvertTo-Json -InputObject $Parameter.Value
        }
        if ($PropertyUpdates) {
            $Body = $ActionAssignmentHT
            if ($PSCmdlet.ShouldProcess($($PropertyUpdates | ConvertTo-Json -Depth 16 -Compress))) {
                $null = Invoke-DuneApiRequest -Uri $Uri -Method PUT -Body $Body
            }
        }
    }
}