-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocumentDB.psm1
More file actions
156 lines (145 loc) · 5.74 KB
/
DocumentDB.psm1
File metadata and controls
156 lines (145 loc) · 5.74 KB
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
function Get-DocDBKey([System.String]$Verb = '',[System.String]$ResourceId = '',
[System.String]$ResourceType = '',[System.String]$Date = '',[System.String]$masterKey = '') {
Add-Type -AssemblyName System.Web
$keyBytes = [System.Convert]::FromBase64String($masterKey)
$text = @($Verb.ToLowerInvariant() + "`n" + $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + $Date.ToLowerInvariant() + "`n" + "" + "`n")
$body =[Text.Encoding]::UTF8.GetBytes($text)
$hmacsha = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes)
$hash = $hmacsha.ComputeHash($body)
$signature = [System.Convert]::ToBase64String($hash)
return [System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature))
}
function Get-UTCDate() {
$date = $(Get-Date).ToUniversalTime()
return $date.ToString("r", [System.Globalization.CultureInfo]::InvariantCulture)
}
function Get-DocDBDatabases([string]$accountName, [string]$key) {
$BaseUri = "https://" + $accountName + ".documents.azure.com"
$uri = $BaseUri + "/dbs"
$headers = New-DocDBHeader -resType dbs -key $key
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
return $response.Databases
}
function Get-DocDBCollections([string]$DBName, [string]$accountName, [string]$key){
$BaseUri = "https://" + $accountName + ".documents.azure.com"
$uri = $BaseUri + "/" + "dbs/" + $DBName + "/colls"
$headers = New-DocDBHeader -resType colls -resourceId $("dbs/" + $DBName) -key $key
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
return $response.DocumentCollections
}
function New-DocDBHeader([string]$action = "get",[string]$resType, [string]$resourceId, [String]$key) {
$apiDate = Get-UTCDate
$auth = Get-DocDBKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $Key
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("x-ms-date", $apiDate)
$headers.Add("Authorization", $auth)
$headers.Add("x-ms-version", '2015-12-16')
return $headers
}
#Post json query
function New-DocDBQuery([switch]$NoClean, [string]$JSONQuery, [string]$DBName, [string]$collection, [string]$accountName, [string]$key){
$BaseUri = "https://" + $accountName + ".documents.azure.com"
$collName = "dbs/"+$DBName+"/colls/" + $collection
$DBName = "dbs/" + $databaseName
$headers = New-DocDBHeader -action Post -resType docs -resourceId $collName -key $key
$headers.Add("x-ms-documentdb-is-upsert", "true")
$uri = $BaseUri + "/" + $collName + "/docs"
Write-host ("Calling " + $uri)
try
{
$JSONQuery | ConvertFrom-Json|out-null
}
catch
{
Throw "Problem with JSON input"
break
}
try
{
$response = Invoke-RestMethod $uri -Method Post -Body $JSONQuery -ContentType 'application/query+json' -Headers $headers
}
catch
{
Throw $_.Exception.Message
break
}
if($NoClean)
{
return $response
}
else
{
return Get-CleanDocDBResponse -DocDBOutput $response
}
}
#Post Document
function Set-DocDBDocument{
[CmdletBinding(DefaultParameterSetName='JSON')]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'JSON')]
[string]$JSONdocument,
[string]$DBName,
[string]$collection,
[string]$accountName,
[string]$key,
[Parameter(Mandatory = $true, ParameterSetName = 'PSO')]
[PSCustomObject]$PSdocument
)
if($PSdocument)
{
$document = $PSdocument|ConvertTo-Json
}
elseif($JSONDocument)
{
try
{
$JSONDocument | ConvertFrom-Json|out-null
}
catch
{
Throw "Problem with JSON input"
break
}
$document = $JSONDocument
}
$BaseUri = "https://" + $accountName + ".documents.azure.com"
$collName = "dbs/"+$DBName+"/colls/" + $collection
$DBName = "dbs/" + $databaseName
$headers = New-DocDBHeader -action Post -resType docs -resourceId $collName -key $key
$headers.Add("x-ms-documentdb-is-upsert", "true")
$uri = $BaseUri + "/" + $collName + "/docs"
Write-host ("Calling " + $uri)
$response = Invoke-RestMethod $uri -Method Post -Body $document -ContentType 'application/json' -Headers $headers
return $response
}
function Get-CleanDocDBResponse($DocDBOutput){
if($DocDBOutput.Documents -ne $null)
{
$CleanResponse = $DocDBOutput.Documents|Select-Object * -ExcludeProperty _*
return $CleanResponse
}
else
{
$ErrorActionPreference = 'SilentlyContinue'
$DocDBOutput|ConvertFrom-Json -ErrorVariable converterr -ErrorAction SilentlyContinue
if($converterr -like "*Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys*")
{
$DuplicateKey = [regex]::Match($converterr,"\'(.*?)\'").captures.groups[1].value
$NewDocDBOutput = $DocDBOutput -replace($DuplicateKey,$($DuplicateKey))
if($NewDocDBOutput -ne $null)
{
return $($NewDocDBOutput|convertfrom-json).Documents|Select-Object * -ExcludeProperty _*
}
else
{
$ErrorActionPreference = 'Stop'
Write-Error "Problem converting data to PSObject please use -NoClean argument."
}
}
else
{
$ErrorActionPreference = 'Stop'
Write-Error "Problem converting data to PSObject please use -NoClean argument."
}
}
}