-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDwpManagedEnvironmentListStep.ts
More file actions
118 lines (103 loc) · 5.74 KB
/
DwpManagedEnvironmentListStep.ts
File metadata and controls
118 lines (103 loc) · 5.74 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
109
110
111
112
113
114
115
116
117
118
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type ContainerAppsAPIClient, type ManagedEnvironment } from "@azure/arm-appcontainers";
import { type ResourceGroup } from "@azure/arm-resources";
import { ResourceGroupListStep, getResourceGroupFromId, uiUtils } from "@microsoft/vscode-azext-azureutils";
import { AzureWizardPromptStep, nonNullProp, nonNullValueAndProp, type IAzureQuickPickItem } from "@microsoft/vscode-azext-utils";
import { createContainerAppsAPIClient } from "../../../../utils/azureClients";
import { localize } from "../../../../utils/localize";
import { type DeploymentConfigurationSettings } from "../../settings/DeployWorkspaceProjectSettingsV2";
import { dwpSettingUtilsV2 } from "../../settings/dwpSettingUtilsV2";
import { type DeployWorkspaceProjectInternalContext } from "../DeployWorkspaceProjectInternalContext";
const recommendedPickDescription: string = localize('recommended', '(Recommended)');
export class DwpManagedEnvironmentListStep extends AzureWizardPromptStep<DeployWorkspaceProjectInternalContext> {
public async prompt(context: DeployWorkspaceProjectInternalContext): Promise<void> {
const placeHolder: string = localize('selectManagedEnvironment', 'Select a container apps environment');
const picks: IAzureQuickPickItem<ManagedEnvironment | undefined>[] = await this.getPicks(context);
if (!picks.length) {
// No managed environments to choose from
return;
}
await this.setRecommendedPicks(context, picks);
const pick = await context.ui.showQuickPick(picks, { placeHolder, suppressPersistence: true });
context.telemetry.properties.usedRecommendedEnv = pick.description === recommendedPickDescription ? 'true' : 'false';
context.telemetry.properties.recommendedEnvCount =
String(picks.reduce((count, p) => count + (p.description === recommendedPickDescription ? 1 : 0), 0));
const managedEnvironment: ManagedEnvironment | undefined = pick.data;
if (!managedEnvironment) {
// User is choosing to create a new managed environment
return;
}
await this.setContextWithManagedEnvironmentResources(context, managedEnvironment);
}
public shouldPrompt(context: DeployWorkspaceProjectInternalContext): boolean {
return !context.managedEnvironment;
}
private async getPicks(context: DeployWorkspaceProjectInternalContext): Promise<IAzureQuickPickItem<ManagedEnvironment | undefined>[]> {
const client: ContainerAppsAPIClient = await createContainerAppsAPIClient(context);
const managedEnvironments: ManagedEnvironment[] = await uiUtils.listAllIterator(
context.resourceGroup ?
client.managedEnvironments.listByResourceGroup(nonNullValueAndProp(context.resourceGroup, 'name')) :
client.managedEnvironments.listBySubscription()
);
if (!managedEnvironments.length) {
return [];
}
return [
{
label: localize('newManagedEnvironment', '$(plus) Create new container apps environment'),
data: undefined
},
...managedEnvironments.map(env => {
return {
label: nonNullProp(env, 'name'),
data: env
};
})
];
}
private async setContextWithManagedEnvironmentResources(context: DeployWorkspaceProjectInternalContext, managedEnvironment: ManagedEnvironment): Promise<void> {
const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(context);
context.resourceGroup = resourceGroups.find(rg => rg.name === getResourceGroupFromId(nonNullProp(managedEnvironment, 'id')));
context.managedEnvironment = managedEnvironment;
}
private async setRecommendedPicks(context: DeployWorkspaceProjectInternalContext, picks: IAzureQuickPickItem<ManagedEnvironment | undefined>[]): Promise<void> {
const deploymentConfigurations: DeploymentConfigurationSettings[] | undefined = await dwpSettingUtilsV2.getWorkspaceDeploymentConfigurations(nonNullProp(context, 'rootFolder'));
if (!deploymentConfigurations?.length) {
return;
}
const client = await createContainerAppsAPIClient(context);
for (const config of deploymentConfigurations) {
try {
if (config.resourceGroup && config.containerApp) {
const containerApp = await client.containerApps.get(config.resourceGroup, config.containerApp);
const recommendedPick = picks.find(p => p.data?.id === containerApp.managedEnvironmentId);
if (recommendedPick) {
recommendedPick.description = recommendedPickDescription;
}
}
}
catch (these_hands) {
// ignore the error and continue
}
}
const recommendedPick = (pick: IAzureQuickPickItem<ManagedEnvironment | undefined>): boolean => {
if (pick.description === recommendedPickDescription) {
return true;
}
return false;
};
// sort the picks by recommendation
picks.sort((a, b) => {
if (recommendedPick(a)) {
return -1;
} else if (recommendedPick(b)) {
return 1;
} else {
return 0;
}
});
}
}