in src/extension/src/azure/azure-cosmosDB/cosmosDbModule.ts [135:255]
public async createCosmosDB(userCosmosDBSelection: CosmosDBSelections, genPath: string): Promise<DatabaseObject> {
/*
* Create Cosmos Client with users credentials and selected subscription *
*/
let userSubscriptionItem: SubscriptionItem;
try {
userSubscriptionItem = userCosmosDBSelection.subscriptionItem;
this.setCosmosClient(userSubscriptionItem);
} catch (error) {
throw new AuthorizationError(error.message);
}
const resourceGroup = userCosmosDBSelection.resourceGroupItem.name;
const databaseName = userCosmosDBSelection.cosmosDBResourceName;
const location = userCosmosDBSelection.location;
const experience = userCosmosDBSelection.cosmosAPI;
const template = JSON.parse(
fs.readFileSync(
path.join(
Controller.vsContext.extensionPath,
"src",
"azure",
"azure-cosmosDB",
"arm-templates",
"template.json"
),
"utf8"
)
);
const parameters = JSON.parse(
fs.readFileSync(
path.join(
Controller.vsContext.extensionPath,
"src",
"azure",
"azure-cosmosDB",
"arm-templates",
"parameters.json"
),
"utf8"
)
);
const definitions: APIdefinition = this.APIdefinitionMap.get(experience)!;
parameters.parameters = {
name: {
value: databaseName,
},
location: {
value: location.split(" ").join("").toLowerCase(),
},
locationName: {
value: location,
},
defaultExperience: {
value: definitions.defaultExperience,
},
capabilities: {
value: definitions.capabilities,
},
kind: {
value: definitions.kind,
},
};
const deploymentParams = parameters.parameters;
const options: ResourceManagementModels.Deployment = {
properties: {
mode: "Incremental",
parameters: deploymentParams,
template: template,
},
};
try {
if (this.SubscriptionItemCosmosClient === undefined) {
throw new AuthorizationError(MESSAGES.ERRORS.COSMOS_CLIENT_NOT_DEFINED);
}
const azureResourceClient: ResourceManagementClient = new ResourceManager().getResourceManagementClient(
userSubscriptionItem
);
ARMFileHelper.createDirIfNonExistent(path.join(genPath, "arm-templates"));
ARMFileHelper.writeObjectToJsonFile(path.join(genPath, "arm-templates", "cosmos-template.json"), template);
ARMFileHelper.writeObjectToJsonFile(path.join(genPath, "arm-templates", "cosmos-parameters.json"), parameters);
/*
* Cosmos Client to generate a cosmos DB resource using resource group name, database name, and options *
*/
await azureResourceClient.deployments.createOrUpdate(
resourceGroup,
databaseName + COSMOS_DEPLOYMENT_SUFFIX,
options
);
const databaseAccount: CosmosDBManagementModels.DatabaseAccountGetResults =
await this.SubscriptionItemCosmosClient.databaseAccounts.get(resourceGroup, databaseName);
const connectionString = await this.getConnectionString(
this.SubscriptionItemCosmosClient,
resourceGroup,
databaseName
);
/*
* Returning a tuple which includes databaseAccount from callback and its connection string
*/
const db: DatabaseObject = { databaseAccount, connectionString };
return db;
} catch (error) {
throw new DeploymentError(error.message);
}
}