Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deleteEnvironmentVariable command #798

Open
wants to merge 3 commits into
base: mwf/mature-lime
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@
"title": "%containerApps.bulkEditEnvironmentVariables%",
"category": "Azure Container Apps"
},
{
"command": "containerApps.deleteEnvironmentVariable",
"title": "%containerApps.deleteEnvironmentVariable.title%",
"shortTitle": "%containerApps.deleteEnvironmentVariable.shortTitle%",
"category": "Azure Container Apps"
},
{
"command": "containerApps.editScaleRange",
"title": "%containerApps.editScaleRange%",
Expand Down Expand Up @@ -505,6 +511,11 @@
"when": "view =~ /(azureResourceGroups|azureFocusView)/ && viewItem =~ /environmentVariableItem/i",
"group": "1@1"
},
{
"command": "containerApps.deleteEnvironmentVariable",
"when": "view =~ /(azureResourceGroups|azureFocusView)/ && viewItem =~ /environmentVariableItem/i",
"group": "1@2"
},
{
"command": "containerApps.editScaleRange",
"when": "view =~ /(azureResourceGroups|azureFocusView)/ && viewItem =~ /scaleItem/i",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"containerApps.editEnvironmentVariableName.title": "Edit Environment Variable Name...",
"containerApps.editEnvironmentVariableName.shortTitle": "Edit Name...",
"containerApps.bulkEditEnvironmentVariables": "Bulk Edit Environment Variables (.env)...",
"containerApps.deleteEnvironmentVariable.title": "Delete Environment Variable...",
"containerApps.deleteEnvironmentVariable.shortTitle": "Delete...",
"containerApps.deployImageApi": "Deploy Image to Container App (API)...",
"containerApps.deployWorkspaceProject": "Deploy Project from Workspace...",
"containerApps.deployWorkspaceProjectApi": "Deploy Project from Workspace (API)...",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type EnvironmentVar } from "@azure/arm-appcontainers";
import { type SetTelemetryProps } from "../../../telemetry/SetTelemetryProps";
import { type ContainerUpdateTelemetryProps as TelemetryProps } from "../../../telemetry/commandTelemetryProps";
import { type EnvironmentVariablesBaseContext } from "../EnvironmentVariablesContext";

export interface EnvironmentVariableDeleteBaseContext extends EnvironmentVariablesBaseContext {
// Require the environment variable upfront so we can delete
environmentVariable: EnvironmentVar;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be part of the edit context?

Copy link
Contributor Author

@MicroFish91 MicroFish91 Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, which of the edit contexts are you referring to?

}

export type EnvironmentVariableDeleteContext = EnvironmentVariableDeleteBaseContext & SetTelemetryProps<TelemetryProps>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type Container } from "@azure/arm-appcontainers";
import { type Progress } from "vscode";
import { type ContainerAppItem } from "../../../tree/ContainerAppItem";
import { type RevisionsItemModel } from "../../../tree/revisionManagement/RevisionItem";
import { localize } from "../../../utils/localize";
import { RevisionDraftUpdateBaseStep } from "../../revisionDraft/RevisionDraftUpdateBaseStep";
import { type EnvironmentVariableDeleteContext } from "./EnvironmentVariableDeleteContext";

export class EnvironmentVariableDeleteDraftStep<T extends EnvironmentVariableDeleteContext> extends RevisionDraftUpdateBaseStep<T> {
public priority: number = 590;

constructor(baseItem: ContainerAppItem | RevisionsItemModel) {
super(baseItem);
}

public async execute(context: T, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
progress.report({ message: localize('deletingEnv', 'Deleting environment variable (draft)...') });
this.revisionDraftTemplate.containers ??= [];

const container: Container = this.revisionDraftTemplate.containers[context.containersIdx] ?? {};
container.env = container.env?.filter(env => env.name !== context.environmentVariable.name) ?? [];

await this.updateRevisionDraftWithTemplate(context);
}

public shouldExecute(context: T): boolean {
return !!context.environmentVariable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type Revision } from "@azure/arm-appcontainers";
import { AzureWizard, createSubscriptionContext, DeleteConfirmationStep, type IActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils";
import { type ContainerAppModel } from "../../../tree/ContainerAppItem";
import { type EnvironmentVariableItem } from "../../../tree/containers/EnvironmentVariableItem";
import { createActivityContext } from "../../../utils/activityUtils";
import { getManagedEnvironmentFromContainerApp } from "../../../utils/getResourceUtils";
import { getVerifyProvidersStep } from "../../../utils/getVerifyProvidersStep";
import { localize } from "../../../utils/localize";
import { pickEnvironmentVariable } from "../../../utils/pickItem/pickEnvironmentVariables";
import { getParentResourceFromItem, isTemplateItemEditable, TemplateItemNotEditableError } from "../../../utils/revisionDraftUtils";
import { type EnvironmentVariableDeleteContext } from "./EnvironmentVariableDeleteContext";
import { EnvironmentVariableDeleteDraftStep } from "./EnvironmentVariableDeleteDraftStep";

export async function deleteEnvironmentVariable(context: IActionContext, node?: EnvironmentVariableItem): Promise<void> {
const item: EnvironmentVariableItem = node ?? await pickEnvironmentVariable(context, { autoSelectDraft: true });
const { subscription, containerApp } = item;

if (!isTemplateItemEditable(item)) {
throw new TemplateItemNotEditableError(item);
}

const subscriptionContext: ISubscriptionContext = createSubscriptionContext(subscription);
const parentResource: ContainerAppModel | Revision = getParentResourceFromItem(item);

const wizardContext: EnvironmentVariableDeleteContext = {
...context,
...subscriptionContext,
...await createActivityContext(),
subscription,
managedEnvironment: await getManagedEnvironmentFromContainerApp({ ...context, ...subscriptionContext }, containerApp),
containerApp,
containersIdx: item.containersIdx,
environmentVariable: item.envVariable,
};
wizardContext.telemetry.properties.revisionMode = containerApp.revisionsMode;

const confirmMessage: string = localize('confirmDeleteEnv', 'Are you sure you want to delete environment variable "{0}" (draft)?', item.envVariable.name);
const wizard: AzureWizard<EnvironmentVariableDeleteContext> = new AzureWizard(wizardContext, {
title: localize('deleteEnvironmentVariable', 'Delete environment variable "{0}" from "{1}" (draft)', item.envVariable.name, parentResource.name),
promptSteps: [
new DeleteConfirmationStep(confirmMessage),
],
executeSteps: [
getVerifyProvidersStep<EnvironmentVariableDeleteContext>(),
new EnvironmentVariableDeleteDraftStep(item),
],
});

await wizard.prompt();
await wizard.execute();
}
2 changes: 2 additions & 0 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { editContainerImage } from './editContainer/editContainerImage/editConta
import { editContainerApp } from './editContainerApp';
import { addEnvironmentVariable } from './environmentVariables/addEnvironmentVariable/addEnvironmentVariable';
import { bulkEditEnvironmentVariables } from './environmentVariables/bulkEditEnvironmentVariables/bulkEditEnvironmentVariables';
import { deleteEnvironmentVariable } from './environmentVariables/deleteEnvironmentVariable/deleteEnvironmentVariable';
import { editEnvironmentVariableName } from './environmentVariables/editEnvironmentVariable/editEnvironmentVariableName';
import { connectToGitHub } from './gitHub/connectToGitHub/connectToGitHub';
import { disconnectRepo } from './gitHub/disconnectRepo/disconnectRepo';
Expand Down Expand Up @@ -75,6 +76,7 @@ export function registerCommands(): void {
registerCommandWithTreeNodeUnwrapping('containerApps.addEnvironmentVariable', addEnvironmentVariable);
registerCommandWithTreeNodeUnwrapping('containerApps.editEnvironmentVariableName', editEnvironmentVariableName);
registerCommandWithTreeNodeUnwrapping('containerApps.bulkEditEnvironmentVariables', bulkEditEnvironmentVariables);
registerCommandWithTreeNodeUnwrapping('containerApps.deleteEnvironmentVariable', deleteEnvironmentVariable);

// deploy
registerCommandWithTreeNodeUnwrapping('containerApps.deployImageApi', deployImageApi);
Expand Down