lib/triggers.ps1
function Set-CosmosDbTriggerType { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] $Trigger ) foreach ($item in $Trigger) { $item.PSObject.TypeNames.Insert(0, 'CosmosDB.Trigger') } return $Trigger } function Get-CosmosDbTriggerResourcePath { [CmdletBinding()] [OutputType([System.String])] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $Database, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $CollectionId, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $Id ) return ('dbs/{0}/colls/{1}/triggers/{2}' -f $Database, $CollectionId, $Id) } function Get-CosmosDbTrigger { [CmdletBinding(DefaultParameterSetName = 'Context')] [OutputType([Object])] param ( [Alias("Connection")] [Parameter(Mandatory = $true, ParameterSetName = 'Context')] [ValidateNotNullOrEmpty()] [CosmosDb.Context] $Context, [Parameter(Mandatory = $true, ParameterSetName = 'Account')] [ValidateNotNullOrEmpty()] [System.String] $Account, [Parameter()] [ValidateNotNullOrEmpty()] [System.Security.SecureString] $Key, [Parameter()] [ValidateSet('master', 'resource')] [System.String] $KeyType = 'master', [Parameter()] [ValidateNotNullOrEmpty()] [System.String] $Database, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $CollectionId, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] $Id ) $null = $PSBoundParameters.Remove('CollectionId') $resourcePath = ('colls/{0}/triggers' -f $CollectionId) if ($PSBoundParameters.ContainsKey('Id')) { $null = $PSBoundParameters.Remove('Id') $result = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Get' ` -ResourceType 'triggers' ` -ResourcePath ('{0}/{1}' -f $resourcePath, $Id) $trigger = ConvertFrom-Json -InputObject $result.Content } else { $result = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Get' ` -ResourceType 'triggers' ` -ResourcePath $resourcePath $body = ConvertFrom-Json -InputObject $result.Content $trigger = $body.Triggers } if ($trigger) { return (Set-CosmosDbTriggerType -Trigger $trigger) } } function New-CosmosDbTrigger { [CmdletBinding(DefaultParameterSetName = 'Context')] [OutputType([Object])] param ( [Alias("Connection")] [Parameter(Mandatory = $true, ParameterSetName = 'Context')] [ValidateNotNullOrEmpty()] [CosmosDb.Context] $Context, [Parameter(Mandatory = $true, ParameterSetName = 'Account')] [ValidateNotNullOrEmpty()] [System.String] $Account, [Parameter()] [ValidateSet('master', 'resource')] [System.String] $KeyType = 'master', [Parameter()] [ValidateNotNullOrEmpty()] [System.Security.SecureString] $Key, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] $Database, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $CollectionId, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $Id, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $TriggerBody, [Parameter(Mandatory = $true)] [ValidateSet('All', 'Create', 'Replace', 'Delete')] [System.String] $TriggerOperation, [Parameter(Mandatory = $true)] [ValidateSet('Pre', 'Post')] [System.String] $TriggerType ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $null = $PSBoundParameters.Remove('TriggerBody') $null = $PSBoundParameters.Remove('TriggerOperation') $null = $PSBoundParameters.Remove('TriggerType') $resourcePath = ('colls/{0}/triggers' -f $CollectionId) $TriggerBody = ((($TriggerBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"') $result = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Post' ` -ResourceType 'triggers' ` -ResourcePath $resourcePath ` -Body "{ `"id`": `"$id`", `"body`" : `"$TriggerBody`", `"triggerOperation`" : `"$triggerOperation`", `"triggerType`" : `"$triggerType`" }" $trigger = ConvertFrom-Json -InputObject $result.Content if ($trigger) { return (Set-CosmosDbTriggerType -Trigger $trigger) } } function Remove-CosmosDbTrigger { [CmdletBinding(DefaultParameterSetName = 'Context')] param ( [Alias("Connection")] [Parameter(Mandatory = $true, ParameterSetName = 'Context')] [ValidateNotNullOrEmpty()] [CosmosDb.Context] $Context, [Parameter(Mandatory = $true, ParameterSetName = 'Account')] [ValidateNotNullOrEmpty()] [System.String] $Account, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] $Database, [Parameter()] [ValidateNotNullOrEmpty()] [System.Security.SecureString] $Key, [Parameter()] [ValidateSet('master', 'resource')] [System.String] $KeyType = 'master', [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $CollectionId, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] $Id ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $resourcePath = ('colls/{0}/triggers/{1}' -f $CollectionId, $Id) $null = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Delete' ` -ResourceType 'triggers' ` -ResourcePath $resourcePath } function Set-CosmosDbTrigger { [CmdletBinding(DefaultParameterSetName = 'Context')] [OutputType([Object])] param ( [Alias("Connection")] [Parameter(Mandatory = $true, ParameterSetName = 'Context')] [ValidateNotNullOrEmpty()] [CosmosDb.Context] $Context, [Parameter(Mandatory = $true, ParameterSetName = 'Account')] [ValidateNotNullOrEmpty()] [System.String] $Account, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] $Database, [Parameter()] [ValidateNotNullOrEmpty()] [System.Security.SecureString] $Key, [Parameter(ParameterSetName = 'Account')] [ValidateSet('master', 'resource')] [System.String] $KeyType = 'master', [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $CollectionId, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $Id, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $TriggerBody, [Parameter(Mandatory = $true)] [ValidateSet('All', 'Create', 'Replace', 'Delete')] [System.String] $TriggerOperation, [Parameter(Mandatory = $true)] [ValidateSet('Pre', 'Post')] [System.String] $TriggerType ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $null = $PSBoundParameters.Remove('TriggerBody') $null = $PSBoundParameters.Remove('TriggerOperation') $null = $PSBoundParameters.Remove('TriggerType') $resourcePath = ('colls/{0}/triggers/{1}' -f $CollectionId, $Id) $TriggerBody = ((($TriggerBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"') $result = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Put' ` -ResourceType 'triggers' ` -ResourcePath $resourcePath ` -Body "{ `"id`": `"$id`", `"body`" : `"$TriggerBody`", `"triggerOperation`" : `"$triggerOperation`", `"triggerType`" : `"$triggerType`" }" $trigger = ConvertFrom-Json -InputObject $result.Content if ($trigger) { return (Set-CosmosDbTriggerType -Trigger $trigger) } } |