Skip to content

Commit

Permalink
Remove boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroFish91 committed Jan 10, 2025
1 parent ddafff3 commit a8c57c3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type ResourceGroup } from "@azure/arm-resources";
import { LocationListStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { nonNullValue, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
import { type ManagedEnvironmentItem } from "../../tree/ManagedEnvironmentItem";
import { StartingResourcesLogStep } from "../StartingResourcesLogStep";
import { type ContainerAppCreateContext } from "./ContainerAppCreateContext";

export class ContainerAppCreateStartingResourcesLogStep<T extends ContainerAppCreateContext> extends StartingResourcesLogStep<T> {
constructor(readonly parentItem: ManagedEnvironmentItem) {
super();
}

async configureStartingResources(context: T): Promise<void> {
// Use the same resource group and location as the parent resource (managed environment)
const resourceGroupName: string = nonNullValueAndProp(this.parentItem.resource, 'resourceGroup');
const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(context);
context.resourceGroup = nonNullValue(resourceGroups.find(rg => rg.name === resourceGroupName));

await LocationListStep.setLocation(context, nonNullValueAndProp(this.parentItem.resource, 'location'));
}
}
31 changes: 12 additions & 19 deletions src/commands/createContainerApp/createContainerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type ResourceGroup } from "@azure/arm-resources";
import { LocationListStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { AzureWizard, createSubscriptionContext, nonNullProp, nonNullValue, nonNullValueAndProp, type IActionContext } from "@microsoft/vscode-azext-utils";
import { AzureWizard, createSubscriptionContext, nonNullProp, type IActionContext } from "@microsoft/vscode-azext-utils";
import { ImageSource } from "../../constants";
import { ext } from "../../extensionVariables";
import { ContainerAppItem } from "../../tree/ContainerAppItem";
Expand All @@ -17,41 +15,36 @@ import { localize } from "../../utils/localize";
import { pickEnvironment } from "../../utils/pickItem/pickEnvironment";
import { ImageSourceListStep } from "../image/imageSource/ImageSourceListStep";
import { type ContainerAppCreateContext } from "./ContainerAppCreateContext";
import { ContainerAppCreateStartingResourcesLogStep } from "./ContainerAppCreateStartingResourcesLogStep";
import { ContainerAppCreateStep } from "./ContainerAppCreateStep";
import { ContainerAppNameStep } from "./ContainerAppNameStep";
import { showContainerAppNotification } from "./showContainerAppNotification";

export async function createContainerApp(context: IActionContext, node?: ManagedEnvironmentItem): Promise<ContainerAppItem> {
export async function createContainerApp(context: IActionContext, item?: ManagedEnvironmentItem): Promise<ContainerAppItem> {
// If an incompatible tree item is passed, treat it as if no item was passed
if (node && !ManagedEnvironmentItem.isManagedEnvironmentItem(node)) {
node = undefined;
if (item && !ManagedEnvironmentItem.isManagedEnvironmentItem(item)) {
item = undefined;
}

node ??= await pickEnvironment(context);
item ??= await pickEnvironment(context);

const wizardContext: ContainerAppCreateContext = {
...context,
...createSubscriptionContext(node.subscription),
...createSubscriptionContext(item.subscription),
...await createActivityContext(true),
subscription: node.subscription,
managedEnvironment: node.managedEnvironment,
subscription: item.subscription,
managedEnvironment: item.managedEnvironment,
imageSource: ImageSource.QuickstartImage,
};

if (isAzdExtensionInstalled()) {
wizardContext.telemetry.properties.isAzdExtensionInstalled = 'true';
}

// Use the same resource group and location as the parent resource (managed environment)
const resourceGroupName: string = nonNullValueAndProp(node.resource, 'resourceGroup');
const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(wizardContext);
wizardContext.resourceGroup = nonNullValue(resourceGroups.find(rg => rg.name === resourceGroupName));

await LocationListStep.setLocation(wizardContext, nonNullProp(node.resource, 'location'));

const wizard: AzureWizard<ContainerAppCreateContext> = new AzureWizard(wizardContext, {
title: localize('createContainerApp', 'Create container app'),
promptSteps: [
new ContainerAppCreateStartingResourcesLogStep(item),
new ContainerAppNameStep(),
new ImageSourceListStep(),
],
Expand All @@ -65,7 +58,7 @@ export async function createContainerApp(context: IActionContext, node?: Managed

const newContainerAppName = nonNullProp(wizardContext, 'newContainerAppName');
await ext.state.showCreatingChild(
node.managedEnvironment.id,
item.managedEnvironment.id,
localize('creating', 'Creating "{0}"...', newContainerAppName),
async () => {
wizardContext.activityTitle = localize('createNamedContainerApp', 'Create container app "{0}"', newContainerAppName);
Expand All @@ -78,5 +71,5 @@ export async function createContainerApp(context: IActionContext, node?: Managed
void showContainerAppNotification(createdContainerApp);
}

return new ContainerAppItem(node.subscription, createdContainerApp);
return new ContainerAppItem(item.subscription, createdContainerApp);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type ResourceGroup } from "@azure/arm-resources";
import { LocationListStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { nonNullValue, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
import { localize } from "../../utils/localize";
import { StartingResourcesLogStep } from "../StartingResourcesLogStep";
import { type ContainerAppDeployContext } from "./ContainerAppDeployContext";

export class ContainerAppDeployStartingResourcesLogStep<T extends ContainerAppDeployContext> extends StartingResourcesLogStep<T> {
async configureStartingResources(context: T): Promise<void> {
const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(context);
context.resourceGroup = nonNullValue(
resourceGroups.find(rg => rg.name === context.containerApp?.resourceGroup),
localize('containerAppResourceGroup', 'Expected to find the container app\'s resource group.'),
);
await LocationListStep.setLocation(context, nonNullValueAndProp(context.containerApp, 'location'));
}
}
35 changes: 3 additions & 32 deletions src/commands/deployContainerApp/deployContainerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { KnownActiveRevisionsMode } from "@azure/arm-appcontainers";
import { type ResourceGroup } from "@azure/arm-resources";
import { LocationListStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { activityInfoIcon, activitySuccessContext, AzureWizard, createSubscriptionContext, createUniversallyUniqueContextValue, GenericTreeItem, nonNullProp, nonNullValue, type IActionContext, type ISubscriptionActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils";
import { AzureWizard, createSubscriptionContext, nonNullProp, type IActionContext, type ISubscriptionActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils";
import { ImageSource } from "../../constants";
import { ext } from "../../extensionVariables";
import { type ContainerAppItem } from "../../tree/ContainerAppItem";
import { createActivityContext } from "../../utils/activityUtils";
import { isAzdExtensionInstalled } from "../../utils/azdUtils";
Expand All @@ -21,6 +18,7 @@ import { editContainerCommandName } from "../editContainer/editContainer";
import { ContainerAppUpdateStep } from "../image/imageSource/ContainerAppUpdateStep";
import { ImageSourceListStep } from "../image/imageSource/ImageSourceListStep";
import { type ContainerAppDeployContext } from "./ContainerAppDeployContext";
import { ContainerAppDeployStartingResourcesLogStep } from "./ContainerAppDeployStartingResourcesLogStep";

const deployContainerAppCommandName: string = localize('deployContainerApp', 'Deploy to Container App...');

Expand Down Expand Up @@ -57,37 +55,10 @@ export async function deployContainerApp(context: IActionContext, node?: Contain
wizardContext.telemetry.properties.isAzdExtensionInstalled = 'true';
}

const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(wizardContext);
wizardContext.resourceGroup = nonNullValue(
resourceGroups.find(rg => rg.name === item.containerApp.resourceGroup),
localize('containerAppResourceGroup', 'Expected to find the container app\'s resource group.'),
);

// Log resource group
wizardContext.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['useExistingResourceGroupInfoItem', activitySuccessContext]),
label: localize('useResourceGroup', 'Using resource group "{0}"', wizardContext.resourceGroup.name),
iconPath: activityInfoIcon
})
);
ext.outputChannel.appendLog(localize('usingResourceGroup', 'Using resource group "{0}".', wizardContext.resourceGroup.name));

// Log container app
wizardContext.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['useExistingContainerAppInfoItem', activitySuccessContext]),
label: localize('useContainerApp', 'Using container app "{0}"', wizardContext.containerApp?.name),
iconPath: activityInfoIcon
})
);
ext.outputChannel.appendLog(localize('usingContainerApp', 'Using container app "{0}".', wizardContext.containerApp?.name));

await LocationListStep.setLocation(wizardContext, item.containerApp.location);

const wizard: AzureWizard<ContainerAppDeployContext> = new AzureWizard(wizardContext, {
title: localize('deployContainerAppTitle', 'Deploy image to container app'),
promptSteps: [
new ContainerAppDeployStartingResourcesLogStep(),
new ImageSourceListStep(),
],
executeSteps: [
Expand Down
35 changes: 3 additions & 32 deletions src/commands/deployImage/deployImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { type ResourceGroup } from "@azure/arm-resources";
import { LocationListStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { activityInfoIcon, activitySuccessContext, AzureWizard, createSubscriptionContext, createUniversallyUniqueContextValue, GenericTreeItem, nonNullValue, nonNullValueAndProp, type IActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils";
import { ext } from "../../extensionVariables";
import { AzureWizard, createSubscriptionContext, nonNullValueAndProp, type IActionContext, type ISubscriptionContext } from "@microsoft/vscode-azext-utils";
import { type ContainerItem } from "../../tree/containers/ContainerItem";
import { createActivityContext } from "../../utils/activityUtils";
import { isAzdExtensionInstalled } from "../../utils/azdUtils";
Expand All @@ -16,6 +13,7 @@ import { localize } from "../../utils/localize";
import { getParentResource } from "../../utils/revisionDraftUtils";
import { ContainerAppOverwriteConfirmStep } from "../ContainerAppOverwriteConfirmStep";
import { showContainerAppNotification } from "../createContainerApp/showContainerAppNotification";
import { ContainerAppDeployStartingResourcesLogStep } from "../deployContainerApp/ContainerAppDeployStartingResourcesLogStep";
import { ContainerAppUpdateStep } from "../image/imageSource/ContainerAppUpdateStep";
import { ImageSourceListStep } from "../image/imageSource/ImageSourceListStep";
import { type ContainerRegistryImageSourceContext } from "../image/imageSource/containerRegistry/ContainerRegistryImageSourceContext";
Expand All @@ -40,38 +38,11 @@ export async function deployImage(context: IActionContext & Partial<ContainerReg
wizardContext.telemetry.properties.isAzdExtensionInstalled = 'true';
}

const resourceGroups: ResourceGroup[] = await ResourceGroupListStep.getResourceGroups(wizardContext);
wizardContext.resourceGroup = nonNullValue(
resourceGroups.find(rg => rg.name === containerApp.resourceGroup),
localize('containerAppResourceGroup', 'Expected to find the container app\'s resource group.'),
);

// Log resource group
wizardContext.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['useExistingResourceGroupInfoItem', activitySuccessContext]),
label: localize('useResourceGroup', 'Using resource group "{0}"', wizardContext.resourceGroup.name),
iconPath: activityInfoIcon
})
);
ext.outputChannel.appendLog(localize('usingResourceGroup', 'Using resource group "{0}".', wizardContext.resourceGroup.name));

// Log container app
wizardContext.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['useExistingContainerAppInfoItem', activitySuccessContext]),
label: localize('useContainerApp', 'Using container app "{0}"', wizardContext.containerApp?.name),
iconPath: activityInfoIcon
})
);
ext.outputChannel.appendLog(localize('usingContainerApp', 'Using container app "{0}".', wizardContext.containerApp?.name));

await LocationListStep.setLocation(wizardContext, containerApp.location);

const parentResourceName: string = getParentResource(containerApp, node.revision).name ?? containerApp.name;
const wizard: AzureWizard<DeployImageContext> = new AzureWizard(wizardContext, {
title: localize('deployImageTitle', 'Deploy image to "{0}"', parentResourceName),
promptSteps: [
new ContainerAppDeployStartingResourcesLogStep(),
new ImageSourceListStep(),
new ContainerAppOverwriteConfirmStep(),
],
Expand Down

0 comments on commit a8c57c3

Please sign in to comment.