@@ -127,7 +127,7 @@ Function Get-AuthorizationHeader([string]$ResourceGroup, [string]$SubscriptionId
127
127
Function Get-CommonHeaders ([string ]$now , [string ]$encodedAuthString , [string ]$contentType = " application/json" , [bool ]$isQuery = $false , [string ]$PartitionKey = $null )
128
128
{
129
129
$headers = @ {
130
- " x-ms-date" = $now ;
130
+ " x-ms-date" = $now ;
131
131
" x-ms-version" = $API_VERSION ;
132
132
" Authorization" = $encodedAuthString ;
133
133
" Cache-Control" = " No-Cache" ;
@@ -136,12 +136,12 @@ Function Get-CommonHeaders([string]$now, [string]$encodedAuthString, [string]$co
136
136
137
137
if ($isQuery )
138
138
{
139
- $headers [ " x-ms-documentdb-isquery" ] = " true"
139
+ $headers [" x-ms-documentdb-isquery" ] = " true"
140
140
}
141
141
142
142
if ($PartitionKey )
143
143
{
144
- $headers [" x-ms-documentdb-partitionkey" ] = " [`" $requestPartitionKey `" ]"
144
+ $headers [" x-ms-documentdb-partitionkey" ] = " [`" $PartitionKey `" ]"
145
145
}
146
146
147
147
$headers
@@ -204,7 +204,7 @@ Function Invoke-CosmosDbApiRequestWithContinuation([string]$verb, [string]$url,
204
204
}
205
205
}
206
206
207
- Function Get-PartitionKeyRanges
207
+ Function Get-PartitionKeyRangesOrError
208
208
(
209
209
[parameter (Mandatory = $true )][string ]$ResourceGroup ,
210
210
[parameter (Mandatory = $true )][string ]$Database ,
@@ -213,33 +213,40 @@ Function Get-PartitionKeyRanges
213
213
[parameter (Mandatory = $true )][string ]$SubscriptionId
214
214
)
215
215
{
216
- $baseUrl = Get-BaseDatabaseUrl $Database
217
- $collectionsUrl = Get-CollectionsUrl $Container $Collection
218
- $pkRangeUrl = " $collectionsUrl /$PARTITIONKEYRANGE_TYPE "
216
+ try
217
+ {
218
+ $baseUrl = Get-BaseDatabaseUrl $Database
219
+ $collectionsUrl = Get-CollectionsUrl $Container $Collection
220
+ $pkRangeUrl = " $collectionsUrl /$PARTITIONKEYRANGE_TYPE "
219
221
220
- $url = " $baseUrl /$pkRangeUrl "
222
+ $url = " $baseUrl /$pkRangeUrl "
221
223
222
- $cacheKey = $url
223
- $cacheResult = Get-CacheValue - Key $cacheKey - Cache $PARTITION_KEY_RANGE_CACHE
224
- if ($cacheResult )
225
- {
226
- return $cacheResult
227
- }
224
+ $cacheKey = $url
225
+ $cacheResult = Get-CacheValue - Key $cacheKey - Cache $PARTITION_KEY_RANGE_CACHE
226
+ if ($cacheResult )
227
+ {
228
+ return $cacheResult
229
+ }
228
230
229
- $now = Get-Time
231
+ $now = Get-Time
230
232
231
- $encodedAuthString = Get-AuthorizationHeader - ResourceGroup $ResourceGroup - SubscriptionId $SubscriptionId - Database $Database - verb $GET_VERB - resourceType $PARTITIONKEYRANGE_TYPE - resourceUrl $collectionsUrl - now $now
233
+ $encodedAuthString = Get-AuthorizationHeader - ResourceGroup $ResourceGroup - SubscriptionId $SubscriptionId - Database $Database - verb $GET_VERB - resourceType $PARTITIONKEYRANGE_TYPE - resourceUrl $collectionsUrl - now $now
232
234
233
- $headers = Get-CommonHeaders - now $now - encodedAuthString $encodedAuthString - PartitionKey $requestPartitionKey
234
- $headers [" x-ms-documentdb-query-enablecrosspartition" ] = " true"
235
+ $headers = Get-CommonHeaders - now $now - encodedAuthString $encodedAuthString - PartitionKey $requestPartitionKey
236
+ $headers [" x-ms-documentdb-query-enablecrosspartition" ] = " true"
235
237
236
- $response = Invoke-CosmosDbApiRequest - Verb $GET_VERB - Url $url - Headers $headers | Get-CosmosDbRecordContent
238
+ $response = Invoke-CosmosDbApiRequest - Verb $GET_VERB - Url $url - Headers $headers | Get-CosmosDbRecordContent
237
239
238
- $ranges = $response.partitionKeyRanges
240
+ $ranges = $response.partitionKeyRanges
239
241
240
- Set-CacheValue - Key $cacheKey - Value $ranges - Cache $PARTITION_KEY_RANGE_CACHE - ExpirationHours 6
242
+ Set-CacheValue - Key $cacheKey - Value $ranges - Cache $PARTITION_KEY_RANGE_CACHE - ExpirationHours 6
241
243
242
- $ranges
244
+ $ranges
245
+ }
246
+ catch [System.Net.WebException ]
247
+ {
248
+ $_.Exception
249
+ }
243
250
}
244
251
245
252
Function Get-FilteredPartitionKeyRangesForQuery ($allRanges , $queryRanges )
@@ -537,10 +544,21 @@ Function Search-CosmosDbRecords(
537
544
}
538
545
}
539
546
540
- Function Search-CosmosDbRecordsWithExtraFeatures ([string ]$ResourceGroup , [string ]$Database , [string ]$Container , [string ]$Collection , [string ]$Query , $Parameters , [string ]$SubscriptionId )
547
+ Function Search-CosmosDbRecordsWithExtraFeatures
548
+ (
549
+ [string ]$ResourceGroup ,
550
+ [string ]$Database ,
551
+ [string ]$Container ,
552
+ [string ]$Collection ,
553
+ [string ]$Query ,
554
+ $Parameters ,
555
+ [string ]$SubscriptionId
556
+ )
541
557
{
542
558
begin
543
559
{
560
+ $Parameters = @ (Get-QueryParametersAsNameValuePairs $Parameters )
561
+
544
562
$baseUrl = Get-BaseDatabaseUrl $Database
545
563
$collectionsUrl = Get-CollectionsUrl $Container $Collection
546
564
$docsUrl = " $collectionsUrl /$DOCS_TYPE "
@@ -551,12 +569,17 @@ Function Search-CosmosDbRecordsWithExtraFeatures([string]$ResourceGroup, [string
551
569
552
570
$encodedAuthString = Get-AuthorizationHeader - ResourceGroup $ResourceGroup - SubscriptionId $SubscriptionId - Database $Database - verb $POST_VERB - resourceType $DOCS_TYPE - resourceUrl $collectionsUrl - now $now
553
571
554
- $allPartitionKeyRanges = Get-PartitionKeyRanges - ResourceGroup $ResourceGroup - Database $Database - Container $Container - Collection $Collection - SubscriptionId $SubscriptionId
572
+ $allPartitionKeyRangesOrError = Get-PartitionKeyRangesOrError - ResourceGroup $ResourceGroup - Database $Database - Container $Container - Collection $Collection - SubscriptionId $SubscriptionId
555
573
}
556
574
process
557
575
{
558
576
try
559
577
{
578
+ if ($allPartitionKeyRangesOrError -is [System.Net.WebException ])
579
+ {
580
+ throw $allPartitionKeyRangesOrError
581
+ }
582
+
560
583
$body = @ {
561
584
query = $Query ;
562
585
parameters = $Parameters ;
@@ -571,32 +594,32 @@ Function Search-CosmosDbRecordsWithExtraFeatures([string]$ResourceGroup, [string
571
594
" x-ms-cosmos-is-query-plan-request" = " True" ;
572
595
}
573
596
574
- $response = Invoke-CosmosDbApiRequest - verb $POST_VERB - url $url - Body $body - Headers $headers | Get-CosmosDbRecordContent
597
+ $queryPlan = Invoke-CosmosDbApiRequest - verb $POST_VERB - url $url - Body $body - Headers $headers | Get-CosmosDbRecordContent
575
598
576
- $rewrittenQuery = $response .QueryInfo.RewrittenQuery
599
+ $rewrittenQuery = $queryPlan .QueryInfo.RewrittenQuery
577
600
$searchQuery = if ($rewrittenQuery ) { $rewrittenQuery } else { $Query };
578
601
602
+ $body = @ {
603
+ query = $searchQuery ;
604
+ parameters = $Parameters ;
605
+ }
606
+
579
607
$headers.Remove (" x-ms-cosmos-is-query-plan-request" )
580
608
581
609
$partitionKeyRanges =
582
610
if ($env: COSMOS_DB_FLAG_ENABLE_PARTITION_KEY_RANGE_SEARCHES -eq 1 )
583
611
{
584
- Get-FilteredPartitionKeyRangesForQuery - AllRanges $partitionKeyRanges - QueryRanges $response .QueryRanges
612
+ Get-FilteredPartitionKeyRangesForQuery - AllRanges $allPartitionKeyRangesOrError - QueryRanges $queryPlan .QueryRanges
585
613
}
586
614
else
587
615
{
588
- $allPartitionKeyRanges
616
+ $allPartitionKeyRangesOrError
589
617
}
590
618
591
619
foreach ($partitionKeyRange in $partitionKeyRanges )
592
620
{
593
621
$headers [" x-ms-documentdb-partitionkeyrangeid" ] = $partitionKeyRange.id
594
622
595
- $body = @ {
596
- query = $searchQuery ;
597
- parameters = $Parameters ;
598
- }
599
-
600
623
Invoke-CosmosDbApiRequestWithContinuation - verb $POST_VERB - url $url - Body $body - Headers $headers
601
624
}
602
625
}
@@ -888,11 +911,6 @@ Function Get-CosmosDbRecordContent([parameter(ValueFromPipeline)]$RecordResponse
888
911
}
889
912
}
890
913
891
- Function Use-CosmosDbFiddlerDebugging ()
892
- {
893
- $env: AZURE_CLI_DISABLE_CONNECTION_VERIFICATION = 1
894
- }
895
-
896
914
Function Use-CosmosDbInternalFlag
897
915
(
898
916
$enableFiddlerDebugging = $null ,
0 commit comments