-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathContainerAppListStep.ts
More file actions
121 lines (99 loc) · 5.21 KB
/
Copy pathContainerAppListStep.ts
File metadata and controls
121 lines (99 loc) · 5.21 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
119
120
121
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type ContainerApp, type ContainerAppsAPIClient } from "@azure/arm-appcontainers";
import { LocationListStep, parseAzureResourceId, ResourceGroupListStep, uiUtils } from "@microsoft/vscode-azext-azureutils";
import { AzureWizardPromptStep, nonNullProp, nonNullValueAndProp, type AzureWizardExecuteStep, type ConfirmationViewProperty, type IAzureQuickPickItem, type IWizardOptions } from "@microsoft/vscode-azext-utils";
import { containerAppProvider, containerAppResourceType } from "../../constants";
import { ContainerAppItem } from "../../tree/ContainerAppItem";
import { createContainerAppsAPIClient } from "../../utils/azureClients";
import { localize } from "../../utils/localize";
import { ContainerAppUpdateStep } from "../image/imageSource/ContainerAppUpdateStep";
import { type ContainerAppCreateContext } from "./ContainerAppCreateContext";
import { ContainerAppCreateStep } from "./ContainerAppCreateStep";
import { ContainerAppNameStep } from "./ContainerAppNameStep";
export type ContainerAppListStepOptions = {
/**
* For existing container apps, automatically add subwizard steps to update
*/
updateIfExists?: boolean;
/**
* Provide a custom strategy for updating the list of container app picks.
* Can be used to inject custom sorting, grouping, filtering, etc.
*/
pickUpdateStrategy?: ContainerAppPickUpdateStrategy;
};
export interface ContainerAppPickUpdateStrategy {
updatePicks(context: Partial<ContainerAppCreateContext>, picks: IAzureQuickPickItem<ContainerApp>[]): void | Promise<void>;
}
export class ContainerAppListStep<T extends ContainerAppCreateContext> extends AzureWizardPromptStep<T> {
constructor(readonly options: ContainerAppListStepOptions = {}) {
super();
}
public async prompt(context: T): Promise<void> {
const containerAppPicks: IAzureQuickPickItem<ContainerApp>[] = await this.getPicks(context);
const picks: IAzureQuickPickItem<ContainerApp | undefined>[] = await this.options.pickUpdateStrategy?.updatePicks(context, containerAppPicks) ?? containerAppPicks;
picks.unshift({
label: localize('newContainerApp', '$(plus) Create new container app'),
data: undefined,
});
const containerApp: ContainerApp | undefined = (await context.ui.showQuickPick(picks, {
placeHolder: localize('selectContainerApp', 'Select a container app'),
suppressPersistence: true,
})).data;
if (containerApp) {
context.containerApp = ContainerAppItem.CreateContainerAppModel(containerApp);
}
}
public shouldPrompt(context: T): boolean {
return !context.containerApp && !context.newContainerAppName;
}
public confirmationViewProperty(context: T): ConfirmationViewProperty {
return {
name: localize('containerApp', 'Container App'),
value: nonNullValueAndProp(context.containerApp, 'name'),
contextPropertyName: 'containerApp',
}
}
private async getPicks(context: T): Promise<IAzureQuickPickItem<ContainerApp>[]> {
const client: ContainerAppsAPIClient = await createContainerAppsAPIClient(context);
let containerApps: ContainerApp[] = [];
if (context.resourceGroup) {
containerApps = await uiUtils.listAllIterator(client.containerApps.listByResourceGroup(nonNullProp(context.resourceGroup, 'name')));
} else if (context.newResourceGroupName) {
containerApps = [];
} else {
containerApps = await uiUtils.listAllIterator(client.containerApps.listBySubscription());
}
if (context.managedEnvironment) {
containerApps = containerApps.filter(ca => ca.managedEnvironmentId === context.managedEnvironment?.id);
}
return containerApps.map(ca => {
return {
label: nonNullProp(ca, 'name'),
description: parseAzureResourceId(nonNullProp(ca, 'id')).resourceGroup,
data: ca,
};
});
}
public async getSubWizard(context: T): Promise<IWizardOptions<T> | undefined> {
const promptSteps: AzureWizardPromptStep<T>[] = [];
const executeSteps: AzureWizardExecuteStep<T>[] = [];
// Create
if (!context.containerApp) {
promptSteps.push(new ContainerAppNameStep());
LocationListStep.addProviderForFiltering(context, containerAppProvider, containerAppResourceType);
if (!context.resourceGroup) {
promptSteps.push(new ResourceGroupListStep());
}
LocationListStep.addStep(context, promptSteps);
executeSteps.push(new ContainerAppCreateStep());
}
// Update
if (context.containerApp && this.options.updateIfExists) {
executeSteps.push(new ContainerAppUpdateStep());
}
return { promptSteps, executeSteps };
}
}