Exemple #1
0
        public static async Task <ICosmosDBAccount> CreateCosmosAccountIfNotExists(this IAzure azure, ILogger logger, AppyCosmosDbParameters parameters)
        {
            logger.Info($"Creating Cosmos Account with name {parameters.AccountName} in group {parameters.ResourceGroupName}");
            var cosmosResource = await azure.CosmosDBAccounts.GetByResourceGroupAsync(parameters.ResourceGroupName, parameters.AccountName);

            if (!ReferenceEquals(cosmosResource, null))
            {
                logger.Info($"Cosmos Account with name {parameters.AccountName} already exists in group {parameters.ResourceGroupName}");
                return(cosmosResource);
            }

            var cosmosAccount = await azure.CosmosDBAccounts
                                .Define(parameters.AccountName)
                                .WithRegion(parameters.AccountRegion)
                                .WithExistingResourceGroup(parameters.ResourceGroupName)
                                .WithDataModelSql()
                                .WithEventualConsistency()
                                .WithWriteReplication(parameters.WriteReplicationRegion)
                                .WithReadReplication(parameters.ReadReplicationRegion)
                                .CreateAsync();

            logger.Info($"Created Cosmos Account with name {parameters.AccountName} in group {parameters.ResourceGroupName}");

            return(cosmosAccount);
        }
Exemple #2
0
        public static async Task DeleteCosmosDbResourcesIfExists(this IAzure azure, ILogger logger, AppyCosmosDbParameters parameters)
        {
            logger.Info($"Deleting Cosmos Account with name {parameters.AccountName} in group {parameters.ResourceGroupName}");
            var cosmosResource = await azure.CosmosDBAccounts.GetByResourceGroupAsync(parameters.ResourceGroupName, parameters.AccountName);

            if (ReferenceEquals(cosmosResource, null))
            {
                logger.Info($"Cosmos Account with name {parameters.AccountName} not exists in group {parameters.ResourceGroupName}");
                return;
            }

            await azure.CosmosDBAccounts.DeleteByResourceGroupAsync(parameters.ResourceGroupName, parameters.AccountName);

            logger.Info($"Deleted Cosmos Account with name {parameters.AccountName} in group {parameters.ResourceGroupName}");
        }
Exemple #3
0
        public static async Task CreateCosmosResourcesIfNotExists(this IAzure azure, ILogger logger, AppyCosmosDbParameters parameters)
        {
            var cosmosAccount = await azure.CreateCosmosAccountIfNotExists(logger, parameters);

            var cosmosKeys     = cosmosAccount.ListKeys();
            var masterKey      = cosmosKeys.PrimaryMasterKey;
            var endPoint       = cosmosAccount.DocumentEndpoint;
            var documentClient = CreateCosmosDocumentClient(masterKey, endPoint);

            await documentClient.CreateCosmosDatabaseIfNotExists(logger, parameters.DatabaseId);

            foreach (var collectionId in parameters.CollectionIds)
            {
                await documentClient.CreateCosmosDbAndCollectionIfNotExists(logger, parameters.DatabaseId, collectionId);
            }
        }