-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAcrListStep.ts
More file actions
71 lines (59 loc) · 4.08 KB
/
AcrListStep.ts
File metadata and controls
71 lines (59 loc) · 4.08 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { ContainerRegistryManagementClient, Registry } from "@azure/arm-containerregistry";
import { uiUtils } from "@microsoft/vscode-azext-azureutils";
import { AzureWizardPromptStep, IAzureQuickPickItem, ISubscriptionActionContext, IWizardOptions, nonNullProp } from "@microsoft/vscode-azext-utils";
import { acrDomain, currentlyDeployed, quickStartImageName } from "../../../../../constants";
import { createContainerRegistryManagementClient } from "../../../../../utils/azureClients";
import { parseImageName } from "../../../../../utils/imageNameUtils";
import { localize } from "../../../../../utils/localize";
import type { IContainerRegistryImageContext } from "../IContainerRegistryImageContext";
import { getLatestContainerAppImage } from "../getLatestContainerImage";
import { RegistryEnableAdminUserStep } from "./RegistryEnableAdminUserStep";
export class AcrListStep extends AzureWizardPromptStep<IContainerRegistryImageContext> {
public async prompt(context: IContainerRegistryImageContext): Promise<void> {
const placeHolder: string = localize('selectRegistry', 'Select an Azure Container Registry');
context.registry = (await context.ui.showQuickPick(this.getPicks(context), { placeHolder })).data;
}
public shouldPrompt(context: IContainerRegistryImageContext): boolean {
return !context.registry;
}
public async getSubWizard(context: IContainerRegistryImageContext): Promise<IWizardOptions<IContainerRegistryImageContext> | undefined> {
if (!context.registry?.adminUserEnabled) {
return { promptSteps: [new RegistryEnableAdminUserStep()] }
}
return undefined;
}
public async getPicks(context: IContainerRegistryImageContext): Promise<IAzureQuickPickItem<Registry>[]> {
const registries: Registry[] = await AcrListStep.getRegistries(context);
// Try to suggest a registry only when the user is deploying to a Container App
let suggestedRegistry: string | undefined;
let srExists: boolean = false;
if (context.containerApp) {
const { registryDomain, registryName, imageNameReference } = parseImageName(getLatestContainerAppImage(context.containerApp));
// If the image is not the default quickstart image, then we can try to suggest a registry based on the latest Container App image
if (registryDomain === acrDomain && imageNameReference !== quickStartImageName) {
suggestedRegistry = registryName;
}
// Does the suggested registry exist in the list of pulled registries? If so, move it to the front of the list
const srIndex: number = registries.findIndex((r) => !!suggestedRegistry && r.loginServer === suggestedRegistry);
srExists = srIndex !== -1;
if (srExists) {
const sr: Registry = registries.splice(srIndex, 1)[0];
registries.unshift(sr);
}
}
// Preferring 'suppressPersistence: true' over 'priority: highest' to avoid the possibility of a double parenthesis appearing in the description
return registries.map((r) => {
return !!suggestedRegistry && r.loginServer === suggestedRegistry ?
{ label: nonNullProp(r, 'name'), data: r, description: `${r.loginServer} ${currentlyDeployed}`, suppressPersistence: true } :
{ label: nonNullProp(r, 'name'), data: r, description: r.loginServer, suppressPersistence: srExists };
});
}
public static async getRegistries(context: ISubscriptionActionContext): Promise<Registry[]> {
const client: ContainerRegistryManagementClient = await createContainerRegistryManagementClient(context);
return await uiUtils.listAllIterator(client.registries.list());
}
}