-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRegistryNameStep.ts
More file actions
47 lines (39 loc) · 2.44 KB
/
RegistryNameStep.ts
File metadata and controls
47 lines (39 loc) · 2.44 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ContainerRegistryManagementClient, RegistryNameStatus } from "@azure/arm-containerregistry";
import { AzureWizardPromptStep } from "@microsoft/vscode-azext-utils";
import { createContainerRegistryManagementClient } from "../../../../../../utils/azureClients";
import { localize } from "../../../../../../utils/localize";
import { ICreateAcrContext } from "./ICreateAcrContext";
export class RegistryNameStep extends AzureWizardPromptStep<ICreateAcrContext> {
public async prompt(context: ICreateAcrContext): Promise<void> {
context.newRegistryName = await context.ui.showInputBox({
prompt: localize('registryName', 'Enter a name for the new registry'),
validateInput: this.validateInput,
asyncValidationTask: (value: string): Promise<string | undefined> => this.validateNameAvalability(context, value)
});
}
public shouldPrompt(context: ICreateAcrContext): boolean {
return !context.newRegistryName;
}
private validateInput(name: string | undefined): string | undefined {
name = name ? name.trim() : '';
const { minLength, maxLength } = { minLength: 5, maxLength: 50 };
if (name.length < minLength || name.length > maxLength) {
return localize('validationLengthError', 'The name must be between {0} and {1} characters.', minLength, maxLength);
} else if (!/^[a-z][a-zA-Z0-9]*$/.test(name)) {
return localize('validateInputError', `Connection names can only consist of alphanumeric characters.`);
}
return undefined;
}
private async validateNameAvalability(context: ICreateAcrContext, name: string) {
const client: ContainerRegistryManagementClient = await createContainerRegistryManagementClient(context);
const nameResponse: RegistryNameStatus = await client.registries.checkNameAvailability({ name: name, type: "Microsoft.ContainerRegistry/registries" });
if (nameResponse.nameAvailable === false) {
return localize('validateInputError', `The registry name ${name} is already in use.`);
}
return undefined;
}
}