/// <summary> /// Initialize a SQL backed Cosmos Graph Client. /// If database or container do not exist under the account, the <paramref name="createOptions"/> must be provided and include required parameters to create the database and container. /// If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception. /// If database and container exist under the account, <paramref name="createOptions"/> will be ignored if passed in. /// </summary> /// <param name="accountName">Name of the Cosmos account to connect to. (i.e [yourAccount] from -> https://yourAccount.documents.azure.com:443/)</param> /// <param name="key">Account Key from the Key blade in the portal</param> /// <param name="databaseId">Id of the Database to connect to.</param> /// <param name="containerId">Id of the Container to connect to.</param> /// <param name="createOptions">Speficies the options for creating a new database and contianer if need be (throughput, partitionKey, indexing strategy, TTL etc..)</param> /// <exception cref="Exception">If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.</exception> /// <returns>Reference to a Graph CosmosClient</returns> public static async Task <ICosmosClientGraph> GetClientWithSql(string accountName, string key, string databaseId, string containerId, bool isCloud = true, CreateOptions createOptions = null) { var sqlClient = (EdSmartCosmosClientSql)(await EdSmartCosmosClientSql.GetByAccountName(accountName, key, databaseId, containerId, isCloud, createOptions)); var gremlinEndpoint = isCloud ? string.Format(EdSmartCosmosClientGraph.GraphEndpointFormat, accountName) : accountName; var gremlinPort = isCloud ? 443 : 8901; var server = new GremlinServer(gremlinEndpoint, gremlinPort, username: "******" + databaseId + "/colls/" + containerId, enableSsl: isCloud, password: key); return(new EdSmartCosmosClientGraph { GremlinServer = server, CosmosSqlClient = sqlClient, CosmosSerializer = sqlClient.CosmosSerializer, }); }
private static async Task <ICosmosClientSql> GetCosmosDbClientInternal(CosmosClient client, string databaseId, string containerId, CreateOptions createOptions = null) { Database database = null; Container container = null; if (createOptions != null) { database = await client.CreateDatabaseIfNotExistsAsync(databaseId, createOptions.DatabaseThrouhput); container = await database.CreateContainerIfNotExistsAsync(createOptions.Container, createOptions.ContainerThroughput); } else { database = client.GetDatabase(databaseId); var ensureDbExists = await database.ReadAsync(); if (ensureDbExists.StatusCode == System.Net.HttpStatusCode.NotFound) { throw new Exception($"Database '{databaseId}' not found. Use forceCreate:true if you want the database to be created for you."); } container = database.GetContainer(containerId); var ensureExists = await container.ReadContainerAsync(); if (ensureExists.StatusCode == System.Net.HttpStatusCode.NotFound) { throw new Exception($"Container '{containerId}' not found. Use forceCreate:true if you want a collection to be created for you."); } } var res = new EdSmartCosmosClientSql { Client = client, Database = database, Container = container }; var r = await container.ReadContainerAsync(); res._partitionKeyPropertyName = r.Resource.PartitionKeyPath.Trim('/'); res.CosmosSerializer = new CosmosEntitySerializer(res._partitionKeyPropertyName); return(res); }