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
156 stars 46 forks source link

Add support for Cosmos DB emulator #39

Closed fmonroe closed 6 years ago

fmonroe commented 6 years ago

I tried 'localhost', 'localhost:8081' and other patterns. As per Tweet chat with Dan, this is not yet supported. Dan: "This doesn’t work because internally the URI to the CosmosDB is built by combining the $account with ‘.documents.azure.com’. However, there should be a simple work around you could use (until I add built in support): After creating the $connection variable, set the BaseUri property to ‘http://localhost:8081’. E.g. $connection.BaseUri = [uri] ‘http://localhost:8081’. I’m assuming here the emulator uses HTTP rather than HTTPS. What I’ll do is add a way to set the base URI through a parameter when creating the connection object." Thanks, great tool (a must IMHO).

PlagueHO commented 6 years ago

Hi @fmonroe - thank you very much for logging this! I'm getting onto this today and should have a new version out to the PS Gallery by the end of the day. Thanks again!

PlagueHO commented 6 years ago

Hi @fmonroe - quick update. I've made the preliminary changes and confirmed everything is working. What I did was added a new parameter set to the New-CosmosDbConnection function so that you can do this:

$conn = New-CosmosDbConnection -Emulator -Database 'testDatabase'
New-CosmosDbUser -Connection $conn -Id 'frank'

image

You don't need to specify an account name or master key when using an emulator so it is quite a lot easier to do.

I'll finish up the changes and update the documentation a bit later on today.

fmonroe commented 6 years ago

This is great!

“You don't need to specify an account name or master key when using an emulator”

-- Good design decision (IMHO)

I’m playing with https://hackolade.com/ to reverse engineer my SQL database. (they DO require to enter the key for the emulator, which I think is too cumbersome 😊 ).

Thanks for your work

From: Daniel Scott-Raynsfordmailto:notifications@github.com Sent: Friday, January 26, 2018 3:19 PM To: PlagueHO/CosmosDBmailto:CosmosDB@noreply.github.com Cc: Frank Monroemailto:frank.monroe@outlook.com; Mentionmailto:mention@noreply.github.com Subject: Re: [PlagueHO/CosmosDB] Add support for Cosmos DB emulator (#39)

Hi @fmonroehttps://github.com/fmonroe - quick update. I've made the preliminary changes and confirmed everything is working. What I did was added a new parameter set to the New-CosmosDbConnection function so that you can do this:

$conn = New-CosmosDbConnection -Emulator -Database 'testDatabase'

New-CosmosDbUser -Connection $conn -Id 'frank'

[image]https://user-images.githubusercontent.com/7589164/35458889-004dfc4e-0343-11e8-9472-398f46e841ae.png

You don't need to specify an account name or master key when using an emulator so it is quite a lot easier to do.

I'll finish up the changes and update the documentation a bit later on today.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PlagueHO/CosmosDB/issues/39#issuecomment-360893854, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AMkiRZG8NIGK5Z67yIYl9Bb7rBc7XH5Vks5tOjNLgaJpZM4RubN9.

PlagueHO commented 6 years ago

Hi @fmonroe - I've finished and released the new version: https://www.powershellgallery.com/packages/CosmosDB/2.0.1.173

You can see an example of usage here: https://github.com/PlagueHO/CosmosDB#create-a-connection-to-a-cosmosdb-emulator

fmonroe commented 6 years ago

Hello Daniel, Thanks for your work.

I have a problem:

This code:

Import-Module CosmosDB Get-Module CosmosDB $Server = 'localhost' try { if ($Server -eq 'localhost') { $Connection = New-CosmosDbConnection -Emulator Write-Host "Connected!!!"

} else {
    $Connection = New-CosmosDbConnection -Account $Server -Key $MasterKey
    $MasterKey = $null
}

} catch { $e = "Could not connect to Cosmos server '$Server'.`n" + $_.Exception.Message Show-Error $e }

Write-Host "Connected to Cosmos server $Server." write-Host $Connection write-Host $Connection.ToString()

… gives me an error:

ModuleType Version Name ExportedCommands


Script 2.0.1.173 CosmosDB {ConvertTo-CosmosDbTokenDateString, Get-CosmosDbAttachment, Get-CosmosDbAttachmentResourcePath, Get-CosmosD... Connected! Connected to Cosmos server localhost.

You cannot call a method on a null-valued expression. At D:\frank\Repos\Untitled4.ps1:21 char:5

WoW! I cannot see the problem. There seems to be something related to a try/catch with the Cosmos emulator (I am in Powershell ISE). (note: the Cosmos emulator is running, and there is no problem when there is no try/catch) I am very confused. Am I doing something wrong ?

Name Value


PSVersion 5.1.16299.98 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.16299.98 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

Thanks for your help.

Frank

PS. I just noticed that the problem is only in the ISE! It looks like it’s working when executing directly in the shell. Did you know that some scripts work in the shell but not in the ISE? I did not!

From: Daniel Scott-Raynsfordmailto:notifications@github.com Sent: Friday, January 26, 2018 10:42 PM To: PlagueHO/CosmosDBmailto:CosmosDB@noreply.github.com Cc: Frank Monroemailto:frank.monroe@outlook.com; Mentionmailto:mention@noreply.github.com Subject: Re: [PlagueHO/CosmosDB] Add support for Cosmos DB emulator (#39)

Hi @fmonroehttps://github.com/fmonroe - I've finished and released the new version: https://www.powershellgallery.com/packages/CosmosDB/2.0.1.173

You can see an example of usage here: https://github.com/PlagueHO/CosmosDB#create-a-connection-to-a-cosmosdb-emulator

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PlagueHO/CosmosDB/issues/39#issuecomment-360957058, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AMkiRXRbgtrjMuE-IOczBQCcw9Nxema5ks5tOpsGgaJpZM4RubN9.

PlagueHO commented 6 years ago

Hi @fmonroe - that is really odd! I haven't seen this before. I gave your script a try in ISE and it looks like it worked as expected: image

But if I didn't know better I'd think that the $connection variable is being scoped to the try { } block. But I know PowerShell shouldn't actually behave this way. So I'm scratching my head.

One thing you could try (and I'd not recommend this for a production script) is make the $Connection variable scoped global and see if that solves the problem. E.g.

Import-Module CosmosDB
Get-Module CosmosDB
$Server = 'localhost'
try {
if ($Server -eq 'localhost') {
$Global:Connection = New-CosmosDbConnection -Emulator
Write-Host "Connected!!!"

} else {
$Global:Connection = New-CosmosDbConnection -Account $Server -Key $MasterKey
$MasterKey = $null
}
}
catch {
$e = "Could not connect to Cosmos server '$Server'.`n" + $_.Exception.Message
Show-Error $e
}

Write-Host "Connected to Cosmos server $Server."
write-Host $Global:Connection
write-Host $Global:Connection.ToString()

If that resolves the issue then I think we know that there is some strange scope behavior going on in the ISE!

fmonroe commented 6 years ago

Holly kangaroo! I think I got it!

2 issues:

  1. In my script I had a function to create an object in a CosmosDB database. My function took on parameters and created the Cosmos object. My function was called New-Object (duh!). I renamed it to New-CosmosObject and that fixed the problem.
  2. With the former name (New-Object), the line $Connection = New-CosmosDbConnection -Emulator always gives an error: even after the ‘New-Object’ function has been deleted or commented and the script is restarted! (That is why I still had the error with my minimal script but you did not). That makes it quite difficult to find the issue by deleting/commenting lines one by one until you get the error. The error is stucked until you exit the ISE and reload it and then reload the script.

For a while I though I was drunk, but no I can still hold my beer and do some coding 😊

Thanks for your help. Thanks for your module, I am using it to script objects in my Cosmos DB.

Best regards,

Frank Monroe

From: Daniel Scott-Raynsfordmailto:notifications@github.com Sent: Friday, February 2, 2018 2:43 AM To: PlagueHO/CosmosDBmailto:CosmosDB@noreply.github.com Cc: Frank Monroemailto:frank.monroe@outlook.com; Mentionmailto:mention@noreply.github.com Subject: Re: [PlagueHO/CosmosDB] Add support for Cosmos DB emulator (#39)

Hi @fmonroehttps://github.com/fmonroe - that is really odd! I haven't seen this before. I gave your script a try in ISE and it looks like it worked as expected: [image]https://user-images.githubusercontent.com/7589164/35721838-de0eb68a-0858-11e8-8b0a-fe258cd04e64.png

But if I didn't know better I'd think that the $connection variable is being scoped to the try { } block. But I know PowerShell shouldn't actually behave this way. So I'm scratching my head.

One thing you could try (and I'd not recommend this for a production script) is make the $Connection variable scoped global and see if that solves the problem. E.g.

Import-Module CosmosDB

Get-Module CosmosDB

$Server = 'localhost'

try {

if ($Server -eq 'localhost') {

$Global:Connection = New-CosmosDbConnection -Emulator

Write-Host "Connected!!!"

} else {

$Global:Connection = New-CosmosDbConnection -Account $Server -Key $MasterKey

$MasterKey = $null

}

}

catch {

$e = "Could not connect to Cosmos server '$Server'.`n" + $_.Exception.Message

Show-Error $e

}

Write-Host "Connected to Cosmos server $Server."

write-Host $Global:Connection

write-Host $Global:Connection.ToString()

If that resolves the issue then I think we know that there is some strange scope behavior going on in the ISE!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PlagueHO/CosmosDB/issues/39#issuecomment-362510357, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AMkiRZwrm0iflpvqNoJAoKTpyAKw67K-ks5tQrysgaJpZM4RubN9.

PlagueHO commented 6 years ago

Hahaha @fmonroe - I completely understand now! I use New-Object to create the connection object in New-CosmosDbConnection - so the standard PS version of New-Object was being replaced. Good catch - and with a beer! :grin:

PlagueHO commented 6 years ago

:beer: