Skip to content

Commit 5ac8f07

Browse files
authored
Decouple editTargetPort from createContainerApp (#371)
1 parent 60d08b2 commit 5ac8f07

File tree

9 files changed

+142
-50
lines changed

9 files changed

+142
-50
lines changed

src/commands/createContainerApp/EnableIngressStep.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import { AzureWizardPromptStep, IWizardOptions } from "@microsoft/vscode-azext-utils";
77
import { localize } from "../../utils/localize";
8+
import { TargetPortInputStep } from "../ingress/editTargetPort/TargetPortInputStep";
89
import { ICreateContainerAppContext } from "./ICreateContainerAppContext";
910
import { IngressVisibilityStep } from "./IngressVisibilityStep";
10-
import { TargetPortStep } from "./TargetPortStep";
1111

1212
export class EnableIngressStep extends AzureWizardPromptStep<ICreateContainerAppContext> {
1313
public async prompt(context: ICreateContainerAppContext): Promise<void> {
@@ -23,7 +23,9 @@ export class EnableIngressStep extends AzureWizardPromptStep<ICreateContainerApp
2323
public async getSubWizard(context: ICreateContainerAppContext): Promise<IWizardOptions<ICreateContainerAppContext> | undefined> {
2424
if (context.enableIngress) {
2525
return {
26-
promptSteps: [new IngressVisibilityStep(), new TargetPortStep()]
26+
// Context types will be temporarily incompatible until all the ingress commands are decoupled from the create command
27+
// @ts-ignore
28+
promptSteps: [new IngressVisibilityStep(), new TargetPortInputStep()]
2729
}
2830
}
2931

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 { ExecuteActivityContext } from "@microsoft/vscode-azext-utils";
7+
import type { IContainerAppContext } from "../IContainerAppContext";
8+
9+
export interface IngressContext extends IContainerAppContext, ExecuteActivityContext {
10+
enableIngress?: boolean;
11+
enableExternal?: boolean;
12+
13+
targetPort?: number;
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 { Ingress } from "@azure/arm-appcontainers";
7+
import { AzureWizardExecuteStep, nonNullProp } from "@microsoft/vscode-azext-utils";
8+
import type { Progress } from "vscode";
9+
import { ext } from "../../extensionVariables";
10+
import type { IContainerAppContext } from "../IContainerAppContext";
11+
import { updateContainerApp } from "../deployContainerApp/updateContainerApp";
12+
13+
type IngressOptions = {
14+
ingress: Ingress | null,
15+
working: string,
16+
workCompleted: string
17+
}
18+
19+
export abstract class IngressUpdateBaseStep<T extends IContainerAppContext> extends AzureWizardExecuteStep<T> {
20+
protected async updateIngressSettings(context: T, progress: Progress<{ message?: string | undefined}>, options: IngressOptions): Promise<void> {
21+
const containerApp = nonNullProp(context, 'containerApp');
22+
const { ingress, working, workCompleted } = options;
23+
24+
progress.report({ message: working });
25+
await updateContainerApp(context, context.subscription, containerApp, { configuration: { ingress: ingress as Ingress | undefined } });
26+
27+
ext.outputChannel.appendLog(workCompleted);
28+
ext.state.notifyChildrenChanged(containerApp.managedEnvironmentId);
29+
}
30+
}

src/commands/ingress/editTargetPort.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/commands/createContainerApp/TargetPortStep.ts renamed to src/commands/ingress/editTargetPort/TargetPortInputStep.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { AzureWizardPromptStep } from "@microsoft/vscode-azext-utils";
7-
import { localize } from "../../utils/localize";
8-
import { ICreateContainerAppContext } from "./ICreateContainerAppContext";
7+
import { localize } from "../../../utils/localize";
8+
import type { IngressContext } from "../IngressContext";
9+
import { getDefaultPort } from "./getDefaultPort";
910

10-
export class TargetPortStep extends AzureWizardPromptStep<ICreateContainerAppContext> {
11-
public async prompt(context: ICreateContainerAppContext): Promise<void> {
11+
export class TargetPortInputStep extends AzureWizardPromptStep<IngressContext> {
12+
public async prompt(context: IngressContext): Promise<void> {
1213
context.targetPort = Number(await context.ui.showInputBox({
1314
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.'),
14-
value: String(context.defaultPort ?? 80),
15+
value: String(getDefaultPort(context)),
1516
validateInput: this.validateInput
1617
}));
1718
}
1819

19-
public shouldPrompt(context: ICreateContainerAppContext): boolean {
20+
public shouldPrompt(context: IngressContext): boolean {
2021
return !context.targetPort;
2122
}
2223

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 { nonNullProp, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
7+
import type { Progress } from "vscode";
8+
import { localize } from "../../../utils/localize";
9+
import type { IngressContext } from "../IngressContext";
10+
import { IngressUpdateBaseStep } from "../IngressUpdateBaseStep";
11+
12+
export class TargetPortUpdateStep extends IngressUpdateBaseStep<IngressContext> {
13+
public priority: number = 280;
14+
15+
public async execute(context: IngressContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
16+
const containerApp = nonNullProp(context, 'containerApp');
17+
const ingress = nonNullValueAndProp(containerApp.configuration, 'ingress');
18+
ingress.targetPort = context.targetPort;
19+
20+
const working: string = localize('updatingTargetPort', 'Updating target port...');
21+
const workCompleted: string = localize('updatedTargetPort', 'Updated target port to {0} for container app "{1}"', context.targetPort, containerApp.name);
22+
23+
context.activityTitle = localize('updateTargetPort', 'Update target port to {0} for container app "{1}"', context.targetPort, containerApp.name);
24+
await this.updateIngressSettings(context, progress, { ingress, working, workCompleted });
25+
}
26+
27+
public shouldExecute(): boolean {
28+
return true;
29+
}
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { AzureWizard, AzureWizardExecuteStep, AzureWizardPromptStep, createSubscriptionContext, IActionContext } from "@microsoft/vscode-azext-utils";
7+
import type { ContainerAppItem } from "../../../tree/ContainerAppItem";
8+
import { IngressItem } from "../../../tree/IngressItem";
9+
import { createActivityContext } from "../../../utils/activityUtils";
10+
import { localize } from "../../../utils/localize";
11+
import { pickContainerApp } from "../../../utils/pickContainerApp";
12+
import type { IngressContext } from "../IngressContext";
13+
import { TargetPortInputStep } from "./TargetPortInputStep";
14+
import { TargetPortUpdateStep } from "./TargetPortUpdateStep";
15+
16+
export async function editTargetPort(context: IActionContext, node?: IngressItem): Promise<void> {
17+
const { subscription, containerApp }: ContainerAppItem | IngressItem = node ?? await pickContainerApp(context);
18+
19+
const wizardContext: IngressContext = {
20+
...context,
21+
...createSubscriptionContext(subscription),
22+
...(await createActivityContext()),
23+
subscription,
24+
containerApp
25+
};
26+
27+
const title: string = localize('updateTargetPort', 'Update target port for container app "{0}"', containerApp.name);
28+
29+
const promptSteps: AzureWizardPromptStep<IngressContext>[] = [
30+
new TargetPortInputStep()
31+
];
32+
33+
const executeSteps: AzureWizardExecuteStep<IngressContext>[] = [
34+
new TargetPortUpdateStep()
35+
];
36+
37+
const wizard: AzureWizard<IngressContext> = new AzureWizard(wizardContext, {
38+
title,
39+
promptSteps,
40+
executeSteps,
41+
showLoadingPrompt: true
42+
});
43+
44+
await wizard.prompt();
45+
await wizard.execute();
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.md in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import type { IngressContext } from "../IngressContext";
7+
8+
export function getDefaultPort(context: IngressContext, fallbackPort: number = 80): number {
9+
return context.containerApp?.configuration?.ingress?.targetPort || fallbackPort;
10+
}

src/commands/registerCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { connectToGitHub } from './gitHub/connectToGitHub/connectToGitHub';
1616
import { disconnectRepo } from './gitHub/disconnectRepo/disconnectRepo';
1717
import { openGitHubRepo } from './gitHub/openGitHubRepo';
1818
import { disableIngress } from './ingress/disableIngress';
19-
import { editTargetPort } from './ingress/editTargetPort';
19+
import { editTargetPort } from './ingress/editTargetPort/editTargetPort';
2020
import { enableIngress } from './ingress/enableIngress';
2121
import { toggleIngressVisibility } from './ingress/toggleIngressVisibility';
2222
import { startStreamingLogs } from './logStream/startStreamingLogs';

0 commit comments

Comments
 (0)