PlagueHO / CosmosDB

PowerShell Module for working with Azure Cosmos DB databases, collections, documents, attachments, offers, users, permissions, triggers, stored procedures and user defined functions.
http://dscottraynsford.com
MIT License
154 stars 46 forks source link

New-CosmosDbContext is not returning Token #430

Closed Raag007 closed 3 years ago

Raag007 commented 3 years ago

Hi - when we try to set context it is not returning a token.

Hence we are not able to run the next commands. image

PlagueHO commented 3 years ago

Hi @Raag007 - what is the method you're using to generate the Context - e.g. are you setting the key manually or having it retrieved automatically? I note you're defaulting the Database in the connection to the same name as the account. Do you have a database with this name in the account?

Raag007 commented 3 years ago

Hi @PlagueHO - I have set the context manually by hardcoding the primary key. Here is my code: please let me know if I'm missing anything.

$primaryKey = ConvertTo-SecureString -String 'RsRuCnqDm6V3Ea46yFrjZ9aQZqB9Wb7w4K==' -AsPlainText -Force

$cosmosDbContext = New-CosmosDbContext -Account 's00563ncda00001d2a' -Database 's00563ncdb00001d2a' -Key $primaryKey

0..1 | Foreach-Object {
    $id = $([Guid]::NewGuid().ToString())
    $document = @"
{
    `"id`": `"$id`",
    `"content`": `"Some string`",
    `"more`": `"Some other string`"
}
"@
    New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'delivers' -DocumentBody $document -PartitionKey $id
}

Error:

Invoke-WebRequest: /home/anuraag/.local/share/powershell/Modules/CosmosDB/4.5.0/CosmosDB.psm1:833
Line |
 833 |  …        $requestResult = Invoke-WebRequest @invokeWebRequestParameters
     |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {"code":"BadRequest","message":"Message: {\"Errors\":[\"PartitionKey extracted from document doesn't match the one specified in the header. Learn more:
     | https:\\/\\/aka.ms\\/CosmosDB\\/sql\\/errors\\/wrong-pk-value\"]}\r\nActivityId: 499a3c55-b234-4b2d-a804-c4c9aaa1eb98, Request URI:
     | /apps/9c967a2b-7b0d-4f49-a38d-36f1391fda86/services/4037aa9b-2bd2-4528-bd63-040dac7c36ca/partitions/9f285d7b-d3bf-4690-8835-bf482362ffe2/replicas/132742185342865120p/, RequestStats: \r\nRequestStartTime:
     | 2021-09-02T03:46:44.8416670Z, RequestEndTime: 2021-09-02T03:46:44.8616192Z,  Number of regions attempted:1\r\nResponseTime: 2021-09-02T03:46:44.8616192Z, StoreResult: StorePhysicalAddress:
     | rntbd://cdb-ms-prod-westus2-fd38.documents.azure.com:14370/apps/9c967a2b-7b0d-4f49-a38d-36f1391fda86/services/4037aa9b-2bd2-4528-bd63-040dac7c36ca/partitions/9f285d7b-d3bf-4690-8835-bf482362ffe2/replicas/132742185342865120p/, LSN: 33, GlobalCommittedLsn: 33, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 1001, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#33, UsingLocalLSN: False, TransportException: null, BELatencyMs: 0.466, ActivityId: 499a3c55-b234-4b2d-a804-c4c9aaa1eb98, TransportRequestTimeline: [(Event: Created, StartTime: 2021-09-02T03:46:44.8416670Z, DurationMicroSecs: 0),(Event: ChannelAcquisitionStarted, StartTime: 2021-09-02T03:46:44.8416670Z, DurationMicroSecs: 10008),(Event: Pipelined, StartTime: 2021-09-02T03:46:44.8516757Z, DurationMicroSecs: 0),(Event: Transit Time, StartTime: 2021-09-02T03:46:44.8516757Z, DurationMicroSecs: 0),(Event: Received, StartTime: 2021-09-02T03:46:44.8516757Z, DurationMicroSecs: 0),(Event: Completed, StartTime: 2021-09-02T03:46:44.8516757Z, DurationMicroSecs: 0),], ResourceType: Document, OperationType: Create\r\n, SDK: Microsoft.Azure.Documents.Common/2.14.0"}
Raag007 commented 3 years ago

Here are the resources which are already in place. image

PlagueHO commented 3 years ago

Hi @Raag007 - make sure you've rolled your keys if the above was a real key.

Can you try the operation again? I noticed the same problem you did in some recent test runs, but they seem to have gone away now. I'm wondering if it is an intermittent failure of some kind as I've never encountered it before.

Raag007 commented 3 years ago

Hi @PlagueHO - those are dummy keys :)

I will test it and let you know.

Raag007 commented 3 years ago

Hi @PlagueHO - yes, it is working as expected now. Thank you for your response and time.

PlagueHO commented 3 years ago

No worries - happy to help. Still not sure why the issue occurred, but suspect it was a short-term glitch at the server end.