-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAddScaleRuleStep.ts
More file actions
93 lines (79 loc) · 4.26 KB
/
AddScaleRuleStep.ts
File metadata and controls
93 lines (79 loc) · 4.26 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { KnownActiveRevisionsMode, type ScaleRule } from "@azure/arm-appcontainers";
import { nonNullProp } from "@microsoft/vscode-azext-utils";
import type { Progress } from "vscode";
import { ScaleRuleTypes } from "../../../constants";
import { ext } from "../../../extensionVariables";
import type { RevisionsItemModel } from "../../../tree/revisionManagement/RevisionItem";
import { delay } from "../../../utils/delay";
import { localize } from "../../../utils/localize";
import { RevisionDraftUpdateBaseStep } from "../../revisionDraft/RevisionDraftUpdateBaseStep";
import type { IAddScaleRuleContext } from "./IAddScaleRuleContext";
export class AddScaleRuleStep<T extends IAddScaleRuleContext> extends RevisionDraftUpdateBaseStep<T> {
public priority: number = 200;
constructor(baseItem: RevisionsItemModel) {
super(baseItem);
}
public async execute(context: IAddScaleRuleContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
let adding: string | undefined;
let added: string | undefined;
if (context.containerApp.revisionsMode === KnownActiveRevisionsMode.Single) {
adding = localize('addingScaleRuleSingle', 'Add {0} rule "{1}" to container app "{2}" (draft)', context.ruleType, context.ruleName, context.containerApp.name);
added = localize('addedScaleRuleSingle', 'Added {0} rule "{1}" to container app "{2}" (draft).', context.ruleType, context.ruleName, context.containerApp.name);
} else {
adding = localize('addingScaleRuleMultiple', 'Add {0} rule "{1}" to revision "{2}" (draft)', context.ruleType, context.ruleName, this.baseItem.revision.name);
added = localize('addedScaleRuleMultiple', 'Added {0} rule "{1}" to revision "{2}" (draft)', context.ruleType, context.ruleName, this.baseItem.revision.name);
}
context.activityTitle = adding;
progress.report({ message: localize('addingRule', 'Adding scale rule...') });
this.revisionDraftTemplate.scale ||= {};
this.revisionDraftTemplate.scale.rules ||= [];
const scaleRule: ScaleRule = this.buildRule(context);
this.integrateRule(context, this.revisionDraftTemplate.scale.rules, scaleRule);
this.updateRevisionDraftWithTemplate();
// Artificial delay to make the activity log look like it's performing an action
await delay(1000);
ext.outputChannel.appendLog(added);
}
public shouldExecute(context: IAddScaleRuleContext): boolean {
return !!context.ruleName && !!context.ruleType;
}
private buildRule(context: IAddScaleRuleContext): ScaleRule {
const scaleRule: ScaleRule = { name: context.ruleName };
switch (context.ruleType) {
case ScaleRuleTypes.HTTP:
scaleRule.http = {
metadata: {
concurrentRequests: nonNullProp(context, 'concurrentRequests')
}
};
break;
case ScaleRuleTypes.Queue:
scaleRule.azureQueue = {
queueName: context.queueName,
queueLength: context.queueLength,
auth: [{ secretRef: context.secretRef, triggerParameter: context.triggerParameter }]
}
break;
default:
}
return scaleRule;
}
private integrateRule(context: IAddScaleRuleContext, scaleRules: ScaleRule[], scaleRule: ScaleRule): void {
switch (context.ruleType) {
case ScaleRuleTypes.HTTP:
// Portal only allows one HTTP rule per revision
const idx: number = scaleRules.findIndex((rule) => rule.http);
if (idx !== -1) {
scaleRules.splice(idx, 1);
}
break;
case ScaleRuleTypes.Queue:
default:
}
scaleRules.push(scaleRule);
}
}