-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathManagedEnvironmentNameStep.ts
More file actions
64 lines (53 loc) · 3.28 KB
/
ManagedEnvironmentNameStep.ts
File metadata and controls
64 lines (53 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ContainerAppsAPIClient } from "@azure/arm-appcontainers";
import { AzureWizardPromptStep, ISubscriptionActionContext, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
import { createContainerAppsAPIClient } from "../../utils/azureClients";
import { localize } from "../../utils/localize";
import { IManagedEnvironmentContext } from './IManagedEnvironmentContext';
export class ManagedEnvironmentNameStep extends AzureWizardPromptStep<IManagedEnvironmentContext> {
public async prompt(context: IManagedEnvironmentContext): Promise<void> {
const prompt: string = localize('containerAppNamePrompt', 'Enter a container apps environment name.');
context.newManagedEnvironmentName = (await context.ui.showInputBox({
prompt,
validateInput: this.validateInput,
asyncValidationTask: (name: string) => this.validateNameAvailable(context, name)
})).trim();
context.valuesToMask.push(context.newManagedEnvironmentName);
}
public shouldPrompt(context: IManagedEnvironmentContext): boolean {
return !context.managedEnvironment && !context.newManagedEnvironmentName;
}
private validateInput(name: string | undefined): string | undefined {
name = name ? name.trim() : '';
const { minLength, maxLength } = { minLength: 4, maxLength: 20 };
if (!/^[a-z]([-a-z0-9]*[a-z0-9])?$/.test(name)) {
return localize('invalidChar', `A name must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character and cannot have '--'.`);
} else if ((name.length < minLength) || name.length > maxLength) {
return localize('invalidLength', 'The name must be between {0} and {1} characters.', minLength, maxLength);
}
return undefined;
}
private async validateNameAvailable(context: IManagedEnvironmentContext, name: string): Promise<string | undefined> {
if (!context.resourceGroup) {
// If a new resource group will house the managed environment, we can skip the name check
return undefined;
}
const resourceGroupName: string = nonNullValueAndProp(context.resourceGroup, 'name');
if (!await ManagedEnvironmentNameStep.isNameAvailable(context, resourceGroupName, name)) {
return localize('managedEnvironmentExists', 'The container apps environment "{0}" already exists in resource group "{1}".', name, resourceGroupName);
}
return undefined;
}
public static async isNameAvailable(context: ISubscriptionActionContext, resourceGroupName: string, environmentName: string): Promise<boolean> {
const client: ContainerAppsAPIClient = await createContainerAppsAPIClient(context);
try {
await client.managedEnvironments.get(resourceGroupName, environmentName);
return false;
} catch (_e) {
return true;
}
}
}