lib/userdefinedfunctions.ps1
function Set-CosmosDbUserDefinedFunctionType { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] $UserDefinedFunction ) foreach ($item in $UserDefinedFunction) { $item.PSObject.TypeNames.Insert(0, 'CosmosDB.UserDefinedFunction') } return $UserDefinedFunction } function Get-CosmosDbUserDefinedFunctionResourcePath { [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}/udfs/{2}' -f $Database, $CollectionId, $Id) } function Get-CosmosDbUserDefinedFunction { [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}/udfs' -f $CollectionId) if ($PSBoundParameters.ContainsKey('Id')) { $null = $PSBoundParameters.Remove('Id') $userDefinedFunction = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Get' ` -ResourceType 'udfs' ` -ResourcePath ('{0}/{1}' -f $resourcePath, $Id) } else { $result = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Get' ` -ResourceType 'udfs' ` -ResourcePath $resourcePath $userDefinedFunction = $result.UserDefinedFunctions } if ($userDefinedFunction) { return (Set-CosmosDbUserDefinedFunctionType -UserDefinedFunction $userDefinedFunction) } } function New-CosmosDbUserDefinedFunction { [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] $UserDefinedFunctionBody ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $null = $PSBoundParameters.Remove('UserDefinedFunctionBody') $resourcePath = ('colls/{0}/udfs' -f $CollectionId) $UserDefinedFunctionBody = ((($UserDefinedFunctionBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"') $userDefinedFunction = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Post' ` -ResourceType 'udfs' ` -ResourcePath $resourcePath ` -Body "{ `"id`": `"$id`", `"body`" : `"$UserDefinedFunctionBody`" }" if ($userDefinedFunction) { return (Set-CosmosDbUserDefinedFunctionType -UserDefinedFunction $userDefinedFunction) } } function Remove-CosmosDbUserDefinedFunction { [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}/udfs/{1}' -f $CollectionId, $Id) $null = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Delete' ` -ResourceType 'udfs' ` -ResourcePath $resourcePath } function Set-CosmosDbUserDefinedFunction { [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] $UserDefinedFunctionBody ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $null = $PSBoundParameters.Remove('UserDefinedFunctionBody') $resourcePath = ('colls/{0}/udfs/{1}' -f $CollectionId, $Id) $UserDefinedFunctionBody = ((($UserDefinedFunctionBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"') $userDefinedFunction = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Put' ` -ResourceType 'udfs' ` -ResourcePath $resourcePath ` -Body "{ `"id`": `"$id`", `"body`" : `"$UserDefinedFunctionBody`" }" if ($userDefinedFunction) { return (Set-CosmosDbUserDefinedFunctionType -UserDefinedFunction $userDefinedFunction) } } |