public async deployCosmosDBResource()

in extensions/azurePublish/src/node/azureResourceManager/azureResourceManager.ts [615:740]


  public async deployCosmosDBResource(config: CosmosDBConfig) {
    try {
      this.logger({
        status: BotProjectDeployLoggerType.PROVISION_INFO,
        message: 'Deploying Cosmos DB Resource ...',
      });

      // Create DB accounts
      const cosmosDBManagementClient = new CosmosDBManagementClient(this.creds, this.subscriptionId, this.options);
      const dbAccountDeployResult = await cosmosDBManagementClient.databaseAccounts.createOrUpdate(
        config.resourceGroupName,
        config.name,
        {
          location: config.location,
          locations: [
            {
              locationName: config.location,
              failoverPriority: 0,
            },
          ],
        }
      );

      if (dbAccountDeployResult._response.status >= 300 || dbAccountDeployResult.provisioningState != 'Succeeded') {
        this.logger({
          status: BotProjectDeployLoggerType.PROVISION_ERROR,
          message: dbAccountDeployResult._response.bodyAsText,
        });
        throw createCustomizeError(ProvisionErrors.CREATE_COSMOSDB_ERROR, dbAccountDeployResult._response.bodyAsText);
      }

      // Create DB
      const dbDeployResult = await cosmosDBManagementClient.sqlResources.createUpdateSqlDatabase(
        config.resourceGroupName,
        config.name,
        config.databaseName,
        {
          resource: {
            id: config.databaseName,
          },
          options: {},
        }
      );

      if (dbDeployResult._response.status >= 300) {
        this.logger({
          status: BotProjectDeployLoggerType.PROVISION_ERROR,
          message: dbDeployResult._response.bodyAsText,
        });
        throw createCustomizeError(ProvisionErrors.CREATE_COSMOSDB_ERROR, dbDeployResult._response.bodyAsText);
      }

      // Create Container
      const containerCreateResult = await cosmosDBManagementClient.sqlResources.createUpdateSqlContainer(
        config.resourceGroupName,
        config.name,
        config.databaseName,
        config.containerName,
        {
          resource: {
            id: config.containerName,
            indexingPolicy: {
              indexingMode: 'Consistent',
              automatic: true,
              includedPaths: [
                {
                  path: '/*',
                },
              ],
              excludedPaths: [
                {
                  path: '/"_etag"/?',
                },
              ],
            },
            partitionKey: {
              paths: ['/id'],
              kind: 'Hash',
            },
            conflictResolutionPolicy: {
              mode: 'LastWriterWins',
              conflictResolutionPath: '/_ts',
            },
          },
          options: {},
        }
      );

      if (containerCreateResult._response.status >= 300) {
        this.logger({
          status: BotProjectDeployLoggerType.PROVISION_ERROR,
          message: containerCreateResult._response.bodyAsText,
        });
        throw createCustomizeError(ProvisionErrors.CREATE_COSMOSDB_ERROR, containerCreateResult._response.bodyAsText);
      }

      const authKeyResult = await cosmosDBManagementClient.databaseAccounts.listKeys(
        config.resourceGroupName,
        config.name
      );
      if (authKeyResult._response.status >= 300) {
        this.logger({
          status: BotProjectDeployLoggerType.PROVISION_ERROR,
          message: authKeyResult._response.bodyAsText,
        });
        throw createCustomizeError(ProvisionErrors.CREATE_COSMOSDB_ERROR, authKeyResult._response.bodyAsText);
      }

      const authKey = authKeyResult.primaryMasterKey;
      const cosmosDBEndpoint = dbAccountDeployResult.documentEndpoint;

      return {
        authKey,
        cosmosDBEndpoint,
        databaseId: config.databaseName,
        containerId: config.containerName,
        collectionId: 'botstate-collection',
      };
    } catch (err) {
      this.logger({
        status: BotProjectDeployLoggerType.PROVISION_ERROR,
        message: JSON.stringify(err, Object.getOwnPropertyNames(err)),
      });
      throw createCustomizeError(ProvisionErrors.CREATE_COSMOSDB_ERROR, stringifyError(err));
    }
  }