-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNew-CosmosCollection.ps1
85 lines (72 loc) · 3.17 KB
/
New-CosmosCollection.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function New-CosmosCollection {
<#
.SYNOPSIS
Creates a newCosmosDB Collection
.DESCRIPTION
Creates a new CosmosDB Collection in the specified Database
.PARAMETER DatabaseName
Name of the Database where the Collection should be created
.PARAMETER CollectionName
Name of the new Collection
.PARAMETER PartitionKey
Partition key for sharding collections larger than 10GB. Setting this parameter configures the collection size to 'unlimited'.
.PARAMETER OfferThroughput
Request Units for collection to be created.
.PARAMETER CosmosDBVariables
This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
.EXAMPLE
New-CosmosCollection -DatabaseName MyPrivateCosmos -CollectionName TohuVaBohu
.EXAMPLE
New-CosmosCollection -DatabaseName MyPrivateCosmos -CollectionName TohuVaBohu -partitionKey "location" -offerThroughput 2000
.NOTES
https://docs.microsoft.com/en-us/rest/api/documentdb/create-a-collection
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,
HelpMessage='Name of the Database containing the Collection')]
[string]$DatabaseName,
[Parameter(Mandatory=$true,
HelpMessage='Name of your Collection')]
[string]$CollectionName,
[Parameter(Mandatory=$false,
HelpMessage="Request Units for collection creation")]
[string]$partitionKey,
[Parameter(Mandatory=$false,
HelpMessage="Partition key for sharding collections larger than 10GB")]
[int]$OfferThroughput,
[Parameter(Mandatory=$false,
HelpMessage="Use Connect-CosmosDB to create this Variable collection")]
[hashtable]$CosmosDBVariables=$Script:CosmosDBVariables
)
begin {
Test-CosmosDBVariable $CosmosDBVariables
$Database = $CosmosDBConnection[$($DatabaseName + '_db')]
if (-not $Database) {
Write-Warning "Database $DatabaseName not found"
continue
}
}
process {
# Add preceeding forward slash if not present
if ($partitionKey -notmatch "^/" -and $partitionKey) {$partitionKey = "/$partitionKey"}
$Verb = 'POST'
$Url = '{0}/{1}/colls' -f $CosmosDBVariables['URI'],$Database._self
$ResourceType = 'colls'
$Header = New-CosmosDBHeader -resourceId $Database._rid -resourceType $ResourceType -Verb $Verb -OfferThroughput $OfferThroughput
$Body = @{id=$CollectionName}
if ($partitionKey) { $Body.Add("partitionKey", @{"paths"=@($PartitionKey); "kind"="Hash"}) }
$CosmosBody = $Body | ConvertTo-Json
try {
$Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -Body $CosmosBody -ErrorAction Stop
$Return = Get-CosmosCollection -DatabaseName $DatabaseName -CollectionName $CollectionName
$script:CosmosDBConnection[$Database.id][$CollectionName] = $Return
Write-Verbose "Collection $CollectionName has been created"
}
catch {
Write-Warning -Message $_.Exception.Message
}
}
end {
}
}