|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils"; |
| 7 | +import { AzureWizardPromptStep, nonNullValueAndProp } from "@microsoft/vscode-azext-utils"; |
| 8 | +import { ProgressLocation, window } from "vscode"; |
| 9 | +import { ext } from "../../../extensionVariables"; |
| 10 | +import { localize } from "../../../utils/localize"; |
| 11 | +import { validateUtils } from "../../../utils/validateUtils"; |
| 12 | +import { ContainerAppNameStep } from "../../createContainerApp/ContainerAppNameStep"; |
| 13 | +import { ManagedEnvironmentNameStep } from "../../createManagedEnvironment/ManagedEnvironmentNameStep"; |
| 14 | +import { RegistryNameStep } from "../../deployImage/imageSource/containerRegistry/acr/createAcr/RegistryNameStep"; |
| 15 | +import type { DeployWorkspaceProjectContext } from "../DeployWorkspaceProjectContext"; |
| 16 | + |
| 17 | +export class DefaultResourcesNameStep extends AzureWizardPromptStep<DeployWorkspaceProjectContext> { |
| 18 | + public async prompt(context: DeployWorkspaceProjectContext): Promise<void> { |
| 19 | + ext.outputChannel.appendLog(localize('resourceNameUnavailable', |
| 20 | + 'Info: Some container app resources matching the workspace name "{0}" were invalid or unavailable.', |
| 21 | + cleanWorkspaceName(nonNullValueAndProp(context.rootFolder, 'name'))) |
| 22 | + ); |
| 23 | + |
| 24 | + const resourceBaseName: string = (await context.ui.showInputBox({ |
| 25 | + prompt: localize('resourceBaseNamePrompt', 'Enter a name for new container app resources.'), |
| 26 | + validateInput: this.validateInput, |
| 27 | + asyncValidationTask: (name: string) => this.validateNameAvailability(context, name) |
| 28 | + })).trim(); |
| 29 | + |
| 30 | + ext.outputChannel.appendLog(localize('usingResourceName', 'User provided the new resource name "{0}" as the default for resource creation.', resourceBaseName)) |
| 31 | + |
| 32 | + !context.resourceGroup && (context.newResourceGroupName = resourceBaseName); |
| 33 | + !context.managedEnvironment && (context.newManagedEnvironmentName = resourceBaseName); |
| 34 | + !context.registry && (context.newRegistryName = resourceBaseName.replace(/[^a-z0-9]+/g, '')); |
| 35 | + !context.containerApp && (context.newContainerAppName = resourceBaseName); |
| 36 | + context.imageName = `${resourceBaseName}:latest`; |
| 37 | + } |
| 38 | + |
| 39 | + public async configureBeforePrompt(context: DeployWorkspaceProjectContext): Promise<void> { |
| 40 | + const workspaceName: string = cleanWorkspaceName(nonNullValueAndProp(context.rootFolder, 'name')); |
| 41 | + if (this.validateInput(workspaceName) !== undefined) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (!await this.isWorkspaceNameAvailable(context, workspaceName)) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (!context.resourceGroup || !context.managedEnvironment || !context.registry || !context.containerApp) { |
| 50 | + ext.outputChannel.appendLog(localize('usingWorkspaceName', 'Using workspace name "{0}" as the default for remaining resource creation.', workspaceName)); |
| 51 | + } |
| 52 | + |
| 53 | + !context.resourceGroup && (context.newResourceGroupName = workspaceName); |
| 54 | + !context.managedEnvironment && (context.newManagedEnvironmentName = workspaceName); |
| 55 | + !context.registry && (context.newRegistryName = workspaceName.replace(/[^a-z0-9]+/g, '')); |
| 56 | + !context.containerApp && (context.newContainerAppName = workspaceName); |
| 57 | + context.imageName = `${context.containerApp?.name || workspaceName}:latest`; |
| 58 | + } |
| 59 | + |
| 60 | + public shouldPrompt(context: DeployWorkspaceProjectContext): boolean { |
| 61 | + return (!context.resourceGroup && !context.newResourceGroupName) || |
| 62 | + (!context.managedEnvironment && !context.newManagedEnvironmentName) || |
| 63 | + (!context.registry && !context.newRegistryName) || |
| 64 | + (!context.containerApp && !context.newContainerAppName); |
| 65 | + } |
| 66 | + |
| 67 | + private validateInput(name: string | undefined): string | undefined { |
| 68 | + name ??= ''; |
| 69 | + |
| 70 | + // 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 |
| 71 | + const nameWithoutSymbols: string = name.replace(/[^a-z0-9]+/g, ''); |
| 72 | + if (!validateUtils.isValidLength(nameWithoutSymbols, 5, 20)) { |
| 73 | + return localize('invalidLength', 'The alphanumeric portion of the name must be least 5 characters but no more than 20 characters.'); |
| 74 | + } |
| 75 | + |
| 76 | + const symbols: string = '-'; |
| 77 | + if (!validateUtils.isLowerCaseAlphanumericWithSymbols(name, symbols, false /** canSymbolsRepeat */)) { |
| 78 | + return validateUtils.getInvalidLowerCaseAlphanumericWithSymbolsMessage(symbols); |
| 79 | + } |
| 80 | + |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + |
| 84 | + protected async validateNameAvailability(context: DeployWorkspaceProjectContext, name: string): Promise<string | undefined> { |
| 85 | + return await window.withProgress({ |
| 86 | + location: ProgressLocation.Notification, |
| 87 | + cancellable: false, |
| 88 | + title: localize('verifyingAvailabilityTitle', 'Verifying resource name availability...') |
| 89 | + }, async () => { |
| 90 | + const resourceNameUnavailable: string = localize('resourceNameUnavailable', 'Resource name "{0}" is already taken.', name); |
| 91 | + |
| 92 | + const registryAvailable: boolean = !!context.registry || await RegistryNameStep.isNameAvailable(context, name.replace(/[^a-zA-Z0-9]+/g, '')); |
| 93 | + if (!registryAvailable) { |
| 94 | + return resourceNameUnavailable; |
| 95 | + } |
| 96 | + |
| 97 | + const resourceGroupAvailable: boolean = !!context.resourceGroup || await ResourceGroupListStep.isNameAvailable(context, name); |
| 98 | + if (!resourceGroupAvailable) { |
| 99 | + return resourceNameUnavailable; |
| 100 | + } |
| 101 | + |
| 102 | + if (context.resourceGroup) { |
| 103 | + const managedEnvironmentAvailable: boolean = !!context.managedEnvironment || await ManagedEnvironmentNameStep.isNameAvailable(context, name, name); |
| 104 | + if (!managedEnvironmentAvailable) { |
| 105 | + return resourceNameUnavailable; |
| 106 | + } |
| 107 | + } else { |
| 108 | + // Skip check - new resource group means unique managed environment |
| 109 | + } |
| 110 | + |
| 111 | + if (context.managedEnvironment) { |
| 112 | + const containerAppAvailable: boolean = !!context.containerApp || await ContainerAppNameStep.isNameAvailable(context, name, name); |
| 113 | + if (!containerAppAvailable) { |
| 114 | + return resourceNameUnavailable; |
| 115 | + } |
| 116 | + } else { |
| 117 | + // Skip check - new managed environment means unique container app |
| 118 | + } |
| 119 | + |
| 120 | + return undefined; |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + protected async isWorkspaceNameAvailable(context: DeployWorkspaceProjectContext, workspaceName: string): Promise<boolean> { |
| 125 | + const isAvailable: Record<string, boolean> = {}; |
| 126 | + |
| 127 | + if (context.resourceGroup || await ResourceGroupListStep.isNameAvailable(context, workspaceName)) { |
| 128 | + isAvailable['resourceGroup'] = true; |
| 129 | + } |
| 130 | + |
| 131 | + if (context.managedEnvironment || await ManagedEnvironmentNameStep.isNameAvailable(context, workspaceName, workspaceName)) { |
| 132 | + isAvailable['managedEnvironment'] = true; |
| 133 | + } |
| 134 | + |
| 135 | + if (context.registry || await RegistryNameStep.isNameAvailable(context, workspaceName.replace(/[^a-z0-9]+/g, ''))) { |
| 136 | + isAvailable['containerRegistry'] = true; |
| 137 | + } |
| 138 | + |
| 139 | + if (context.containerApp || await ContainerAppNameStep.isNameAvailable(context, workspaceName, workspaceName)) { |
| 140 | + isAvailable['containerApp'] = true; |
| 141 | + } |
| 142 | + |
| 143 | + return isAvailable['resourceGroup'] && isAvailable['managedEnvironment'] && isAvailable['containerRegistry'] && isAvailable['containerApp']; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +export function cleanWorkspaceName(workspaceName: string): string { |
| 148 | + // Only alphanumeric characters or hyphens |
| 149 | + let cleanedWorkspaceName: string = workspaceName.toLowerCase().replace(/[^a-z0-9-]+/g, ''); |
| 150 | + |
| 151 | + // Remove any consecutive hyphens |
| 152 | + cleanedWorkspaceName = cleanedWorkspaceName.replace(/-+/g, '-'); |
| 153 | + |
| 154 | + // Remove any leading or ending hyphens |
| 155 | + if (cleanedWorkspaceName.startsWith('-')) { |
| 156 | + cleanedWorkspaceName = cleanedWorkspaceName.slice(1); |
| 157 | + } |
| 158 | + if (cleanedWorkspaceName.endsWith('-')) { |
| 159 | + cleanedWorkspaceName = cleanedWorkspaceName.slice(0, -1); |
| 160 | + } |
| 161 | + |
| 162 | + return cleanedWorkspaceName; |
| 163 | +} |
0 commit comments