-
Notifications
You must be signed in to change notification settings - Fork 0
/
CosmosDB.psm1
210 lines (168 loc) · 6.72 KB
/
CosmosDB.psm1
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<#
.Synopsis
Perform REST API methods with Azure CosmosDB.
.Description
Sent REST API requests (GET/POST/PUT/DELETE).
Tested on Triggers only!
MSDN documentation: https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources
.Parameter EndPoint
CosmosDB URL
https://<db>.documents.azure.com/
.Parameter DataBaseId
Database Id
.Parameter CollectionId
Collection Id
.Parameter MasterKey
Use the Primary Key provided in CosmosDB account Keys!
.Parameter Verb
Type of request:
GET/POST/PUT/DELETE supported only
.Parameter ResourceType
e.g. triggers
.Parameter ResourceName
e.g. <trigger_name>: "trigger1"
.Parameter Query
Hashtable like:
$query = @{
body="function foo(){text}";
id="foo";
triggerOperation="Create";
triggerType="Pre";}
.Parameter QueryType
Define type of query.
Could be in HashTable or JSON format.
If in JSON convertation will be skipped.
.Example
<#EXAMPLE of GET trigger method
$triggers = Query-CosmosDB -EndPoint "https://<link>.documents.azure.com/" `
-DataBaseId "<coll-id>" `
-CollectionId "<db-id>" `
-MasterKey "<Primary_Key==>" `
-Verb "GET" `
-ResourceType "triggers"
$triggersList = $triggers | ConvertFrom-Json
$triggersList.Triggers.id
#>
Add-Type -AssemblyName System.Web
Function Generate-MasterKeyAuthorizationSignature
{
[CmdletBinding()]
Param
(
# The HTTP verb: GET, POST, or PUT.
[Parameter(Mandatory=$true)][String]$verb,
# Identity property of the resource that the request is directed at.
# ResourceLink must maintain its case for the ID of the resource.
# Example, for a collection it looks like: "dbs/MyDatabase/colls/MyCollection".
[String]$resourceLink,
# Identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs".
[Parameter(Mandatory=$true)][String]$resourceType,
# RFC 7231: Tue, 01 Nov 1994 08:12:31 GMT
[Parameter(Mandatory=$true)][String]$dateTime,
[Parameter(Mandatory=$true)][String]$key,
[Parameter(Mandatory=$true)][String]$keyType,
[Parameter(Mandatory=$true)][String]$tokenVersion
)
$hmacSha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacSha256.Key = [System.Convert]::FromBase64String($key)
If ($resourceLink -eq $resourceType) {
$resourceLink = ""
}
$payLoad = "$($verb.ToLowerInvariant())`n$($resourceType.ToLowerInvariant())`n$resourceLink`n$($dateTime.ToLowerInvariant())`n`n"
$hashPayLoad = $hmacSha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($payLoad))
$signature = [System.Convert]::ToBase64String($hashPayLoad);
Write-Verbose "> Payload: $($payLoad | Out-String)"
[System.Web.HttpUtility]::UrlEncode("type=$keyType&ver=$tokenVersion&sig=$signature")
}
function Query-CosmosDB {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true)]
[String]$EndPoint,
[Parameter(Mandatory = $true)]
[String]$MasterKey,
[Parameter(Mandatory = $true)]
[string]$Verb,
[Parameter(Mandatory = $true)]
[String]$ResourceType,
[String]$DataBaseId,
[String]$CollectionId,
[string]$ResourceName,
$Query,
# JSON or HashTable
$QueryType,
$OfferThroughput #Optional The user specified throughput for the collection expressed in units of 100 request units per second.
#It can be between 400 and 250, 000 (or higher by requesting a limit increase).
)
$dateTime = [DateTime]::UtcNow.ToString("r")
# Generate Query URL
switch ($ResourceType) {
"dbs" {$ResourceLink = ""}
"colls" {$ResourceLink = "dbs/$DataBaseId"}
"triggers" {$ResourceLink = "dbs/$DataBaseId/colls/$CollectionId"}
"docs" {$ResourceLink = "dbs/$DataBaseId/colls/$CollectionId"}
#"attachments" {$ResourceLink = "dbs/$DataBaseId/colls/$CollectionId/docs/{doc-name}"}
"sprocs" {$ResourceLink = "dbs/$DataBaseId/colls/$CollectionId"}
"udfs" {$ResourceLink = "dbs/$DataBaseId/colls/$CollectionId"}
"users" {$ResourceLink = "dbs/$DataBaseId"}
#"permissions" {$ResourceLink = "dbs$/DataBaseId/users/{user-name}"}
"offers" {$ResourceLink = ""}
}
switch ($Verb) {
"GET" {
$contentType = "application/json";
$queryUri = $EndPoint + "$ResourceLink/$ResourceType"
}
"POST" {
$contentType = "application/json";
$queryUri = $EndPoint + "$ResourceLink/$ResourceType"
}
#$ResourceLink += "/$ResourceType"}
"PUT" {
$ResourceLink += "/$ResourceType/$ResourceName";
$contentType = "application/query+json";
$queryUri = $EndPoint + $ResourceLink
}
"DELETE" {
$ResourceLink += "/$ResourceType/$ResourceName";
$contentType = "application/query+json";
$queryUri = $EndPoint + $ResourceLink
}
}
if ($ResourceName -and ($verb -eq "GET")) {
$queryUri += "/$ResourceName"
if ($ResourceLink -ne "") {
$ResourceLink += "/"
}
$ResourceLink += "$ResourceType/$ResourceName"
}
# Delete starting "/"
if ($ResourceLink -match "^/") {
$ResourceLink = $ResourceLink -replace "^/"
}
$authHeader = Generate-MasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink `
-resourceType $ResourceType -key $MasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime
$header = @{authorization = $authHeader; "x-ms-version" = "2017-02-22"; "x-ms-date" = $dateTime; }
if ($OfferThroughput) {
$header += @{'x-ms-offer-throughput' = $OfferThroughput}
}
Write-Verbose "> Headers: $($header | Out-String)"
# Generate Invoke-RESTMethod arguments
$RESTParam = @{'-Method' = $Verb; '-Headers' = $header}
$RESTParam.Add("-Uri", $queryUri)
if ($Query) {
if ($QueryType -eq "HashTable") {
Write-Verbose("Query is HashTable. Converting...")
$queryJson = $Query | ConvertTo-Json
Write-Verbose "> Query: $($queryJson | Out-String)"
}
else {
$queryJson = $Query
Write-Verbose("Query is in JSON format.")
}
$RESTParam.Add("-Body", $queryJson)
}
$result = Invoke-RestMethod @RESTParam -Verbose -Debug
return $result | ConvertTo-Json -Depth 100
}