Esempio n. 1
0
        private static async Task <OfferV2> GetDatabaseOfferAsync(this IDocumentClient documentClient, string databaseName)
        {
            var databaseUri = UriFactory.CreateDatabaseUri(databaseName);
            var database    = (await documentClient.ReadDatabaseAsync(databaseUri)).Resource;

            return(await documentClient.GetOfferFromSelfLinkAsync(database.SelfLink));
        }
        private async Task <ResourceResponse <Database> > CreateDatabaseIfNotExists(string databaseName)
        {
            ResourceResponse <Database> result;

            // Check to verify a database with the id=FamilyDB does not exist
            try
            {
                result = await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName)).ConfigureAwait(false);

                Logger.Info("Found {0}", databaseName);
            }
            catch (DocumentClientException de)
            {
                // If the database does not exist, create a new database
                if (de.StatusCode == HttpStatusCode.NotFound)
                {
                    result = await _documentClient.CreateDatabaseAsync(new Database { Id = databaseName }).ConfigureAwait(false);

                    Logger.Info("Created {0}", databaseName);
                }
                else
                {
                    throw;
                }
            }

            return(result);
        }
        private async Task EnsureDataStoreCreated(string dbName, string collName)
        {
            if (_cosmosDatabase == null)
            {
                try
                {
                    var response = await _client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(dbName));

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new DataStoreException($"Unable to find CosmosDB database: {dbName}");
                    }

                    var collectionResponse = await _client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(dbName, collName));

                    if (collectionResponse.StatusCode != HttpStatusCode.OK)
                    {
                        throw new DataStoreException($"Unable to find CosmosDB collection: {collName}");
                    }
                    _cosmosDatabase = response.Resource;
                }
                catch (DocumentClientException e)
                {
                    if (e.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new DataStoreException($"CosmosDB database {dbName} not found. Verify deployment of CosmosDB database");
                    }
                    throw;
                }
            }
        }
        /// <summary>
        /// Creates a Database if it does not exist. This functionality is defined in DocumentClient, but not IDocumentClient
        /// </summary>
        /// <param name="documentClient">The document client</param>
        /// <param name="database">The database to create</param>
        /// <param name="options">The request options</param>
        /// <returns>The result</returns>
        public static async Task <ResourceResponse <Database> > CreateDatabaseIfNotExistsAsync(this IDocumentClient documentClient, Database database, RequestOptions options = null)
        {
            var databaseUri = UriFactory.CreateDatabaseUri(database.Id);

            return(await CreateIfNotExists(
                       () => documentClient.ReadDatabaseAsync(databaseUri, options),
                       () => documentClient.CreateDatabaseAsync(database, options)));
        }
Esempio n. 5
0
        public async Task EnsureDbSetupAsync()
        {
            await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_databaseName));

            foreach (var collectionName in _collectionNames)
            {
                await _documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_databaseName, collectionName));
            }
        }
 private async Task CreateDatabaseIfNotExistsAsync()
 {
     await CreateResourceIfNotExistsAsync
     (
         () => _client.ReadDatabaseAsync(DatabaseUri),
         () => _client.CreateDatabaseAsync(new Database {
         Id = DatabaseId
     })
     );
 }
Esempio n. 7
0
 private static async Task CreateDatabaseIfNotExistsAsync(this IDocumentClient client, string databaseId)
 {
     try {
         await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
     }
     catch (DocumentClientException e) {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await client.CreateDatabaseAsync(new Database { Id = databaseId });
         }
         else
         {
             throw;
         }
     }
 }
Esempio n. 8
0
 private async Task CreateDatabaseIfNotExistsAsync()
 {
     try
     {
         await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_applicationConfig.Database));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await _documentClient.CreateDatabaseAsync(new Database { Id = _applicationConfig.Database });
         }
         else
         {
             throw;
         }
     }
 }
 internal async Task CreateDatabaseIfNotExistsAsync()
 {
     try
     {
         await _client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await _client.CreateDatabaseAsync(new Database { Id = DatabaseId });
         }
         else
         {
             throw;
         }
     }
 }
Esempio n. 10
0
 private async Task CreateDatabaseIfNotExistsAsync()
 {
     try
     {
         await documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(cosmosDbConnection.DatabaseId)).ConfigureAwait(false);
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == HttpStatusCode.NotFound)
         {
             await documentClient.CreateDatabaseAsync(new Database { Id = cosmosDbConnection.DatabaseId }).ConfigureAwait(false);
         }
         else
         {
             throw;
         }
     }
 }
 private async Task CreateDatabaseIfNotExistsAsync()
 {
     try
     {
         await documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == HttpStatusCode.NotFound)
         {
             await documentClient.CreateDatabaseAsync(new Database { Id = databaseId });
         }
         else
         {
             throw;
         }
     }
 }
 private async Task CreateDatabaseIfNotExists()
 {
     try
     {
         await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_databaseName));
     }
     catch (DocumentClientException e)
     {
         //Database do not exists! Create it
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await _documentClient.CreateDatabaseAsync(new Database { Id = _databaseName });
         }
         else
         {
             throw;
         }
     }
 }
Esempio n. 13
0
        private static async Task <Database> CreateDatabaseIfNotExistsAsync(this IDocumentClient client, Database database)
        {
            try
            {
                database = (await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(database.Id))).Resource;
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    database = (await client.CreateDatabaseAsync(database)).Resource;
                }
                else
                {
                    throw;
                }
            }

            return(database);
        }
Esempio n. 14
0
        public static async Task <Database> GetOrCreateDatabaseAsync(this IDocumentClient client, string databaseId)
        {
            try
            {
                var response = await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));

                return(response.Resource);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    var response = await client.CreateDatabaseAsync(new Database { Id = databaseId });

                    return(response.Resource);
                }
                else
                {
                    throw e.InnerException;
                }
            }
        }
Esempio n. 15
0
        private async Task CreateDatabaseIfNotExistsAsync()
        {
            try
            {
                await client.ReadDatabaseAsync(databaseLink, options);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode != HttpStatusCode.NotFound)
                {
                    logger.Error("Error while getting DocumentDb database", () => new { e });
                    throw;
                }

                await CreateDatabaseAsync();
            }
            catch (Exception e)
            {
                logger.Error("Error while getting DocumentDb database", () => new { e });
                throw;
            }
        }
Esempio n. 16
0
        public static async Task Initialize(IDocumentDbClientFactory documentDbClientFactory)
        {
            DatabaseId   = ConfigurationManager.AppSettings.GetRefValue <string>(DatabaseIdConfigName);
            CollectionId = ConfigurationManager.AppSettings.GetRefValue <string>(CollectionNameConfigName);

            IDocumentClient client = documentDbClientFactory.GetClient();

            try
            {
                await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
                }
            }

            try
            {
                await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    var documentCollection = new DocumentCollection
                    {
                        Id             = CollectionId,
                        IndexingPolicy = CollectionIndexingPolicy,
                    };

                    await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(DatabaseId), documentCollection, CollectionRequestOptions);
                }
            }
        }
Esempio n. 17
0
 private Database GetDatabase(IDocumentClient client)
 {
     return(client.ReadDatabaseAsync(
                UriFactory.CreateDatabaseUri(_databaseName))
            .Result.Resource);
 }
Esempio n. 18
0
        private static async Task <bool> IsSharedThroughputEnabledAsync(IDocumentClient client)
        {
            Uri databaseUri = UriFactory.CreateDatabaseUri("TablesDB");

            try
            {
                return((await client.CreateOfferQuery($"SELECT * FROM offers o WHERE o.resource = '{(await client.ReadDatabaseAsync(databaseUri)).Resource.SelfLink}'")
                        .AsDocumentQuery().ExecuteNextAsync <Offer>()).AsEnumerable().SingleOrDefault() != null);
            }
            // catch (NotFoundException)
            // {
            //  return false;
            // }
            catch (DocumentClientException ex2)
            {
                if (ex2.StatusCode.HasValue && ex2.StatusCode.Value.Equals(HttpStatusCode.NotFound))
                {
                    return(false);
                }
                throw;
            }
            catch (Exception ex)
            {
                if (ex.GetType().Name == "NotFoundException")
                {
                    return(false);
                }
                throw;
            }
        }