|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import { getLogger } from '../../../shared/logger/logger' |
| 8 | +import { EventBridgeSchedulerService, ScheduleConfig } from '../eventBridgeSchedulerService' |
| 9 | +import { showQuickPick, showInputBox } from '../../../shared/ui/pickerPrompter' |
| 10 | +import { createQuickStartUrl } from '../../../shared/utilities/workspaceUtils' |
| 11 | + |
| 12 | +/** |
| 13 | + * Command to create a new EventBridge Scheduler schedule |
| 14 | + * |
| 15 | + * This command guides users through creating schedules for automated task execution. |
| 16 | + * EventBridge Scheduler supports various target types including Lambda functions, |
| 17 | + * SQS queues, SNS topics, and Step Functions state machines. |
| 18 | + * |
| 19 | + * Features: |
| 20 | + * - Support for cron and rate expressions |
| 21 | + * - Flexible time windows for fault tolerance |
| 22 | + * - Multiple target integrations |
| 23 | + * - Timezone support for cron schedules |
| 24 | + */ |
| 25 | +export async function createEventBridgeSchedule(): Promise<void> { |
| 26 | + const logger = getLogger() |
| 27 | + logger.info('Starting EventBridge Scheduler create schedule workflow') |
| 28 | + |
| 29 | + try { |
| 30 | + const schedulerService = new EventBridgeSchedulerService() |
| 31 | + |
| 32 | + // Get schedule name |
| 33 | + const scheduleName = await showInputBox({ |
| 34 | + title: 'Schedule Name', |
| 35 | + placeholder: 'my-daily-backup-schedule', |
| 36 | + validateInput: (input) => { |
| 37 | + if (!input || input.trim().length === 0) { |
| 38 | + return 'Schedule name is required' |
| 39 | + } |
| 40 | + if (input.length > 64) { |
| 41 | + return 'Schedule name must be 64 characters or fewer' |
| 42 | + } |
| 43 | + if (!/^[a-zA-Z0-9\-_]+$/.test(input)) { |
| 44 | + return 'Schedule name can only contain letters, numbers, hyphens, and underscores' |
| 45 | + } |
| 46 | + return undefined |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + if (!scheduleName) { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // Get schedule type |
| 55 | + const scheduleType = await showQuickPick([ |
| 56 | + { label: 'Rate-based', detail: 'Run at regular intervals (every X minutes/hours/days)' }, |
| 57 | + { label: 'Cron-based', detail: 'Run based on cron expression (specific times/dates)' }, |
| 58 | + { label: 'One-time', detail: 'Run once at a specific date and time' } |
| 59 | + ], { |
| 60 | + title: 'Schedule Type', |
| 61 | + ignoreFocusOut: true |
| 62 | + }) |
| 63 | + |
| 64 | + if (!scheduleType) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + // Get schedule expression based on type |
| 69 | + let scheduleExpression: string |
| 70 | + switch (scheduleType.label) { |
| 71 | + case 'Rate-based': |
| 72 | + scheduleExpression = await getRateExpression() |
| 73 | + break |
| 74 | + case 'Cron-based': |
| 75 | + scheduleExpression = await getCronExpression() |
| 76 | + break |
| 77 | + case 'One-time': |
| 78 | + scheduleExpression = await getOneTimeExpression() |
| 79 | + break |
| 80 | + default: |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if (!scheduleExpression) { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // Get target type |
| 89 | + const targetType = await showQuickPick([ |
| 90 | + { label: 'lambda', detail: 'AWS Lambda function' }, |
| 91 | + { label: 'sqs', detail: 'Amazon SQS queue' }, |
| 92 | + { label: 'sns', detail: 'Amazon SNS topic' }, |
| 93 | + { label: 'stepfunctions', detail: 'AWS Step Functions state machine' }, |
| 94 | + { label: 'eventbridge', detail: 'Amazon EventBridge custom bus' } |
| 95 | + ], { |
| 96 | + title: 'Target Type', |
| 97 | + ignoreFocusOut: true |
| 98 | + }) |
| 99 | + |
| 100 | + if (!targetType) { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // For now, show a placeholder message |
| 105 | + await vscode.window.showInformationMessage( |
| 106 | + `EventBridge Scheduler integration is not yet fully implemented. ` + |
| 107 | + `Schedule "${scheduleName}" with expression "${scheduleExpression}" ` + |
| 108 | + `targeting ${targetType.label} would be created.`, |
| 109 | + 'View Documentation' |
| 110 | + ).then(async (selection) => { |
| 111 | + if (selection === 'View Documentation') { |
| 112 | + await schedulerService.openScheduleTypesDocumentation() |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + } catch (error) { |
| 117 | + logger.error('Failed to create EventBridge Scheduler schedule:', error) |
| 118 | + await vscode.window.showErrorMessage(`Failed to create schedule: ${error}`) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +async function getRateExpression(): Promise<string | undefined> { |
| 123 | + const interval = await showInputBox({ |
| 124 | + title: 'Rate Interval', |
| 125 | + placeholder: '5 minutes', |
| 126 | + prompt: 'Enter interval (e.g., "5 minutes", "1 hour", "2 days")', |
| 127 | + validateInput: (input) => { |
| 128 | + if (!input || !/^\d+\s+(minute|minutes|hour|hours|day|days)$/.test(input.trim())) { |
| 129 | + return 'Please enter a valid interval (e.g., "5 minutes", "1 hour", "2 days")' |
| 130 | + } |
| 131 | + return undefined |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + return interval ? `rate(${interval})` : undefined |
| 136 | +} |
| 137 | + |
| 138 | +async function getCronExpression(): Promise<string | undefined> { |
| 139 | + const cronExpr = await showInputBox({ |
| 140 | + title: 'Cron Expression', |
| 141 | + placeholder: '0 12 * * ? *', |
| 142 | + prompt: 'Enter cron expression (6 fields: minute hour day month day-of-week year)', |
| 143 | + validateInput: (input) => { |
| 144 | + if (!input || input.trim().split(/\s+/).length !== 6) { |
| 145 | + return 'Cron expression must have exactly 6 fields' |
| 146 | + } |
| 147 | + return undefined |
| 148 | + } |
| 149 | + }) |
| 150 | + |
| 151 | + return cronExpr ? `cron(${cronExpr})` : undefined |
| 152 | +} |
| 153 | + |
| 154 | +async function getOneTimeExpression(): Promise<string | undefined> { |
| 155 | + const datetime = await showInputBox({ |
| 156 | + title: 'One-time Schedule', |
| 157 | + placeholder: '2024-12-31T23:59:59', |
| 158 | + prompt: 'Enter date and time (ISO 8601 format: YYYY-MM-DDTHH:MM:SS)', |
| 159 | + validateInput: (input) => { |
| 160 | + if (!input || !input.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)) { |
| 161 | + return 'Please enter date in ISO 8601 format (YYYY-MM-DDTHH:MM:SS)' |
| 162 | + } |
| 163 | + return undefined |
| 164 | + } |
| 165 | + }) |
| 166 | + |
| 167 | + return datetime ? `at(${datetime})` : undefined |
| 168 | +} |
0 commit comments