Support passing subscriptionId and resourceGroupName to createIoTHub API (#157)

* Support passing subscriptionId and resourceGroupName to createIoTHub API

* Pass subscriptionId to selectIoTHub API

* Update public api

* Use subscriptions instead of filters
This commit is contained in:
Jun Han 2018-09-10 12:56:01 +08:00 коммит произвёл GitHub
Родитель 8972e48ecd
Коммит 3fd8bae2f5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 83 добавлений и 44 удалений

36
src/azure-account.api.d.ts поставляемый
Просмотреть файл

@ -1,10 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Event } from 'vscode';
import { Event, Terminal, Progress, CancellationToken } from 'vscode';
import { ServiceClientCredentials } from 'ms-rest';
import { AzureEnvironment } from 'ms-rest-azure';
import { SubscriptionModels } from 'azure-arm-resource';
import { ReadStream } from 'fs';
export type AzureLoginStatus = 'Initializing' | 'LoggingIn' | 'LoggedIn' | 'LoggedOut';
@ -14,9 +17,13 @@ export interface AzureAccount {
readonly waitForLogin: () => Promise<boolean>;
readonly sessions: AzureSession[];
readonly onSessionsChanged: Event<void>;
readonly subscriptions: AzureSubscription[];
readonly onSubscriptionsChanged: Event<void>;
readonly waitForSubscriptions: () => Promise<boolean>;
readonly filters: AzureResourceFilter[];
readonly onFiltersChanged: Event<void>;
readonly waitForFilters: () => Promise<boolean>;
createCloudShell(os: 'Linux' | 'Windows'): CloudShell;
}
export interface AzureSession {
@ -26,13 +33,26 @@ export interface AzureSession {
readonly credentials: ServiceClientCredentials;
}
export interface AzureResourceFilter {
export interface AzureSubscription {
readonly session: AzureSession;
readonly subscription: SubscriptionModels.Subscription;
}
export interface Credentials {
readSecret(service: string, account: string): Thenable<string | undefined>;
writeSecret(service: string, account: string, secret: string): Thenable<void>;
deleteSecret(service: string, account: string): Thenable<boolean>;
export type AzureResourceFilter = AzureSubscription;
export type CloudShellStatus = 'Connecting' | 'Connected' | 'Disconnected';
export interface UploadOptions {
contentLength?: number;
progress?: Progress<{ message?: string; increment?: number }>;
token?: CancellationToken;
}
export interface CloudShell {
readonly status: CloudShellStatus;
readonly onStatusChanged: Event<CloudShellStatus>;
readonly waitForConnection: () => Promise<boolean>;
readonly terminal: Promise<Terminal>;
readonly session: Promise<AzureSession>;
readonly uploadFile: (filename: string, stream: ReadStream, options?: UploadOptions) => Promise<void>;
}

Просмотреть файл

@ -97,12 +97,12 @@ export class AzureIoTExplorer {
this._iotHubDeviceTwinExplorer.updateDeviceTwin();
}
public async createIoTHub(outputChannel?: vscode.OutputChannel) {
return this._iotHubResourceExplorer.createIoTHub(outputChannel);
public async createIoTHub(outputChannel?: vscode.OutputChannel, subscriptionId?: string, resourceGroupName?: string) {
return this._iotHubResourceExplorer.createIoTHub(outputChannel, subscriptionId, resourceGroupName);
}
public selectIoTHub(outputChannel?: vscode.OutputChannel) {
return this._iotHubResourceExplorer.selectIoTHub(outputChannel);
public selectIoTHub(outputChannel?: vscode.OutputChannel, subscriptionId?: string) {
return this._iotHubResourceExplorer.selectIoTHub(outputChannel, subscriptionId);
}
public copyIoTHubConnectionString(): void {

Просмотреть файл

@ -26,27 +26,26 @@ export class IoTHubResourceExplorer extends BaseExplorer {
this.accountApi = vscode.extensions.getExtension<AzureAccount>("ms-vscode.azure-account")!.exports;
}
public async createIoTHub(outputChannel: vscode.OutputChannel = this._outputChannel): Promise<IotHubDescription> {
public async createIoTHub(outputChannel: vscode.OutputChannel = this._outputChannel, subscriptionId?: string, resourceGroupName?: string): Promise<IotHubDescription> {
TelemetryClient.sendEvent(Constants.IoTHubAICreateStartEvent);
if (!(await this.waitForLogin())) {
return;
}
const subscriptionItem = await vscode.window.showQuickPick(
this.loadSubscriptionItems(this.accountApi),
{ placeHolder: "Select a subscription to create your IoT Hub in...", ignoreFocusOut: true },
);
const subscriptionItem = await this.getOrSelectSubscriptionItem(outputChannel, subscriptionId);
if (!subscriptionItem) {
return;
}
outputChannel.show();
outputChannel.appendLine(`Subscription selected: ${subscriptionItem.label}`);
const resourceGroupItem = await this.getOrCreateResourceGroup(subscriptionItem);
if (!resourceGroupItem) {
return;
if (!resourceGroupName) {
const resourceGroupItem = await this.getOrCreateResourceGroup(subscriptionItem);
if (!resourceGroupItem) {
return;
}
resourceGroupName = resourceGroupItem.resourceGroup.name;
outputChannel.show();
outputChannel.appendLine(`Resource Group selected: ${resourceGroupName}`);
}
outputChannel.appendLine(`Resource Group selected: ${resourceGroupItem.label}`);
const locationItem = await vscode.window.showQuickPick(
this.getLocationItems(subscriptionItem),
@ -55,6 +54,7 @@ export class IoTHubResourceExplorer extends BaseExplorer {
if (!locationItem) {
return;
}
outputChannel.show();
outputChannel.appendLine(`Location selected: ${locationItem.label}`);
const skuMap = {
@ -94,14 +94,14 @@ export class IoTHubResourceExplorer extends BaseExplorer {
const iotHubCreateParams = {
location: locationItem.location.name,
subscriptionid: subscription.subscriptionId,
resourcegroup: resourceGroupItem.resourceGroup.name,
resourcegroup: resourceGroupName,
sku:
{
name: skuMap[sku],
capacity: 1,
},
{
name: skuMap[sku],
capacity: 1,
},
};
return client.iotHubResource.createOrUpdate(resourceGroupItem.resourceGroup.name, name, iotHubCreateParams)
return client.iotHubResource.createOrUpdate(resourceGroupName, name, iotHubCreateParams)
.then(async (iotHubDescription) => {
clearInterval(intervalID);
outputChannel.appendLine("");
@ -130,27 +130,26 @@ export class IoTHubResourceExplorer extends BaseExplorer {
});
}
public async selectIoTHub(outputChannel: vscode.OutputChannel = this._outputChannel): Promise<IotHubDescription> {
public async selectIoTHub(outputChannel: vscode.OutputChannel = this._outputChannel, subscriptionId?: string): Promise<IotHubDescription> {
TelemetryClient.sendEvent("General.Select.IoTHub.Start");
if (!(await this.waitForLogin())) {
return;
}
TelemetryClient.sendEvent("General.Select.Subscription.Start");
const subscriptionItems = this.loadSubscriptionItems(this.accountApi);
const subscriptionItem = await vscode.window.showQuickPick(subscriptionItems, { placeHolder: "Select Subscription", ignoreFocusOut: true });
if (subscriptionItem) {
TelemetryClient.sendEvent("General.Select.Subscription.Done");
const subscriptionItem = await this.getOrSelectSubscriptionItem(outputChannel, subscriptionId);
if (!subscriptionItem) {
return;
}
TelemetryClient.sendEvent("General.Select.Subscription.Done");
const iotHubItem = await this.selectIoTHubItem(subscriptionItem);
if (iotHubItem) {
outputChannel.show();
outputChannel.appendLine(`Subscription selected: ${subscriptionItem.label}`);
const iotHubItem = await this.selectIoTHubItem(subscriptionItem);
if (iotHubItem) {
outputChannel.appendLine(`IoT Hub selected: ${iotHubItem.label}`);
const iotHubConnectionString = await this.getIoTHubConnectionString(subscriptionItem, iotHubItem.iotHubDescription);
await this.updateIoTHubConnectionString(iotHubConnectionString);
(iotHubItem.iotHubDescription as any).iotHubConnectionString = iotHubConnectionString;
TelemetryClient.sendEvent("AZ.Select.IoTHub.Done", undefined, iotHubConnectionString);
return iotHubItem.iotHubDescription;
}
outputChannel.appendLine(`IoT Hub selected: ${iotHubItem.label}`);
const iotHubConnectionString = await this.getIoTHubConnectionString(subscriptionItem, iotHubItem.iotHubDescription);
await this.updateIoTHubConnectionString(iotHubConnectionString);
(iotHubItem.iotHubDescription as any).iotHubConnectionString = iotHubConnectionString;
TelemetryClient.sendEvent("AZ.Select.IoTHub.Done", undefined, iotHubConnectionString);
return iotHubItem.iotHubDescription;
}
}
@ -257,6 +256,26 @@ export class IoTHubResourceExplorer extends BaseExplorer {
});
}
private async getOrSelectSubscriptionItem(outputChannel: vscode.OutputChannel, subscriptionId: string): Promise<SubscriptionItem> {
if (subscriptionId) {
let azureSubscription = this.accountApi.subscriptions.find((subscription) => subscription.subscription.subscriptionId === subscriptionId);
if (azureSubscription) {
return new SubscriptionItem(azureSubscription.subscription, azureSubscription.session);
}
} else {
const subscriptionItem = await vscode.window.showQuickPick(
this.loadSubscriptionItems(this.accountApi),
{ placeHolder: "Select Subscription", ignoreFocusOut: true },
);
if (subscriptionItem) {
outputChannel.show();
outputChannel.appendLine(`Subscription selected: ${subscriptionItem.label}`);
return subscriptionItem;
}
}
return undefined;
}
private async getOrCreateResourceGroup(subscriptionItem: SubscriptionItem): Promise<ResourceGroupItem> {
const pick = await vscode.window.showQuickPick(
this.getResourceGroupItems(subscriptionItem),