in src/iotHubResourceExplorer.ts [32:138]
public async createIoTHub(outputChannel: vscode.OutputChannel = this._outputChannel, subscriptionId?: string, resourceGroupName?: string): Promise<IotHubModels.IotHubDescription> {
TelemetryClient.sendEvent(Constants.IoTHubAICreateStartEvent);
if (!(await this.waitForLogin())) {
return;
}
const subscriptionItem = await this.getOrSelectSubscriptionItem(outputChannel, subscriptionId);
if (!subscriptionItem) {
return;
}
if (!resourceGroupName) {
const resourceGroupItem = await this.getOrCreateResourceGroup(subscriptionItem);
if (!resourceGroupItem) {
return;
}
resourceGroupName = resourceGroupItem.resourceGroup.name;
outputChannel.show();
outputChannel.appendLine(`Resource Group selected: ${resourceGroupName}`);
}
const locationItem = await vscode.window.showQuickPick(
this.getLocationItems(subscriptionItem),
{ placeHolder: "Select a location to create your IoT Hub in...", ignoreFocusOut: true },
);
if (!locationItem) {
return;
}
outputChannel.show();
outputChannel.appendLine(`Location selected: ${locationItem.label}`);
const skuMap = {
"S1: Standard tier": "S1",
"S2: Standard tier": "S2",
"S3: Standard tier": "S3",
"B1: Basic tier": "B1",
"B2: Basic tier": "B2",
"B3: Basic tier": "B3",
"F1: Free tier": "F1",
};
const sku = await vscode.window.showQuickPick(
Object.keys(skuMap),
{ placeHolder: "Select pricing and scale tier for your IoT Hub...", ignoreFocusOut: true },
);
if (!sku) {
return;
}
outputChannel.appendLine(`Pricing and scale tier selected: ${sku}`);
const name = await this.getIoTHubName(subscriptionItem);
if (!name) {
return;
}
return vscode.window.withProgress({
title: `Creating IoT Hub '${name}'`,
location: vscode.ProgressLocation.Notification,
}, async (progress) => {
outputChannel.appendLine(`Creating IoT Hub: ${name}`);
const intervalID = setInterval(() => {
outputChannel.append(".");
}, 1000);
const { session, subscription } = subscriptionItem;
const client = createAzureClient({
credentials: session.credentials2,
subscriptionId: subscription.subscriptionId,
environment: session.environment
}, IotHubClient);
const iotHubCreateParams = {
location: locationItem.location.name,
subscriptionid: subscription.subscriptionId,
resourcegroup: resourceGroupName,
sku:
{
name: skuMap[sku],
capacity: 1,
},
};
return client.iotHubResource.createOrUpdate(resourceGroupName, name, iotHubCreateParams)
.then(async (iotHubDescription) => {
clearInterval(intervalID);
outputChannel.appendLine("");
const newIotHubConnectionString = await this.updateAndStoreIoTHubInfo(subscriptionItem.session.credentials2,
subscriptionItem.subscription.subscriptionId, subscriptionItem.session.environment , iotHubDescription);
outputChannel.appendLine(`IoT Hub '${name}' is created.`);
(iotHubDescription as any).iotHubConnectionString = newIotHubConnectionString;
TelemetryClient.sendEvent(Constants.IoTHubAICreateDoneEvent, { Result: "Success" }, newIotHubConnectionString);
return iotHubDescription;
})
.catch((err) => {
clearInterval(intervalID);
outputChannel.appendLine("");
let errorMessage: string;
if (err.message) {
errorMessage = err.message;
} else if (err.body && err.body.message) {
errorMessage = err.body.message;
} else {
errorMessage = "Error occurred when creating IoT Hub.";
}
outputChannel.appendLine(errorMessage);
TelemetryClient.sendEvent(Constants.IoTHubAICreateDoneEvent, { Result: "Fail", [Constants.errorProperties.Message]: errorMessage });
return Promise.reject(err);
});
});
}