-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathAppInsightsListStep.ts
More file actions
108 lines (89 loc) · 5.01 KB
/
Copy pathAppInsightsListStep.ts
File metadata and controls
108 lines (89 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { ApplicationInsightsComponent, ApplicationInsightsManagementClient } from "@azure/arm-appinsights";
import { LocationListStep, uiUtils } from "@microsoft/vscode-azext-azureutils";
import { AzureWizardPromptStep, IAzureNamingRules, IAzureQuickPickItem, IAzureQuickPickOptions, IWizardOptions, nonNullProp } from "@microsoft/vscode-azext-utils";
import * as vscode from 'vscode';
import { createAppInsightsClient } from "../utils/azureClients";
import { AppInsightsCreateStep } from "./AppInsightsCreateStep";
import { AppInsightsNameStep } from "./AppInsightsNameStep";
import { IAppServiceWizardContext } from "./IAppServiceWizardContext";
import { LogAnalyticsCreateStep } from "./LogAnalyticsCreateStep";
export const appInsightsNamingRules: IAzureNamingRules = {
minLength: 1,
maxLength: 255,
// eslint-disable-next-line no-useless-escape
invalidCharsRegExp: /[^a-zA-Z0-9\.\_\-\(\)]/
};
const skipForNowLabel: string = '$(clock) Skip for now';
export class AppInsightsListStep extends AzureWizardPromptStep<IAppServiceWizardContext> {
private _suppressCreate: boolean | undefined;
public constructor(suppressCreate?: boolean) {
super();
this._suppressCreate = suppressCreate;
}
public static async getAppInsightsComponents(context: IAppServiceWizardContext): Promise<ApplicationInsightsComponent[]> {
if (context.appInsightsTask === undefined) {
const client: ApplicationInsightsManagementClient = await createAppInsightsClient(context);
context.appInsightsTask = uiUtils.listAllIterator(client.components.list());
}
return await context.appInsightsTask;
}
public static async isNameAvailable(context: IAppServiceWizardContext, name: string): Promise<boolean> {
const components: ApplicationInsightsComponent[] = await AppInsightsListStep.getAppInsightsComponents(context);
return !components.some((ai: ApplicationInsightsComponent) => ai.name?.toLowerCase() === name.toLowerCase());
}
public async prompt(context: IAppServiceWizardContext): Promise<void> {
const options: IAzureQuickPickOptions = { placeHolder: 'Select an Application Insights resource for your app.', id: `AppInsightsListStep/${context.subscriptionId}` };
const input: IAzureQuickPickItem<ApplicationInsightsComponent | undefined> = (await context.ui.showQuickPick(this.getQuickPicks(context), options));
context.appInsightsComponent = input.data;
// as create new and skipForNow both have undefined as the data type, check the label
if (input.label === skipForNowLabel) {
context.telemetry.properties.aiSkipForNow = 'true';
context.appInsightsSkip = true;
} else {
context.telemetry.properties.newAI = String(!context.appInsightsComponent);
}
}
public shouldPrompt(context: IAppServiceWizardContext): boolean {
return !context.appInsightsComponent;
}
public async getSubWizard(context: IAppServiceWizardContext): Promise<IWizardOptions<IAppServiceWizardContext> | undefined> {
if (context.appInsightsComponent) {
context.valuesToMask.push(nonNullProp(context.appInsightsComponent, 'name'));
} else if (!context.appInsightsSkip) {
const promptSteps: AzureWizardPromptStep<IAppServiceWizardContext>[] = [new AppInsightsNameStep()];
LocationListStep.addStep(context, promptSteps);
return Promise.resolve({
promptSteps: promptSteps,
executeSteps: [new LogAnalyticsCreateStep(), new AppInsightsCreateStep()]
});
}
return Promise.resolve(undefined);
}
private async getQuickPicks(context: IAppServiceWizardContext): Promise<IAzureQuickPickItem<ApplicationInsightsComponent | undefined>[]> {
const picks: IAzureQuickPickItem<ApplicationInsightsComponent | undefined>[] = !this._suppressCreate ? [{
label: vscode.l10n.t('$(plus) Create new Application Insights resource'),
data: undefined
}] : [];
picks.push({
label: vscode.l10n.t(skipForNowLabel),
data: undefined
});
let components: ApplicationInsightsComponent[] = await AppInsightsListStep.getAppInsightsComponents(context);
// https://github.com/microsoft/vscode-azurefunctions/issues/1454
if (!Array.isArray(components)) {
components = [];
}
return picks.concat(components.map((ai: ApplicationInsightsComponent) => {
return {
id: ai.id,
label: nonNullProp(ai, 'name'),
description: ai.location,
data: ai
};
}));
}
}