Skip to content

Commit d95d633

Browse files
committed
First pass of fixes
1 parent 95b66b3 commit d95d633

File tree

10 files changed

+95
-46
lines changed

10 files changed

+95
-46
lines changed

src/commands/revisionDraft/RevisionDraftFileSystem.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
4949
return this.emitter.event;
5050
}
5151

52-
// Create
5352
createRevisionDraft(item: ContainerAppItem | RevisionsItemModel): void {
5453
const uri: Uri = this.buildUriFromItem(item);
5554
if (this.draftStore.has(uri.path)) {
@@ -70,7 +69,6 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
7069
this.fireSoon({ type: FileChangeType.Created, uri });
7170
}
7271

73-
// Read
7472
parseRevisionDraft(item: ContainerAppsItem): Template | undefined {
7573
const uri: URI = this.buildUriFromItem(item);
7674
if (!this.draftStore.has(uri.path)) {
@@ -110,7 +108,6 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
110108
}
111109
}
112110

113-
// Update
114111
async editRevisionDraft(item: ContainerAppItem | RevisionsItemModel): Promise<void> {
115112
const uri: Uri = this.buildUriFromItem(item);
116113
if (!this.draftStore.has(uri.path)) {
@@ -161,7 +158,6 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
161158
ext.state.notifyChildrenChanged(file.containerAppId);
162159
}
163160

164-
// Delete
165161
discardRevisionDraft(item: ContainerAppsItem): void {
166162
const uri: Uri = this.buildUriFromItem(item);
167163
if (!this.draftStore.has(uri.path)) {
@@ -176,7 +172,6 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
176172
this.fireSoon({ type: FileChangeType.Deleted, uri });
177173
}
178174

179-
// Helper
180175
private buildUriFromItem(item: ContainerAppsItem): Uri {
181176
return URI.parse(`${RevisionDraftFileSystem.scheme}:/${item.containerApp.name}.json`);
182177
}

src/commands/scaling/addScaleRule/AddScaleRuleStep.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
import { KnownActiveRevisionsMode, type ScaleRule } from "@azure/arm-appcontainers";
77
import { nonNullProp } from "@microsoft/vscode-azext-utils";
8-
import type { Progress } from "vscode";
98
import { ScaleRuleTypes } from "../../../constants";
109
import { ext } from "../../../extensionVariables";
1110
import type { RevisionsItemModel } from "../../../tree/revisionManagement/RevisionItem";
12-
import { delay } from "../../../utils/delay";
1311
import { localize } from "../../../utils/localize";
1412
import { RevisionDraftUpdateBaseStep } from "../../revisionDraft/RevisionDraftUpdateBaseStep";
1513
import type { IAddScaleRuleContext } from "./IAddScaleRuleContext";
@@ -21,31 +19,16 @@ export class AddScaleRuleStep<T extends IAddScaleRuleContext> extends RevisionDr
2119
super(baseItem);
2220
}
2321

24-
public async execute(context: IAddScaleRuleContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
25-
let adding: string | undefined;
26-
let added: string | undefined;
27-
if (context.containerApp.revisionsMode === KnownActiveRevisionsMode.Single) {
28-
adding = localize('addingScaleRuleSingle', 'Add {0} rule "{1}" to container app "{2}" (draft)', context.ruleType, context.ruleName, context.containerApp.name);
29-
added = localize('addedScaleRuleSingle', 'Added {0} rule "{1}" to container app "{2}" (draft).', context.ruleType, context.ruleName, context.containerApp.name);
30-
} else {
31-
adding = localize('addingScaleRuleMultiple', 'Add {0} rule "{1}" to revision "{2}" (draft)', context.ruleType, context.ruleName, this.baseItem.revision.name);
32-
added = localize('addedScaleRuleMultiple', 'Added {0} rule "{1}" to revision "{2}" (draft)', context.ruleType, context.ruleName, this.baseItem.revision.name);
33-
}
34-
35-
context.activityTitle = adding;
36-
progress.report({ message: localize('addingRule', 'Adding scale rule...') });
37-
22+
public async execute(context: IAddScaleRuleContext): Promise<void> {
3823
this.revisionDraftTemplate.scale ||= {};
3924
this.revisionDraftTemplate.scale.rules ||= [];
4025

41-
const scaleRule: ScaleRule = this.buildRule(context);
42-
this.integrateRule(context, this.revisionDraftTemplate.scale.rules, scaleRule);
26+
context.scaleRule = this.buildRule(context);
27+
this.integrateRule(context, this.revisionDraftTemplate.scale.rules, context.scaleRule);
4328
this.updateRevisionDraftWithTemplate();
4429

45-
// Artificial delay to make the activity log look like it's performing an action
46-
await delay(1000);
47-
48-
ext.outputChannel.appendLog(added);
30+
const resourceName = context.containerApp.revisionsMode === KnownActiveRevisionsMode.Single ? context.containerApp.name : this.baseItem.revision.name;
31+
ext.outputChannel.appendLog(localize('addedScaleRule', 'Added {0} rule "{1}" to "{2}" (draft)', context.ruleType, context.ruleName, resourceName));
4932
}
5033

5134
public shouldExecute(context: IAddScaleRuleContext): boolean {

src/commands/scaling/addScaleRule/IAddScaleRuleContext.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import type { ScaleRule } from "@azure/arm-appcontainers";
6+
import { ScaleRule } from "@azure/arm-appcontainers";
77
import type { ExecuteActivityContext } from "@microsoft/vscode-azext-utils";
88
import type { ContainerAppModel } from "../../../tree/ContainerAppItem";
99
import type { IContainerAppContext } from "../../IContainerAppContext";
1010

1111
export interface IAddScaleRuleContext extends IContainerAppContext, ExecuteActivityContext {
1212
// Make containerApp _required_
1313
containerApp: ContainerAppModel;
14-
15-
scaleRules: ScaleRule[];
14+
parentResourceName: string;
1615

1716
// Base Rule Properties
1817
ruleName?: string;
@@ -26,4 +25,6 @@ export interface IAddScaleRuleContext extends IContainerAppContext, ExecuteActiv
2625
queueLength?: number;
2726
secretRef?: string;
2827
triggerParameter?: string;
28+
29+
scaleRule?: ScaleRule;
2930
}

src/commands/scaling/addScaleRule/ScaleRuleNameStep.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { ContainerApp, ContainerAppsAPIClient, KnownActiveRevisionsMode, Revision, ScaleRule } from '@azure/arm-appcontainers';
67
import { AzureWizardPromptStep } from '@microsoft/vscode-azext-utils';
8+
import { createContainerAppsAPIClient } from '../../../utils/azureClients';
79
import { localize } from '../../../utils/localize';
810
import type { IAddScaleRuleContext } from './IAddScaleRuleContext';
911

@@ -13,22 +15,39 @@ export class ScaleRuleNameStep extends AzureWizardPromptStep<IAddScaleRuleContex
1315
public async prompt(context: IAddScaleRuleContext): Promise<void> {
1416
context.ruleName = (await context.ui.showInputBox({
1517
prompt: localize('scaleRuleNamePrompt', 'Enter a name for the new scale rule.'),
16-
validateInput: (name: string | undefined) => this.validateInput(context, name)
18+
validateInput: this.validateInput,
19+
asyncValidationTask: (name: string) => this.validateNameAvailable(context, name)
1720
})).trim();
1821
}
1922

2023
public shouldPrompt(context: IAddScaleRuleContext): boolean {
2124
return !context.ruleName;
2225
}
2326

24-
private validateInput(context: IAddScaleRuleContext, name: string | undefined): string | undefined {
27+
private validateInput(name: string | undefined): string | undefined {
2528
name = name ? name.trim() : '';
2629

2730
if (!/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(name)) {
2831
return localize('invalidChar', `A name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character.`);
2932
}
3033

31-
const scaleRuleExists: boolean = !!context.scaleRules?.some((rule) => {
34+
return undefined;
35+
}
36+
37+
private async validateNameAvailable(context: IAddScaleRuleContext, name: string): Promise<string | undefined> {
38+
const client: ContainerAppsAPIClient = await createContainerAppsAPIClient(context);
39+
const resourceGroupName: string = context.containerApp.resourceGroup;
40+
41+
let scaleRules: ScaleRule[] | undefined;
42+
if (context.containerApp.revisionsMode === KnownActiveRevisionsMode.Single) {
43+
const containerApp: ContainerApp = await client.containerApps.get(resourceGroupName, context.parentResourceName);
44+
scaleRules = containerApp.template?.scale?.rules ?? [];
45+
} else {
46+
const revision: Revision = await client.containerAppsRevisions.getRevision(resourceGroupName, context.containerApp.name, context.parentResourceName);
47+
scaleRules = revision.template?.scale?.rules ?? [];
48+
}
49+
50+
const scaleRuleExists: boolean = !!scaleRules?.some((rule) => {
3251
return rule.name?.length && rule.name === name;
3352
});
3453

src/commands/scaling/addScaleRule/addScaleRule.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { KnownActiveRevisionsMode, Scale } from "@azure/arm-appcontainers";
7-
import { AzureWizard, IActionContext, createSubscriptionContext, nonNullValueAndProp } from "@microsoft/vscode-azext-utils";
6+
import { Revision } from "@azure/arm-appcontainers";
7+
import { AzureWizard, IActionContext, createSubscriptionContext, nonNullProp } from "@microsoft/vscode-azext-utils";
8+
import { ContainerAppModel } from "../../../tree/ContainerAppItem";
89
import type { ScaleRuleGroupItem } from "../../../tree/scaling/ScaleRuleGroupItem";
910
import { createActivityContext } from "../../../utils/activityUtils";
1011
import { localize } from "../../../utils/localize";
1112
import { pickScaleRuleGroup } from "../../../utils/pickItem/pickScale";
13+
import { getParentResource } from "../../../utils/revisionDraftUtils";
1214
import { AddScaleRuleStep } from "./AddScaleRuleStep";
1315
import type { IAddScaleRuleContext } from "./IAddScaleRuleContext";
1416
import { ScaleRuleNameStep } from "./ScaleRuleNameStep";
@@ -18,21 +20,15 @@ export async function addScaleRule(context: IActionContext, node?: ScaleRuleGrou
1820
const item: ScaleRuleGroupItem = node ?? await pickScaleRuleGroup(context, { autoSelectDraft: true });
1921
const { subscription, containerApp, revision } = item;
2022

21-
// Branching path reasoning: https://github.com/microsoft/vscode-azurecontainerapps/blob/main/src/commands/revisionDraft/README.md
22-
let scale: Scale | undefined;
23-
if (containerApp.revisionsMode === KnownActiveRevisionsMode.Single) {
24-
scale = nonNullValueAndProp(containerApp.template, 'scale');
25-
} else {
26-
scale = nonNullValueAndProp(revision.template, 'scale');
27-
}
23+
const parentResource: ContainerAppModel | Revision = getParentResource(containerApp, revision);
2824

2925
const wizardContext: IAddScaleRuleContext = {
3026
...context,
3127
...createSubscriptionContext(subscription),
3228
...await createActivityContext(),
3329
containerApp,
3430
subscription,
35-
scaleRules: scale.rules ?? [],
31+
parentResourceName: nonNullProp(parentResource, 'name')
3632
};
3733

3834
const wizard: AzureWizard<IAddScaleRuleContext> = new AzureWizard(wizardContext, {
@@ -43,5 +39,8 @@ export async function addScaleRule(context: IActionContext, node?: ScaleRuleGrou
4339
});
4440

4541
await wizard.prompt();
42+
43+
wizardContext.activityTitle = localize('addScaleRuleTitle', 'Add {0} rule "{1}" to "{2}" (draft)', wizardContext.ruleType, wizardContext.ruleName, parentResource.name);
44+
4645
await wizard.execute();
4746
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
/*---------------------------------------------------------------------------------------------
7+
* Copyright (c) Microsoft Corporation. All rights reserved.
8+
* Licensed under the MIT License. See License.txt in the project root for license information.
9+
*--------------------------------------------------------------------------------------------*/
10+
11+
import { Revision } from "@azure/arm-appcontainers";
12+
import { TreeElementBase } from "@microsoft/vscode-azext-utils";
13+
import { AzureSubscription } from "@microsoft/vscode-azureresources-api";
14+
import { TreeItem } from "vscode";
15+
import { ContainerAppModel } from "../ContainerAppItem";
16+
import { RevisionsItemModel } from "./RevisionItem";
17+
18+
export abstract class RevisionDraftModel implements RevisionsItemModel {
19+
constructor(readonly subscription: AzureSubscription, readonly containerApp: ContainerAppModel, readonly revision: Revision) {
20+
// Have to call a separate method here because abstract methods cannot be called directly within the constructor
21+
this.initItemModel();
22+
}
23+
24+
private initItemModel(): void {
25+
this.hasUnsavedChanges() ? this.setPropertiesDraft() : this.setProperties();
26+
}
27+
28+
abstract getTreeItem(): Promise<TreeItem>;
29+
abstract getChildren(): Promise<TreeElementBase[]>;
30+
31+
abstract hasUnsavedChanges: () => boolean | Promise<boolean>;
32+
abstract setProperties(): void;
33+
abstract setPropertiesDraft(): void;
34+
}

src/tree/scaling/ScaleItem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as deepEqual from 'deep-eql';
1010
import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode";
1111
import { ext } from "../../extensionVariables";
1212
import { localize } from "../../utils/localize";
13+
import { getParentResource } from "../../utils/revisionDraftUtils";
1314
import { treeUtils } from "../../utils/treeUtils";
1415
import type { ContainerAppModel } from "../ContainerAppItem";
1516
import type { TreeElementBase } from "../ContainerAppsBranchDataProvider";
@@ -54,7 +55,7 @@ export class ScaleItem implements RevisionsItemModel, RevisionsDraftModel {
5455
}
5556

5657
private get parentResource(): ContainerAppModel | Revision {
57-
return this.containerApp.revisionsMode === KnownActiveRevisionsMode.Single ? this.containerApp : this.revision;
58+
return getParentResource(this.containerApp, this.revision);
5859
}
5960

6061
getTreeItem(): TreeItem {

src/tree/scaling/ScaleRuleGroupItem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as deepEqual from "deep-eql";
1010
import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode";
1111
import { ext } from "../../extensionVariables";
1212
import { localize } from "../../utils/localize";
13+
import { getParentResource } from "../../utils/revisionDraftUtils";
1314
import type { ContainerAppModel } from "../ContainerAppItem";
1415
import { RevisionDraftItem, RevisionsDraftModel } from "../revisionManagement/RevisionDraftItem";
1516
import type { RevisionsItemModel } from "../revisionManagement/RevisionItem";
@@ -50,7 +51,7 @@ export class ScaleRuleGroupItem implements RevisionsItemModel, RevisionsDraftMod
5051
}
5152

5253
private get parentResource(): ContainerAppModel | Revision {
53-
return this.containerApp.revisionsMode === KnownActiveRevisionsMode.Single ? this.containerApp : this.revision;
54+
return getParentResource(this.containerApp, this.revision);
5455
}
5556

5657
getTreeItem(): TreeItem {

src/tree/scaling/ScaleRuleItem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { AzureSubscription, ViewPropertiesModel } from "@microsoft/vscode-a
88
import * as deepEqual from "deep-eql";
99
import { ThemeIcon, TreeItem } from "vscode";
1010
import { localize } from "../../utils/localize";
11+
import { getParentResource } from "../../utils/revisionDraftUtils";
1112
import type { ContainerAppModel } from "../ContainerAppItem";
1213
import { RevisionDraftItem, RevisionsDraftModel } from "../revisionManagement/RevisionDraftItem";
1314
import type { RevisionsItemModel } from "../revisionManagement/RevisionItem";
@@ -47,7 +48,7 @@ export class ScaleRuleItem implements RevisionsItemModel, RevisionsDraftModel {
4748
}
4849

4950
private get parentResource(): ContainerAppModel | Revision {
50-
return this.containerApp.revisionsMode === KnownActiveRevisionsMode.Single ? this.containerApp : this.revision;
51+
return getParentResource(this.containerApp, this.revision);
5152
}
5253

5354
getTreeItem(): TreeItem {

src/utils/revisionDraftUtils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.md in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { KnownActiveRevisionsMode, Revision } from "@azure/arm-appcontainers";
7+
import { ContainerAppModel } from "../tree/ContainerAppItem";
8+
9+
/**
10+
* Use to always select the correct parent resource model
11+
* https://github.com/microsoft/vscode-azurecontainerapps/blob/main/src/commands/revisionDraft/README.md
12+
*/
13+
export function getParentResource(containerApp: ContainerAppModel, revision: Revision): ContainerAppModel | Revision {
14+
return containerApp.revisionsMode === KnownActiveRevisionsMode.Single ? containerApp : revision;
15+
}

0 commit comments

Comments
 (0)