Public/New-DuneActionAssignment.ps1

<#
.SYNOPSIS
Create or set an action assignment on a deployment/resourcegroup/resource.
 
.DESCRIPTION
POSTs an action-assignment to the action-assignments endpoint. Specify the target via a Deployment, ResourceGroup, or Resource object and provide an ActionId or Action.
 
.PARAMETER Deployment
A `DuneDeployment` object to assign the action to (pipeline input supported).
 
.PARAMETER ResourceGroup
A `DuneResourceGroup` object to assign the action to (pipeline input supported).
 
.PARAMETER Resource
A `DuneResource` object to assign the action to (pipeline input supported).
 
.PARAMETER Name
The name for the action assignment. Required.
 
.PARAMETER ActionId
The GUID of the action to assign.
 
.PARAMETER Action
A `DuneAction` object to assign (pipeline input supported).
 
.PARAMETER Parameters
A hashtable of parameters to pass to the action.
 
.PARAMETER CronExpression
A cron expression for scheduled execution.
 
.EXAMPLE
PS> New-DuneActionAssignment -Name "backup" -ActionId $actionId -Deployment $dep
Creates an action assignment on a deployment.
#>

function New-DuneActionAssignment {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param(
        [Parameter(ParameterSetName = 'Deployment', ValueFromPipeline)]
        [Parameter(ParameterSetName = 'DeploymentId')]
        [DuneDeployment]$Deployment,

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

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

        [Parameter(Mandatory = $true)]
        [string]$Name,

        [Parameter(ParameterSetName = 'Deployment')]
        [Parameter(ParameterSetName = 'ResourceGroup')]
        [Parameter(ParameterSetName = 'Resource')]
        [guid]$ActionId,

        [Parameter(ParameterSetName = 'DeploymentId', ValueFromPipeline)]
        [Parameter(ParameterSetName = 'ResourceGroupId', ValueFromPipeline)]
        [Parameter(ParameterSetName = 'ResourceId', ValueFromPipeline)]
        [DuneAction]$Action,

        [Parameter()]
        [hashtable]$Parameters,

        [Parameter()]
        [string]$CronExpression
    )

    begin {}

    process {
        $Uri = 'action-assignments'

        $Body = @{
            ActionId = $ActionId
            Name     = $Name
        }

        switch ($PSCmdlet.ParameterSetName) {
            'Deployment' { 
                $Body.ConfigItemId = $Deployment.Id
                $Body.ConfigItemType = "Deployment"
            }
            'ResourceGroup' {
                $ResourceGroup.Id
                $Body.ConfigItemType = "ResourceGroup"
            }
            'Resource' {
                $Resource.Id
                $Body.ConfigItemType = "Resource"
            }
        }

        if ($PSBoundParameters.ContainsKey('Parameters')) { $Body.Parameters = $Parameters }
        if ($PSBoundParameters.ContainsKey('CronExpression')) { $Body.CronExpression = $CronExpression }

        $Return = Invoke-DuneApiRequest -Uri $Uri -Method POST -Body $Body
        $ReturnObject = if ($Return.Content) { $Return.Content | ConvertFrom-Json | ConvertTo-DuneClassObject -Class DuneActionAssignment }
        return $ReturnObject
    }

    end {}
}