|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { type ResourceGroup } from "@azure/arm-resources"; |
| 7 | +import { LocationListStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils"; |
| 8 | +import { AzureWizard, createSubscriptionContext, nonNullProp, nonNullValue, type IActionContext, type ISubscriptionActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils"; |
| 9 | +import { ImageSource } from "../../constants"; |
| 10 | +import { type ContainerAppItem } from "../../tree/ContainerAppItem"; |
| 11 | +import { createActivityContext } from "../../utils/activityUtils"; |
| 12 | +import { isAzdExtensionInstalled } from "../../utils/azdUtils"; |
| 13 | +import { getManagedEnvironmentFromContainerApp } from "../../utils/getResourceUtils"; |
| 14 | +import { getVerifyProvidersStep } from "../../utils/getVerifyProvidersStep"; |
| 15 | +import { localize } from "../../utils/localize"; |
| 16 | +import { pickContainerApp } from "../../utils/pickItem/pickContainerApp"; |
| 17 | +import { deployWorkspaceProject } from "../deployWorkspaceProject/deployWorkspaceProject"; |
| 18 | +import { ContainerAppUpdateStep } from "../image/imageSource/ContainerAppUpdateStep"; |
| 19 | +import { ImageSourceListStep } from "../image/imageSource/ImageSourceListStep"; |
| 20 | +import { type ContainerAppDeployContext } from "./ContainerAppDeployContext"; |
| 21 | + |
| 22 | +export async function deployContainerApp(context: IActionContext, node?: ContainerAppItem): Promise<void> { |
| 23 | + const item: ContainerAppItem = node ?? await pickContainerApp(context); |
| 24 | + const subscriptionContext: ISubscriptionContext = createSubscriptionContext(item.subscription); |
| 25 | + const subscriptionActionContext: ISubscriptionActionContext = { ...context, ...subscriptionContext }; |
| 26 | + |
| 27 | + // Prompt for image source before initializing the wizard in case we need to redirect the call to 'deployWorkspaceProject' instead |
| 28 | + const imageSource: ImageSource = await promptImageSource(subscriptionActionContext); |
| 29 | + if (imageSource === ImageSource.RemoteAcrBuild) { |
| 30 | + await deployWorkspaceProject(context, item); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const wizardContext: ContainerAppDeployContext = { |
| 35 | + ...subscriptionActionContext, |
| 36 | + ...await createActivityContext(true), |
| 37 | + subscription: item.subscription, |
| 38 | + containerApp: item.containerApp, |
| 39 | + managedEnvironment: await getManagedEnvironmentFromContainerApp(subscriptionActionContext, item.containerApp), |
| 40 | + imageSource, |
| 41 | + }; |
| 42 | + |
| 43 | + if (isAzdExtensionInstalled()) { |
| 44 | + wizardContext.telemetry.properties.isAzdExtensionInstalled = 'true'; |
| 45 | + } |
| 46 | + |
| 47 | + const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(wizardContext); |
| 48 | + wizardContext.resourceGroup = nonNullValue( |
| 49 | + resourceGroups.find(rg => rg.name === item.containerApp.resourceGroup), |
| 50 | + localize('containerAppResourceGroup', 'Expected to find the container app\'s resource group.'), |
| 51 | + ); |
| 52 | + |
| 53 | + await LocationListStep.setLocation(wizardContext, item.containerApp.location); |
| 54 | + |
| 55 | + const wizard: AzureWizard<ContainerAppDeployContext> = new AzureWizard(wizardContext, { |
| 56 | + title: localize('deployContainerAppTitle', 'Deploy image to container app'), |
| 57 | + promptSteps: [ |
| 58 | + new ImageSourceListStep(), |
| 59 | + ], |
| 60 | + executeSteps: [ |
| 61 | + getVerifyProvidersStep<ContainerAppDeployContext>(), |
| 62 | + new ContainerAppUpdateStep(), |
| 63 | + ], |
| 64 | + showLoadingPrompt: true |
| 65 | + }); |
| 66 | + |
| 67 | + await wizard.prompt(); |
| 68 | + wizardContext.activityTitle = localize('deployContainerAppActivityTitle', 'Deploy image to container app "{0}"', wizardContext.containerApp?.name); |
| 69 | + await wizard.execute(); |
| 70 | +} |
| 71 | + |
| 72 | +async function promptImageSource(context: ISubscriptionActionContext): Promise<ImageSource> { |
| 73 | + const promptContext: ISubscriptionActionContext & { imageSource?: ImageSource } = context; |
| 74 | + |
| 75 | + const imageSourceStep: ImageSourceListStep = new ImageSourceListStep(); |
| 76 | + await imageSourceStep.prompt(promptContext); |
| 77 | + |
| 78 | + return nonNullProp(promptContext, 'imageSource'); |
| 79 | +} |
0 commit comments