Mongo client API with resource token - c#

I have been trying to use a Mongo API cosmos account with multiple databases and wanted to generate resource token for the individual resources. I am seeing the implementation for Document DB like below.
client = new DocumentClient(new Uri(endpointUrl), resourceToken);
However, I am looking for implementation related to Mongo.Driver
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, 10255);
settings.UseSsl = true;
settings.SslSettings = new SslSettings();
settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(tokepass2);
settings.Credential = new MongoCredential("SCRAM-SHA-1", identity, evidence);
MongoClient client = new MongoClient(settings);
I am trying to replace the "tokepass2" with the resource token that is generated. But that is not working and ended up with the exception
One or more errors occurred. (Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.)
I know we have the possibility where we can do a REST based post call with the token in the header, however I am looking for an implementation related to Mongo Client, if some one has implemented.

Unfortunately,i don't think it could be implemented in the C# Mongo DB driver.Based on Wire protocol compatibility :
Azure Cosmos DB implements wire protocols of common NoSQL databases
including Cassandra, MongoDB, Gremlin, and Azure Tables Storage. By
providing a native implementation of the wire protocols directly and
efficiently inside Cosmos DB, it allows existing client SDKs, drivers,
and tools of the NoSQL databases to interact with Cosmos DB
transparently. Cosmos DB does not use any source code of the databases
for providing wire-compatible APIs for any of the NoSQL databases.
By default, new accounts created using Azure Cosmos DB's API for
MongoDB are compatible with version 3.6 of the MongoDB wire protocol.
Any MongoDB client driver that understands this protocol version
should be able to natively connect to Cosmos DB.
Cosmos db mongo api only implements wire protocols for Mongo DB,it doesn't have any specific sdk for mongo db. And other mongo db driver like mongo c# driver or mongoose etc,they built for mongo db,not for cosmos db mongo api. So the resource token feature can't be supported by those drivers directly. You can't replace master key with resource token.
If you do want to use resource token,you could use :
1.REST API as you mentioned in your question
2.Migrate mongo db to cosmos db sql api. Please refer to this link:https://learn.microsoft.com/en-us/azure/cosmos-db/import-data

Related

Accessing DataVerse data and running SQL statements against it from C# code on .NET 6 (Azure Function)

I'm working with Dynamics365 CE in the cloud. I'm trying to run some rather involved queries that I've built up as SQL scripts (using the wonderful "SQL-4-CDS" plugin for the XrmToolBox).
Now I know I can connect to the Dataverse data store through the TDS endpoint (if enabled - it is in my case), and from SSMS, it works just fine:
Server Name = myorg.crm4.dynamics.com,5558
Authentication = Azure Active Directory - Password
User Name = my company e-mail
I can connect to Dataverse, and run my queries - all is great.
Now I'd like to do the same from C# code (running on .NET 6) that I'm writing, that should end up being an Azure Function in the end - so it's a "server-to-server", behind-the-scenes, no interactive login context kind of scenario.
I can connect to Dataverse via the TDS endpoint using this connection string - as long as I'm running the app interactively - as me, in my user context:
Server=myorg.crm4.dynamics.com,5558;Authentication=Active Directory Password;Database=my_dbname;User Id=my_email;Password=my_pwd;
However - this won't work with a server-to-server "daemon"-style setup.
Since I'm using .NET 6 (for the Azure Function), and since I want to run some custom SQL statements, I cannot use the "CRM XRM Client" tooling (with the IOrganizationService classes) - I need to use straight ADO.NET - any idea would I could define an ADO.NET compatible connection string, that would use a Client ID and Client Secret (which I both have at my disposal)?
I've tried a great many values for the Authentication=...... setting - but none have worked so far. Any place I can find a complete list of the supported values for this connection string parameter?
Thanks for any help or pointers!

Is it possible to create a new Gremlin Graph DB and Graph using c# code

I have a Azure Cosmos DB account with Gremlin API. I'm using Gremlin.Net to query the db.
var gremlinServer = new GremlinServer(hostname, port, enableSsl: true, username: "/dbs/" + database + "/colls/" + collectionName, password: authKey);
username parameter takes dbname and collection name.
Just wondering how to create a new graph db and a graph under the account using c# code.
Thanks
I searched the azure gremlin .Net source code , no such methods like creating database or graph could be found. There is an statement mentioned in above linkļ¼š
This sample uses the open-source Gremlin.Net driver to connect to an
Azure Cosmos DB Graph API account and run some basic Create, Read,
Update, Delete Gremlin queries.
It seems that we could only execute gremlin queries, can't CRUD database itself.
I also searched Cosmos DB REST API, no such special api for CRUD cosmos db graph api. Only operations of database and collection could be found. So I tested it with below sample code with Document DB .Net SDK and it works for me.
client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]);
await client.CreateDatabaseAsync(new Database { Id = "db"});
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(DatabaseId),
new DocumentCollection
{
Id = "coll"
},
new RequestOptions { OfferThroughput = 400 });
I know it is strange(there is no collection in cosmos db graph api,it supposed to be graph) but it works for me.You could try it on your side.
Hope it helps you.

Azure - Programmatically add new MySQL db instance

I am trying to programmatically add new MySQL db instance to azure portal.
I looked at this library: https://github.com/Azure/azure-libraries-for-net but i only see an example of how to create a SQL Server and not a MySQL.
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
var sqlServer = azure.SqlServers.Define(sqlServerName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(AdministratorLogin)
.WithAdministratorPassword(AdministratorPassword)
.WithNewFirewallRule(FirewallRuleIPAddress)
.WithNewFirewallRule(FirewallRuleStartIPAddress, FirewallRuleEndIPAddress)
.Create();
var database = sqlServer.Databases
.Define(DatabaseName)
.Create();
Any idea if its supported to programmatically create a MySQL server as well?
Looking at the release notes and SDK code, it seems the ability to manage MySQL databases is still not supported (as of version 1.3).
What you could do is consume the REST API for managing Azure MySQL databses directly. For creation of database, please look at Create Or Update Database operation.
To get started with Azure REST API, you may find this link useful: https://learn.microsoft.com/en-us/rest/api/.

How to set the throughput to 10000 RU for Azure Cosmos DB Mongo API in code

How to set the throughput to 10000 RU for Azure Cosmos DB Mongo API in code? Im using Mongo.Driver for .NET. Want to use something like db.RunCommand to set the throughput to 10.000 RU.
In my opinion, the MongoDB API implements MongoDB functionality, while RU and throughput are concepts in Azure Cosmos DB. Please don't get confused.
So, we have no way to set RU with the Mongo shell command line, which is not supported.
However, you could set RU in the Document DB SDK refer to the official code as follows:
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "coll";
myCollection.PartitionKey.Paths.Add("/deviceId");
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri("db"),
myCollection,
new RequestOptions { OfferThroughput = 3000 });

MongoDB server State is disconnected

I am using MongoDb server installed on VM Ubuntu 14 on Azure, and I use this
tutroial, with last version. I add the port of mongo 27017 too.
And I connect to it directly and add Database with some collections.
I use the mongoDb .Net Driver on VS2015 in C# with version 2.0.1 (using link)
and try to connect to the Mongo Server, but the state of the server is disconnected
var client = new MongoClient("mongodb://name.cloudapp.net:27017");
var state = client.Cluster.Description.State;
MessageBox.Show(state.ToString());
I used it before the same steps and nothing happen, just I don't know where is the problem
Try this please. I think this may work. Just have to add one line to enumerate all databases.
var client = new MongoClient("mongodb://name.cloudapp.net:27017");
var databases = client.ListDatabases();
var state = client.Cluster.Description.State;
MessageBox.Show(state.ToString());
This answer explains better.

Categories