Public/Set-DuneActionAssignment.ps1

<#
.SYNOPSIS
Update an action assignment.
 
.DESCRIPTION
Updates an action assignment by sending a PUT request with changed properties. Accepts an assignment object via pipeline or by ID. Only modified properties are sent.
 
.PARAMETER ActionAssignment
A `DuneActionAssignment` object to update. Accepts pipeline input.
 
.PARAMETER ActionAssignmentId
The GUID of the action assignment to update.
 
.PARAMETER ActionId
The GUID of the action to associate.
 
.PARAMETER Parameters
An array of parameter objects to set on the assignment.
 
.PARAMETER CronExpression
A cron expression for scheduled execution.
 
.PARAMETER Raw
If set, returns raw API objects.
 
.EXAMPLE
PS> Set-DuneActionAssignment -ActionAssignmentId $id -CronExpression "0 2 * * *"
Updates the cron schedule for an action assignment.
 
.EXAMPLE
PS> Get-DuneActionAssignment -Name "backup" | Set-DuneActionAssignment -CronExpression "0 3 * * *"
Updates an assignment via pipeline.
#>

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
            }
        }
    }
}