Skip to content

Commit

Permalink
Improve resource naming validation messages for `deployWorkspaceProje…
Browse files Browse the repository at this point in the history
…ct` (#528)
  • Loading branch information
MicroFish91 authored Oct 30, 2023
1 parent 500eb9a commit 605dee8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { RegistryNameStatus } from "@azure/arm-containerregistry";
import { ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { AzureWizardPromptStep, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
import { ProgressLocation, window } from "vscode";
Expand Down Expand Up @@ -89,22 +90,26 @@ export class DefaultResourcesNameStep extends AzureWizardPromptStep<DeployWorksp
cancellable: false,
title: localize('verifyingAvailabilityTitle', 'Verifying resource name availability...')
}, async () => {
const resourceNameUnavailable: string = localize('resourceNameUnavailable', 'Resource name "{0}" is already taken.', name);
const resourceNameUnavailable: string = localize('resourceNameUnavailable', 'Resource name "{0}" is unavailable.', name);

const registryAvailable: boolean = !!context.registry || await RegistryNameStep.isNameAvailable(context, name.replace(/[^a-zA-Z0-9]+/g, ''));
if (!registryAvailable) {
return resourceNameUnavailable;
if (context.registry) {
// Skip check, one already exists so don't need to worry about naming
} else {
const registryNameStatus: RegistryNameStatus = await RegistryNameStep.isNameAvailable(context, name.replace(/[^a-zA-Z0-9]+/g, ''));
if (!registryNameStatus.nameAvailable) {
return `Registry: ${registryNameStatus.message ?? resourceNameUnavailable}`;
}
}

const resourceGroupAvailable: boolean = !!context.resourceGroup || await ResourceGroupListStep.isNameAvailable(context, name);
if (!resourceGroupAvailable) {
return resourceNameUnavailable;
return `Resource group: ${resourceNameUnavailable}`;
}

if (context.resourceGroup) {
const managedEnvironmentAvailable: boolean = !!context.managedEnvironment || await ManagedEnvironmentNameStep.isNameAvailable(context, name, name);
if (!managedEnvironmentAvailable) {
return resourceNameUnavailable;
return `Container apps environment: ${resourceNameUnavailable}`;
}
} else {
// Skip check - new resource group means unique managed environment
Expand All @@ -113,7 +118,7 @@ export class DefaultResourcesNameStep extends AzureWizardPromptStep<DeployWorksp
if (context.managedEnvironment) {
const containerAppAvailable: boolean = !!context.containerApp || await ContainerAppNameStep.isNameAvailable(context, name, name);
if (!containerAppAvailable) {
return resourceNameUnavailable;
return `Container app: ${resourceNameUnavailable}`;
}
} else {
// Skip check - new managed environment means unique container app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ export class RegistryNameStep extends AzureWizardPromptStep<CreateAcrContext> {
}

private async validateNameAvalability(context: CreateAcrContext, name: string) {
if (!await RegistryNameStep.isNameAvailable(context, name)) {
return localize('validateInputError', `The registry name ${name} is already in use.`);
const registryNameStatus = await RegistryNameStep.isNameAvailable(context, name);
if (!registryNameStatus.nameAvailable) {
return registryNameStatus.message ?? localize('validateInputError', `The registry name ${name} is unavailable.`);
}

return undefined;
}

public static async isNameAvailable(context: ISubscriptionActionContext, name: string): Promise<boolean> {
const client: ContainerRegistryManagementClient = await createContainerRegistryManagementClient(context);
const nameResponse: RegistryNameStatus = await client.registries.checkNameAvailability({ name: name, type: "Microsoft.ContainerRegistry/registries" });
return !!nameResponse.nameAvailable;
public static async isNameAvailable(context: ISubscriptionActionContext, name: string): Promise<RegistryNameStatus> {
try {
const client: ContainerRegistryManagementClient = await createContainerRegistryManagementClient(context);
return await client.registries.checkNameAvailability({ name: name, type: "Microsoft.ContainerRegistry/registries" });
} catch (_theseHands) {
return { nameAvailable: true };
}
}
}

0 comments on commit 605dee8

Please sign in to comment.