diff --git a/src/commands/createContainerApp/EnableIngressStep.ts b/src/commands/createContainerApp/EnableIngressStep.ts index 7ccb44b3f..823aea816 100644 --- a/src/commands/createContainerApp/EnableIngressStep.ts +++ b/src/commands/createContainerApp/EnableIngressStep.ts @@ -5,9 +5,9 @@ import { AzureWizardPromptStep, IWizardOptions } from "@microsoft/vscode-azext-utils"; import { localize } from "../../utils/localize"; +import { TargetPortInputStep } from "../ingress/editTargetPort/TargetPortInputStep"; import { ICreateContainerAppContext } from "./ICreateContainerAppContext"; import { IngressVisibilityStep } from "./IngressVisibilityStep"; -import { TargetPortStep } from "./TargetPortStep"; export class EnableIngressStep extends AzureWizardPromptStep { public async prompt(context: ICreateContainerAppContext): Promise { @@ -23,7 +23,9 @@ export class EnableIngressStep extends AzureWizardPromptStep | undefined> { if (context.enableIngress) { return { - promptSteps: [new IngressVisibilityStep(), new TargetPortStep()] + // Context types will be temporarily incompatible until all the ingress commands are decoupled from the create command + // @ts-ignore + promptSteps: [new IngressVisibilityStep(), new TargetPortInputStep()] } } diff --git a/src/commands/ingress/IngressContext.ts b/src/commands/ingress/IngressContext.ts new file mode 100644 index 000000000..66b96a2f1 --- /dev/null +++ b/src/commands/ingress/IngressContext.ts @@ -0,0 +1,14 @@ +/*--------------------------------------------------------------------------------------------- +* Copyright (c) Microsoft Corporation. All rights reserved. +* Licensed under the MIT License. See License.txt in the project root for license information. +*--------------------------------------------------------------------------------------------*/ + +import type { ExecuteActivityContext } from "@microsoft/vscode-azext-utils"; +import type { IContainerAppContext } from "../IContainerAppContext"; + +export interface IngressContext extends IContainerAppContext, ExecuteActivityContext { + enableIngress?: boolean; + enableExternal?: boolean; + + targetPort?: number; +} diff --git a/src/commands/ingress/IngressUpdateBaseStep.ts b/src/commands/ingress/IngressUpdateBaseStep.ts new file mode 100644 index 000000000..e4e4d8c24 --- /dev/null +++ b/src/commands/ingress/IngressUpdateBaseStep.ts @@ -0,0 +1,30 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import type { Ingress } from "@azure/arm-appcontainers"; +import { AzureWizardExecuteStep, nonNullProp } from "@microsoft/vscode-azext-utils"; +import type { Progress } from "vscode"; +import { ext } from "../../extensionVariables"; +import type { IContainerAppContext } from "../IContainerAppContext"; +import { updateContainerApp } from "../deployContainerApp/updateContainerApp"; + +type IngressOptions = { + ingress: Ingress | null, + working: string, + workCompleted: string +} + +export abstract class IngressUpdateBaseStep extends AzureWizardExecuteStep { + protected async updateIngressSettings(context: T, progress: Progress<{ message?: string | undefined}>, options: IngressOptions): Promise { + const containerApp = nonNullProp(context, 'containerApp'); + const { ingress, working, workCompleted } = options; + + progress.report({ message: working }); + await updateContainerApp(context, context.subscription, containerApp, { configuration: { ingress: ingress as Ingress | undefined } }); + + ext.outputChannel.appendLog(workCompleted); + ext.state.notifyChildrenChanged(containerApp.managedEnvironmentId); + } +} diff --git a/src/commands/ingress/editTargetPort.ts b/src/commands/ingress/editTargetPort.ts deleted file mode 100644 index 90a1712bc..000000000 --- a/src/commands/ingress/editTargetPort.ts +++ /dev/null @@ -1,41 +0,0 @@ -/*--------------------------------------------------------------------------------------------- -* Copyright (c) Microsoft Corporation. All rights reserved. -* Licensed under the MIT License. See License.txt in the project root for license information. -*--------------------------------------------------------------------------------------------*/ - -import { AzureWizard, AzureWizardPromptStep, createSubscriptionContext, IActionContext, nonNullProp, nonNullValueAndProp } from "@microsoft/vscode-azext-utils"; -import { ContainerAppItem } from "../../tree/ContainerAppItem"; -import { IngressItem } from "../../tree/IngressItem"; -import { localize } from "../../utils/localize"; -import { pickContainerApp } from "../../utils/pickContainerApp"; -import { ICreateContainerAppContext } from "../createContainerApp/ICreateContainerAppContext"; -import { TargetPortStep } from "../createContainerApp/TargetPortStep"; -import { updateIngressSettings } from "./updateIngressSettings"; - -export async function editTargetPort(context: IActionContext, node?: IngressItem): Promise { - const { subscription, containerApp }: ContainerAppItem | IngressItem = node ?? await pickContainerApp(context); - - const title: string = localize('updateTargetPort', 'Update Target Port'); - const promptSteps: AzureWizardPromptStep[] = [new TargetPortStep()]; - - const wizardContext: ICreateContainerAppContext = { - ...context, - ...createSubscriptionContext(subscription), - subscription, - managedEnvironmentId: nonNullProp(containerApp, 'managedEnvironmentId'), - defaultPort: containerApp.configuration?.ingress?.targetPort - }; - - const wizard: AzureWizard = new AzureWizard(wizardContext, { - title, - promptSteps, - executeSteps: [] - }); - - await wizard.prompt(); - const ingress = nonNullValueAndProp(containerApp.configuration, 'ingress'); - ingress.targetPort = wizardContext.targetPort; - const working: string = localize('updatingTargetPort', 'Updating target port to {0}...', ingress.targetPort); - const workCompleted: string = localize('updatedTargetPort', 'Updated target port to {0}', ingress.targetPort); - await updateIngressSettings(context, { ingress, subscription, containerApp, working, workCompleted }); -} diff --git a/src/commands/createContainerApp/TargetPortStep.ts b/src/commands/ingress/editTargetPort/TargetPortInputStep.ts similarity index 75% rename from src/commands/createContainerApp/TargetPortStep.ts rename to src/commands/ingress/editTargetPort/TargetPortInputStep.ts index 29dd1ca5c..91d546972 100644 --- a/src/commands/createContainerApp/TargetPortStep.ts +++ b/src/commands/ingress/editTargetPort/TargetPortInputStep.ts @@ -4,19 +4,20 @@ *--------------------------------------------------------------------------------------------*/ import { AzureWizardPromptStep } from "@microsoft/vscode-azext-utils"; -import { localize } from "../../utils/localize"; -import { ICreateContainerAppContext } from "./ICreateContainerAppContext"; +import { localize } from "../../../utils/localize"; +import type { IngressContext } from "../IngressContext"; +import { getDefaultPort } from "./getDefaultPort"; -export class TargetPortStep extends AzureWizardPromptStep { - public async prompt(context: ICreateContainerAppContext): Promise { +export class TargetPortInputStep extends AzureWizardPromptStep { + public async prompt(context: IngressContext): Promise { context.targetPort = Number(await context.ui.showInputBox({ prompt: localize('targetPort', 'This is the port your container is listening on that will receive traffic. Set this value to the port number that your container uses.'), - value: String(context.defaultPort ?? 80), + value: String(getDefaultPort(context)), validateInput: this.validateInput })); } - public shouldPrompt(context: ICreateContainerAppContext): boolean { + public shouldPrompt(context: IngressContext): boolean { return !context.targetPort; } diff --git a/src/commands/ingress/editTargetPort/TargetPortUpdateStep.ts b/src/commands/ingress/editTargetPort/TargetPortUpdateStep.ts new file mode 100644 index 000000000..4bdfaf194 --- /dev/null +++ b/src/commands/ingress/editTargetPort/TargetPortUpdateStep.ts @@ -0,0 +1,30 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { nonNullProp, nonNullValueAndProp } from "@microsoft/vscode-azext-utils"; +import type { Progress } from "vscode"; +import { localize } from "../../../utils/localize"; +import type { IngressContext } from "../IngressContext"; +import { IngressUpdateBaseStep } from "../IngressUpdateBaseStep"; + +export class TargetPortUpdateStep extends IngressUpdateBaseStep { + public priority: number = 280; + + public async execute(context: IngressContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise { + const containerApp = nonNullProp(context, 'containerApp'); + const ingress = nonNullValueAndProp(containerApp.configuration, 'ingress'); + ingress.targetPort = context.targetPort; + + const working: string = localize('updatingTargetPort', 'Updating target port...'); + const workCompleted: string = localize('updatedTargetPort', 'Updated target port to {0} for container app "{1}"', context.targetPort, containerApp.name); + + context.activityTitle = localize('updateTargetPort', 'Update target port to {0} for container app "{1}"', context.targetPort, containerApp.name); + await this.updateIngressSettings(context, progress, { ingress, working, workCompleted }); + } + + public shouldExecute(): boolean { + return true; + } +} diff --git a/src/commands/ingress/editTargetPort/editTargetPort.ts b/src/commands/ingress/editTargetPort/editTargetPort.ts new file mode 100644 index 000000000..fed253e2b --- /dev/null +++ b/src/commands/ingress/editTargetPort/editTargetPort.ts @@ -0,0 +1,46 @@ +/*--------------------------------------------------------------------------------------------- +* Copyright (c) Microsoft Corporation. All rights reserved. +* Licensed under the MIT License. See License.txt in the project root for license information. +*--------------------------------------------------------------------------------------------*/ + +import { AzureWizard, AzureWizardExecuteStep, AzureWizardPromptStep, createSubscriptionContext, IActionContext } from "@microsoft/vscode-azext-utils"; +import type { ContainerAppItem } from "../../../tree/ContainerAppItem"; +import { IngressItem } from "../../../tree/IngressItem"; +import { createActivityContext } from "../../../utils/activityUtils"; +import { localize } from "../../../utils/localize"; +import { pickContainerApp } from "../../../utils/pickContainerApp"; +import type { IngressContext } from "../IngressContext"; +import { TargetPortInputStep } from "./TargetPortInputStep"; +import { TargetPortUpdateStep } from "./TargetPortUpdateStep"; + +export async function editTargetPort(context: IActionContext, node?: IngressItem): Promise { + const { subscription, containerApp }: ContainerAppItem | IngressItem = node ?? await pickContainerApp(context); + + const wizardContext: IngressContext = { + ...context, + ...createSubscriptionContext(subscription), + ...(await createActivityContext()), + subscription, + containerApp + }; + + const title: string = localize('updateTargetPort', 'Update target port for container app "{0}"', containerApp.name); + + const promptSteps: AzureWizardPromptStep[] = [ + new TargetPortInputStep() + ]; + + const executeSteps: AzureWizardExecuteStep[] = [ + new TargetPortUpdateStep() + ]; + + const wizard: AzureWizard = new AzureWizard(wizardContext, { + title, + promptSteps, + executeSteps, + showLoadingPrompt: true + }); + + await wizard.prompt(); + await wizard.execute(); +} diff --git a/src/commands/ingress/editTargetPort/getDefaultPort.ts b/src/commands/ingress/editTargetPort/getDefaultPort.ts new file mode 100644 index 000000000..34e72285a --- /dev/null +++ b/src/commands/ingress/editTargetPort/getDefaultPort.ts @@ -0,0 +1,10 @@ +/*--------------------------------------------------------------------------------------------- +* Copyright (c) Microsoft Corporation. All rights reserved. +* Licensed under the MIT License. See License.md in the project root for license information. +*--------------------------------------------------------------------------------------------*/ + +import type { IngressContext } from "../IngressContext"; + +export function getDefaultPort(context: IngressContext, fallbackPort: number = 80): number { + return context.containerApp?.configuration?.ingress?.targetPort || fallbackPort; +} diff --git a/src/commands/registerCommands.ts b/src/commands/registerCommands.ts index 7ecab5126..9da491bbe 100644 --- a/src/commands/registerCommands.ts +++ b/src/commands/registerCommands.ts @@ -16,7 +16,7 @@ import { connectToGitHub } from './gitHub/connectToGitHub/connectToGitHub'; import { disconnectRepo } from './gitHub/disconnectRepo/disconnectRepo'; import { openGitHubRepo } from './gitHub/openGitHubRepo'; import { disableIngress } from './ingress/disableIngress'; -import { editTargetPort } from './ingress/editTargetPort'; +import { editTargetPort } from './ingress/editTargetPort/editTargetPort'; import { enableIngress } from './ingress/enableIngress'; import { toggleIngressVisibility } from './ingress/toggleIngressVisibility'; import { startStreamingLogs } from './logStream/startStreamingLogs';