-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
addEnvironmentVariable
command (#781)
- Loading branch information
1 parent
d8a7196
commit cd1e562
Showing
16 changed files
with
340 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/commands/environmentVariables/EnvironmentVariablesContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.md in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { type ContainerUpdateTelemetryProps as TelemetryProps } from "../../telemetry/commandTelemetryProps"; | ||
import { type SetTelemetryProps } from "../../telemetry/SetTelemetryProps"; | ||
import { type ContainerEditBaseContext } from "../editContainer/ContainerEditContext"; | ||
|
||
export type EnvironmentVariablesBaseContext = ContainerEditBaseContext; | ||
export type EnvironmentVariablesContext = EnvironmentVariablesBaseContext & SetTelemetryProps<TelemetryProps>; |
19 changes: 19 additions & 0 deletions
19
src/commands/environmentVariables/addEnvironmentVariable/EnvironmentVariableAddContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.md in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { type SetTelemetryProps } from "../../../telemetry/SetTelemetryProps"; | ||
import { type ContainerUpdateTelemetryProps as TelemetryProps } from "../../../telemetry/commandTelemetryProps"; | ||
import { type ISecretContext } from "../../secret/ISecretContext"; | ||
import { type EnvironmentVariablesBaseContext } from "../EnvironmentVariablesContext"; | ||
import { type EnvironmentVariableType } from "./EnvironmentVariableTypeListStep"; | ||
|
||
export interface EnvironmentVariableAddBaseContext extends EnvironmentVariablesBaseContext, Pick<ISecretContext, 'secretName'> { | ||
newEnvironmentVariableType?: EnvironmentVariableType; | ||
newEnvironmentVariableName?: string; | ||
newEnvironmentVariableManualInput?: string; | ||
// secretName | ||
} | ||
|
||
export type EnvironmentVariableAddContext = EnvironmentVariableAddBaseContext & SetTelemetryProps<TelemetryProps>; |
39 changes: 39 additions & 0 deletions
39
src/commands/environmentVariables/addEnvironmentVariable/EnvironmentVariableAddDraftStep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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 EnvironmentVariableAddContext } from "./EnvironmentVariableAddContext"; | ||
|
||
export class EnvironmentVariableAddDraftStep<T extends EnvironmentVariableAddContext> 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('addingEnv', 'Adding environment variable (draft)...') }); | ||
this.revisionDraftTemplate.containers ??= []; | ||
|
||
const container: Container = this.revisionDraftTemplate.containers[context.containersIdx] ?? {}; | ||
container.env ??= []; | ||
container.env.push({ | ||
name: context.newEnvironmentVariableName, | ||
value: context.newEnvironmentVariableManualInput ?? '', // The server doesn't allow this value to be undefined | ||
secretRef: context.secretName, | ||
}); | ||
|
||
await this.updateRevisionDraftWithTemplate(context); | ||
} | ||
|
||
public shouldExecute(context: T): boolean { | ||
return context.containersIdx !== undefined && !!context.newEnvironmentVariableName; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ommands/environmentVariables/addEnvironmentVariable/EnvironmentVariableManualInputStep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { AzureWizardPromptStep } from "@microsoft/vscode-azext-utils"; | ||
import { localize } from "../../../utils/localize"; | ||
import { type EnvironmentVariableAddContext } from "./EnvironmentVariableAddContext"; | ||
|
||
export class EnvironmentVariableManualInputStep<T extends EnvironmentVariableAddContext> extends AzureWizardPromptStep<T> { | ||
public async prompt(context: T): Promise<void> { | ||
context.newEnvironmentVariableManualInput = (await context.ui.showInputBox({ | ||
prompt: localize('envManualPrompt', 'Enter a value for the environment variable'), | ||
})).trim(); | ||
context.valuesToMask.push(context.newEnvironmentVariableManualInput); | ||
} | ||
|
||
public shouldPrompt(context: T): boolean { | ||
return !context.newEnvironmentVariableManualInput; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/commands/environmentVariables/addEnvironmentVariable/EnvironmentVariableNameStep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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, type EnvironmentVar } from "@azure/arm-appcontainers"; | ||
import { AzureWizardPromptStep, validationUtils } from "@microsoft/vscode-azext-utils"; | ||
import { ext } from "../../../extensionVariables"; | ||
import { type EnvironmentVariableItem } from "../../../tree/containers/EnvironmentVariableItem"; | ||
import { type EnvironmentVariablesItem } from "../../../tree/containers/EnvironmentVariablesItem"; | ||
import { localize } from "../../../utils/localize"; | ||
import { getParentResourceFromItem } from "../../../utils/revisionDraftUtils"; | ||
import { type EnvironmentVariableAddContext } from "./EnvironmentVariableAddContext"; | ||
|
||
export class EnvironmentVariableNameStep<T extends EnvironmentVariableAddContext> extends AzureWizardPromptStep<T> { | ||
constructor(readonly baseItem: EnvironmentVariableItem | EnvironmentVariablesItem) { | ||
super(); | ||
} | ||
|
||
public async prompt(context: T): Promise<void> { | ||
context.newEnvironmentVariableName = (await context.ui.showInputBox({ | ||
prompt: localize('envNamePrompt', 'Enter a name for the environment variable'), | ||
validateInput: (value: string) => this.validateInput(context, value), | ||
})).trim(); | ||
context.valuesToMask.push(context.newEnvironmentVariableName); | ||
} | ||
|
||
public shouldPrompt(context: T): boolean { | ||
return !context.newEnvironmentVariableName; | ||
} | ||
|
||
private validateInput(context: T, value: string): string | undefined { | ||
if (!validationUtils.hasValidCharLength(value)) { | ||
return validationUtils.getInvalidCharLengthMessage(); | ||
} | ||
|
||
// This is the same regex used by the portal with similar warning verbiage | ||
const rule = /^[-._a-zA-z][-._a-zA-Z0-9]*$/; | ||
if (!rule.test(value)) { | ||
return localize('invalidEnvName', 'Name contains invalid character. Regex used for validation is "{0}".', String(rule)); | ||
} | ||
|
||
// Check for duplicates | ||
let container: Container | undefined; | ||
if (ext.revisionDraftFileSystem.doesContainerAppsItemHaveRevisionDraft(this.baseItem)) { | ||
container = ext.revisionDraftFileSystem.parseRevisionDraft(this.baseItem)?.containers?.[context.containersIdx]; | ||
} else { | ||
container = getParentResourceFromItem(this.baseItem).template?.containers?.[context.containersIdx]; | ||
} | ||
|
||
const envs: EnvironmentVar[] = container?.env ?? []; | ||
if (envs.some(env => env.name === value)) { | ||
return localize('duplicateEnv', 'Environment variable with name "{0}" already exists for this container.', value); | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/commands/environmentVariables/addEnvironmentVariable/EnvironmentVariableTypeListStep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { AzureWizardPromptStep, type IAzureQuickPickItem, type IWizardOptions } from "@microsoft/vscode-azext-utils"; | ||
import { localize } from "../../../utils/localize"; | ||
import { SecretListStep } from "../../secret/SecretListStep"; | ||
import { type EnvironmentVariableAddContext } from "./EnvironmentVariableAddContext"; | ||
import { EnvironmentVariableManualInputStep } from "./EnvironmentVariableManualInputStep"; | ||
|
||
export enum EnvironmentVariableType { | ||
ManualInput = 'manual', | ||
SecretRef = 'secretRef', | ||
} | ||
|
||
export class EnvironmentVariableTypeListStep<T extends EnvironmentVariableAddContext> extends AzureWizardPromptStep<T> { | ||
public async prompt(context: T): Promise<void> { | ||
const placeHolder: string = localize('environmentVariableTypePrompt', 'Select an environment variable type'); | ||
const picks: IAzureQuickPickItem<EnvironmentVariableType>[] = [ | ||
{ | ||
label: localize('manualLabel', 'Manual entry'), | ||
data: EnvironmentVariableType.ManualInput, | ||
}, | ||
{ | ||
label: localize('secretRefLabel', 'Reference a secret'), | ||
data: EnvironmentVariableType.SecretRef, | ||
}, | ||
]; | ||
context.newEnvironmentVariableType = (await context.ui.showQuickPick(picks, { | ||
placeHolder, | ||
suppressPersistence: true, | ||
})).data; | ||
} | ||
|
||
public shouldPrompt(context: T): boolean { | ||
return !context.newEnvironmentVariableType; | ||
} | ||
|
||
public async getSubWizard(context: T): Promise<IWizardOptions<T> | undefined> { | ||
const promptSteps: AzureWizardPromptStep<T>[] = []; | ||
|
||
switch (context.newEnvironmentVariableType) { | ||
case EnvironmentVariableType.ManualInput: | ||
promptSteps.push(new EnvironmentVariableManualInputStep()); | ||
break; | ||
case EnvironmentVariableType.SecretRef: | ||
promptSteps.push(new SecretListStep({ suppressCreatePick: true })); | ||
break; | ||
default: | ||
} | ||
|
||
return { promptSteps }; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/commands/environmentVariables/addEnvironmentVariable/addEnvironmentVariable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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, type IActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils"; | ||
import { type ContainerAppModel } from "../../../tree/ContainerAppItem"; | ||
import { type EnvironmentVariablesItem } from "../../../tree/containers/EnvironmentVariablesItem"; | ||
import { createActivityContext } from "../../../utils/activityUtils"; | ||
import { getManagedEnvironmentFromContainerApp } from "../../../utils/getResourceUtils"; | ||
import { getVerifyProvidersStep } from "../../../utils/getVerifyProvidersStep"; | ||
import { localize } from "../../../utils/localize"; | ||
import { pickEnvironmentVariables } from "../../../utils/pickItem/pickEnvironmentVariables"; | ||
import { getParentResourceFromItem, isTemplateItemEditable, TemplateItemNotEditableError } from "../../../utils/revisionDraftUtils"; | ||
import { RevisionDraftDeployPromptStep } from "../../revisionDraft/RevisionDraftDeployPromptStep"; | ||
import { type EnvironmentVariableAddContext } from "./EnvironmentVariableAddContext"; | ||
import { EnvironmentVariableAddDraftStep } from "./EnvironmentVariableAddDraftStep"; | ||
import { EnvironmentVariableNameStep } from "./EnvironmentVariableNameStep"; | ||
import { EnvironmentVariableTypeListStep } from "./EnvironmentVariableTypeListStep"; | ||
|
||
export async function addEnvironmentVariable(context: IActionContext, node?: EnvironmentVariablesItem): Promise<void> { | ||
const item: EnvironmentVariablesItem = node ?? await pickEnvironmentVariables(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: EnvironmentVariableAddContext = { | ||
...context, | ||
...subscriptionContext, | ||
...await createActivityContext(), | ||
subscription, | ||
managedEnvironment: await getManagedEnvironmentFromContainerApp({ ...context, ...subscriptionContext }, containerApp), | ||
containerApp, | ||
containersIdx: item.containersIdx, | ||
}; | ||
wizardContext.telemetry.properties.revisionMode = containerApp.revisionsMode; | ||
|
||
const wizard: AzureWizard<EnvironmentVariableAddContext> = new AzureWizard(wizardContext, { | ||
title: localize('updateEnvironmentVariables', 'Add environment variable to "{0}" (draft)', parentResource.name), | ||
promptSteps: [ | ||
new EnvironmentVariableNameStep(item), | ||
new EnvironmentVariableTypeListStep(), | ||
new RevisionDraftDeployPromptStep(), | ||
], | ||
executeSteps: [ | ||
getVerifyProvidersStep<EnvironmentVariableAddContext>(), | ||
new EnvironmentVariableAddDraftStep(item), | ||
], | ||
}); | ||
|
||
await wizard.prompt(); | ||
wizardContext.activityTitle = localize('updateEnvironmentVariables', 'Add environment variable "{0}" to "{1}" (draft)', wizardContext.newEnvironmentVariableName, parentResource.name); | ||
await wizard.execute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.