-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDefaultResourcesNameStep.ts
More file actions
166 lines (136 loc) · 8.67 KB
/
DefaultResourcesNameStep.ts
File metadata and controls
166 lines (136 loc) · 8.67 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { AzureWizardPromptStep, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
import { ProgressLocation, window } from "vscode";
import { ext } from "../../../extensionVariables";
import { localize } from "../../../utils/localize";
import { validateUtils } from "../../../utils/validateUtils";
import { ContainerAppNameStep } from "../../createContainerApp/ContainerAppNameStep";
import { ManagedEnvironmentNameStep } from "../../createManagedEnvironment/ManagedEnvironmentNameStep";
import { ImageNameStep } from "../../image/imageSource/buildImageInAzure/ImageNameStep";
import { RegistryNameStep } from "../../image/imageSource/containerRegistry/acr/createAcr/RegistryNameStep";
import type { DeployWorkspaceProjectContext } from "../DeployWorkspaceProjectContext";
export class DefaultResourcesNameStep extends AzureWizardPromptStep<DeployWorkspaceProjectContext> {
public async prompt(context: DeployWorkspaceProjectContext): Promise<void> {
ext.outputChannel.appendLog(localize('resourceNameUnavailable',
'Info: Some container app resources matching the workspace name "{0}" were invalid or unavailable.',
cleanWorkspaceName(nonNullValueAndProp(context.rootFolder, 'name')))
);
const resourceBaseName: string = (await context.ui.showInputBox({
prompt: localize('resourceBaseNamePrompt', 'Enter a name for the new container app resource(s).'),
validateInput: this.validateInput,
asyncValidationTask: (name: string) => this.validateNameAvailability(context, name)
})).trim();
ext.outputChannel.appendLog(localize('usingResourceName', 'User provided the new resource name "{0}" as the default for resource creation.', resourceBaseName))
!context.resourceGroup && (context.newResourceGroupName = resourceBaseName);
!context.managedEnvironment && (context.newManagedEnvironmentName = resourceBaseName);
!context.containerApp && (context.newContainerAppName = resourceBaseName);
context.imageName = ImageNameStep.getTimestampedImageName(context.containerApp?.name || resourceBaseName);
}
public async configureBeforePrompt(context: DeployWorkspaceProjectContext): Promise<void> {
const workspaceName: string = cleanWorkspaceName(nonNullValueAndProp(context.rootFolder, 'name'));
if (this.validateInput(workspaceName) !== undefined) {
context.telemetry.properties.promptDefaultNameReason = 'invalid';
return;
}
if (!await this.isWorkspaceNameAvailable(context, workspaceName)) {
context.telemetry.properties.promptDefaultNameReason = 'unavailable';
return;
}
if (!context.resourceGroup || !context.managedEnvironment || !context.registry || !context.containerApp) {
ext.outputChannel.appendLog(localize('usingWorkspaceName', 'Using workspace name "{0}" as the default for remaining resource creation.', workspaceName));
}
!context.resourceGroup && (context.newResourceGroupName = workspaceName);
!context.managedEnvironment && (context.newManagedEnvironmentName = workspaceName);
!context.registry && (context.newRegistryName = await RegistryNameStep.tryGenerateRelatedName(context, workspaceName));
!context.containerApp && (context.newContainerAppName = workspaceName);
context.imageName = ImageNameStep.getTimestampedImageName(context.containerApp?.name || workspaceName);
}
public shouldPrompt(context: DeployWorkspaceProjectContext): boolean {
return (!context.resourceGroup && !context.newResourceGroupName) ||
(!context.managedEnvironment && !context.newManagedEnvironmentName) ||
(!context.registry && !context.newRegistryName) ||
(!context.containerApp && !context.newContainerAppName);
}
private validateInput(name: string | undefined): string | undefined {
name ??= '';
// No symbols are allowed for ACR - we will strip out any offending characters from the base name, but still need to ensure this version has an appropriate length
const nameWithoutSymbols: string = name.replace(/[^a-z0-9]+/g, '');
if (!validateUtils.isValidLength(nameWithoutSymbols, 5, 20)) {
return localize('invalidLength', 'The alphanumeric portion of the name must be least 5 characters but no more than 20 characters.');
}
const symbols: string = '-';
if (!validateUtils.isLowerCaseAlphanumericWithSymbols(name, symbols, false /** canSymbolsRepeat */)) {
return validateUtils.getInvalidLowerCaseAlphanumericWithSymbolsMessage(symbols);
}
return undefined;
}
protected async validateNameAvailability(context: DeployWorkspaceProjectContext, name: string): Promise<string | undefined> {
return await window.withProgress({
location: ProgressLocation.Notification,
cancellable: false,
title: localize('verifyingAvailabilityTitle', 'Verifying resource name availability...')
}, async () => {
const resourceNameUnavailable: string = localize('resourceNameUnavailable', 'Resource name "{0}" is unavailable.', name);
if (context.registry) {
// Skip check, one already exists so don't need to worry about naming
} else {
context.newRegistryName = await RegistryNameStep.tryGenerateRelatedName(context, name);
if (!context.newRegistryName) {
return localize('timeoutError', 'Timed out waiting for registry name to be generated. Please try another name.');
}
}
const resourceGroupAvailable: boolean = !!context.resourceGroup || await ResourceGroupListStep.isNameAvailable(context, name);
if (!resourceGroupAvailable) {
return `Resource group: ${resourceNameUnavailable}`;
}
if (context.resourceGroup) {
const managedEnvironmentAvailable: boolean = !!context.managedEnvironment || await ManagedEnvironmentNameStep.isNameAvailable(context, name, name);
if (!managedEnvironmentAvailable) {
return `Container apps environment: ${resourceNameUnavailable}`;
}
} else {
// Skip check - new resource group means unique managed environment
}
if (context.managedEnvironment) {
const containerAppAvailable: boolean = !!context.containerApp || await ContainerAppNameStep.isNameAvailable(context, name, name);
if (!containerAppAvailable) {
return `Container app: ${resourceNameUnavailable}`;
}
} else {
// Skip check - new managed environment means unique container app
}
return undefined;
});
}
protected async isWorkspaceNameAvailable(context: DeployWorkspaceProjectContext, workspaceName: string): Promise<boolean> {
const isAvailable: Record<string, boolean> = {};
if (context.resourceGroup || await ResourceGroupListStep.isNameAvailable(context, workspaceName)) {
isAvailable['resourceGroup'] = true;
}
if (context.managedEnvironment || await ManagedEnvironmentNameStep.isNameAvailable(context, workspaceName, workspaceName)) {
isAvailable['managedEnvironment'] = true;
}
if (context.containerApp || await ContainerAppNameStep.isNameAvailable(context, workspaceName, workspaceName)) {
isAvailable['containerApp'] = true;
}
return isAvailable['resourceGroup'] && isAvailable['managedEnvironment'] && isAvailable['containerApp'];
}
}
export function cleanWorkspaceName(workspaceName: string): string {
// Only alphanumeric characters or hyphens
let cleanedWorkspaceName: string = workspaceName.toLowerCase().replace(/[^a-z0-9-]+/g, '');
// Remove any consecutive hyphens
cleanedWorkspaceName = cleanedWorkspaceName.replace(/-+/g, '-');
// Remove any leading or ending hyphens
if (cleanedWorkspaceName.startsWith('-')) {
cleanedWorkspaceName = cleanedWorkspaceName.slice(1);
}
if (cleanedWorkspaceName.endsWith('-')) {
cleanedWorkspaceName = cleanedWorkspaceName.slice(0, -1);
}
return cleanedWorkspaceName;
}