forked from microsoft/vscode-azurefunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateScheduler.ts
More file actions
110 lines (90 loc) · 5.01 KB
/
createScheduler.ts
File metadata and controls
110 lines (90 loc) · 5.01 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type ILocationWizardContext, type IResourceGroupWizardContext, LocationListStep, ResourceGroupCreateStep, ResourceGroupListStep } from "@microsoft/vscode-azext-azureutils";
import { AzureWizard, AzureWizardExecuteStep, AzureWizardPromptStep, createSubscriptionContext, type ExecuteActivityContext, type IActionContext, type ISubscriptionActionContext, subscriptionExperience } from "@microsoft/vscode-azext-utils";
import { type AzureSubscription } from "@microsoft/vscode-azureresources-api";
import { DurableTaskProvider, DurableTaskSchedulersResourceType } from "../../constants";
import { ext } from '../../extensionVariables';
import { localize } from '../../localize';
import { type DurableTaskSchedulerClient } from "../../tree/durableTaskScheduler/DurableTaskSchedulerClient";
import { type DurableTaskSchedulerDataBranchProvider } from "../../tree/durableTaskScheduler/DurableTaskSchedulerDataBranchProvider";
import { createActivityContext } from "../../utils/activityUtils";
import { withCancellation } from "../../utils/cancellation";
import { type Progress } from "vscode";
interface ICreateSchedulerContext extends ISubscriptionActionContext, ILocationWizardContext, IResourceGroupWizardContext, ExecuteActivityContext {
subscription?: AzureSubscription;
schedulerName?: string;
}
class SchedulerNamingStep extends AzureWizardPromptStep<ICreateSchedulerContext> {
async prompt(wizardContext: ICreateSchedulerContext): Promise<void> {
wizardContext.schedulerName = await wizardContext.ui.showInputBox({
prompt: localize('schedulerNamingStepPrompt', 'Enter a name for the new scheduler')
})
}
shouldPrompt(wizardContext: ICreateSchedulerContext): boolean {
return !wizardContext.schedulerName;
}
}
class SchedulerCreationStep extends AzureWizardExecuteStep<ICreateSchedulerContext> {
priority: number = 1;
constructor(private readonly schedulerClient: DurableTaskSchedulerClient) {
super();
}
async execute(wizardContext: ICreateSchedulerContext, _: Progress<{ message?: string; increment?: number; }>): Promise<void> {
const location = await LocationListStep.getLocation(wizardContext);
const response = await this.schedulerClient.createScheduler(
wizardContext.subscription as AzureSubscription,
wizardContext.resourceGroup?.name as string,
location.name,
wizardContext.schedulerName as string
);
const status = await withCancellation(token => response.status.waitForCompletion(token), 1000 * 60 * 30);
if (status !== true) {
throw new Error(localize('schedulerCreationFailed', 'The scheduler could not be created.'));
}
}
shouldExecute(wizardContext: ICreateSchedulerContext): boolean {
return wizardContext.subscription !== undefined
&& wizardContext.resourceGroup !== undefined
&& wizardContext.schedulerName !== undefined;
}
}
export function createSchedulerCommandFactory(dataBranchProvider: DurableTaskSchedulerDataBranchProvider, schedulerClient: DurableTaskSchedulerClient) {
return async (actionContext: IActionContext, node?: { subscription: AzureSubscription }): Promise<void> => {
const subscription = node?.subscription ?? await subscriptionExperience(actionContext, ext.rgApiV2.resources.azureResourceTreeDataProvider);
const wizardContext: ICreateSchedulerContext =
{
subscription,
...actionContext,
...createSubscriptionContext(subscription),
...await createActivityContext()
};
const promptSteps: AzureWizardPromptStep<ICreateSchedulerContext>[] = [
new SchedulerNamingStep(),
new ResourceGroupListStep()
];
LocationListStep.addProviderForFiltering(wizardContext, DurableTaskProvider, DurableTaskSchedulersResourceType);
LocationListStep.addStep(wizardContext, promptSteps);
const wizard = new AzureWizard<ICreateSchedulerContext>(
wizardContext,
{
hideStepCount: true,
promptSteps,
executeSteps: [
new ResourceGroupCreateStep(),
new SchedulerCreationStep(schedulerClient)
],
title: localize('createSchedulerWizardTitle', 'Create Durable Task Scheduler')
});
await wizard.prompt();
wizardContext.activityTitle = localize('createSchedulerActivityTitle', 'Create Durable Task Scheduler \'{0}\'', wizardContext.schedulerName);
try {
await wizard.execute();
}
finally {
dataBranchProvider.refresh();
}
}
}