lib/storedprocedures.ps1
function Set-CosmosDbStoredProcedureType { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] $StoredProcedure ) foreach ($item in $StoredProcedure) { $item.PSObject.TypeNames.Insert(0, 'CosmosDB.StoredProcedure') } return $StoredProcedure } function Get-CosmosDbStoredProcedureResourcePath { [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}/sprocs/{2}' -f $Database, $CollectionId, $Id) } function Get-CosmosDbStoredProcedure { [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}/sprocs' -f $CollectionId) if ($PSBoundParameters.ContainsKey('Id')) { $null = $PSBoundParameters.Remove('Id') $storedProcedure = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Get' ` -ResourceType 'sprocs' ` -ResourcePath ('{0}/{1}' -f $resourcePath, $Id) } else { $result = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Get' ` -ResourceType 'sprocs' ` -ResourcePath $resourcePath $storedProcedure = $result.StoredProcedures } if ($storedProcedure) { return (Set-CosmosDbStoredProcedureType -StoredProcedure $storedProcedure) } } function Invoke-CosmosDbStoredProcedure { [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()] [ValidateNotNullOrEmpty()] [System.String[]] $StoredProcedureParameter ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $resourcePath = ('colls/{0}/sprocs/{1}' -f $CollectionId, $Id) if ($PSBoundParameters.ContainsKey('StoredProcedureParameter')) { $body = ( $StoredProcedureParameter | ForEach-Object { "`"$_`"" } ) -join ',' $body = "[$body]" $null = $PSBoundParameters.Remove('StoredProcedureParameter') } else { $body = '[]' } return Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Post' ` -ResourceType 'sprocs' ` -ResourcePath $resourcePath ` -Body $body } function New-CosmosDbStoredProcedure { [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] $StoredProcedureBody ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $null = $PSBoundParameters.Remove('StoredProcedureBody') $resourcePath = ('colls/{0}/sprocs' -f $CollectionId) $StoredProcedureBody = ((($StoredProcedureBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"') $storedProcedure = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Post' ` -ResourceType 'sprocs' ` -ResourcePath $resourcePath ` -Body "{ `"id`": `"$id`", `"body`" : `"$StoredProcedureBody`" }" if ($storedProcedure) { return (Set-CosmosDbStoredProcedureType -StoredProcedure $storedProcedure) } } function Remove-CosmosDbStoredProcedure { [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}/sprocs/{1}' -f $CollectionId, $Id) $null = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Delete' ` -ResourceType 'sprocs' ` -ResourcePath $resourcePath } function Set-CosmosDbStoredProcedure { [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] $StoredProcedureBody ) $null = $PSBoundParameters.Remove('CollectionId') $null = $PSBoundParameters.Remove('Id') $null = $PSBoundParameters.Remove('StoredProcedureBody') $resourcePath = ('colls/{0}/sprocs/{1}' -f $CollectionId, $Id) $StoredProcedureBody = ((($StoredProcedureBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"') $storedProcedure = Invoke-CosmosDbRequest @PSBoundParameters ` -Method 'Put' ` -ResourceType 'sprocs' ` -ResourcePath $resourcePath ` -Body "{ `"id`": `"$id`", `"body`" : `"$StoredProcedureBody`" }" if ($storedProcedure) { return (Set-CosmosDbStoredProcedureType -StoredProcedure $storedProcedure) } } |