|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +################################################################### |
| 4 | +# # @script aws_scheduler.sh |
| 5 | +# # @author lamhaison |
| 6 | +# # @description aws_scheduler.sh is a script to manage AWS Scheduler resources. |
| 7 | + |
| 8 | +################################################################### |
| 9 | + |
| 10 | +function aws_scheduler_list() { |
| 11 | + aws_run_commandline "\ |
| 12 | + aws scheduler list-schedules \ |
| 13 | + --query 'Schedules[*].[Name,GroupName,State,ScheduleExpression]' --output table |
| 14 | + " |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | +function aws_scheduler_get() { |
| 19 | + local schedule_name=$1 |
| 20 | + |
| 21 | + if [[ -z "$schedule_name" ]]; then |
| 22 | + echo "Usage: aws_scheduler_get <schedule_name>" |
| 23 | + return 1 |
| 24 | + fi |
| 25 | + |
| 26 | + aws_run_commandline "\ |
| 27 | + aws scheduler get-schedule \ |
| 28 | + --name \"$schedule_name\" \ |
| 29 | + " |
| 30 | +} |
| 31 | + |
| 32 | +function aws_scheduler_get_with_hint() { |
| 33 | + local schedule_name=$(peco_create_menu 'peco_aws_scheduler_list' '--prompt "Choose scheduler name >"') |
| 34 | + aws_scheduler_get "$schedule_name" |
| 35 | +} |
| 36 | + |
| 37 | +function aws_scheduler_disable_schedule() { |
| 38 | + local schedule_name=$1 |
| 39 | + |
| 40 | + if [[ -z "$schedule_name" ]]; then |
| 41 | + echo "Usage: aws_scheduler_disable_schedule <schedule_name>" |
| 42 | + return 1 |
| 43 | + fi |
| 44 | + |
| 45 | + # Get the current schedule details |
| 46 | + local schedule_detail |
| 47 | + schedule_detail=$(aws scheduler get-schedule --name "$schedule_name" 2>/dev/null) |
| 48 | + if [[ $? -ne 0 || -z "$schedule_detail" ]]; then |
| 49 | + echo "Failed to get schedule details for '$schedule_name'" |
| 50 | + return 1 |
| 51 | + fi |
| 52 | + |
| 53 | + # Extract required fields from the schedule detail |
| 54 | + local schedule_expression |
| 55 | + local flexible_time_window |
| 56 | + local target |
| 57 | + |
| 58 | + schedule_expression=$(echo "$schedule_detail" | jq -c -r '.ScheduleExpression') |
| 59 | + flexible_time_window=$(echo "$schedule_detail" | jq -c -r '.FlexibleTimeWindow') |
| 60 | + target=$(echo "$schedule_detail" | jq -c -r '.Target') |
| 61 | + |
| 62 | + if [[ -z "$schedule_expression" || -z "$flexible_time_window" || -z "$target" ]]; then |
| 63 | + echo "Failed to extract required fields from schedule details." |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + |
| 67 | + aws_run_commandline "\ |
| 68 | + aws scheduler update-schedule \ |
| 69 | + --name \"$schedule_name\" \ |
| 70 | + --state DISABLED \ |
| 71 | + --flexible-time-window '$flexible_time_window' \ |
| 72 | + --schedule-expression '$schedule_expression' \ |
| 73 | + --target '$target' |
| 74 | + " |
| 75 | +} |
| 76 | + |
| 77 | +function aws_scheduler_disable_schedule_with_hint() { |
| 78 | + local schedule_name=$(peco_create_menu 'peco_aws_scheduler_list' '--prompt "Choose scheduler name >"') |
| 79 | + aws_scheduler_disable_schedule "$schedule_name" |
| 80 | +} |
| 81 | + |
| 82 | +function aws_scheduler_enable_schedule() { |
| 83 | + local schedule_name=$1 |
| 84 | + |
| 85 | + if [[ -z "$schedule_name" ]]; then |
| 86 | + echo "Usage: aws_scheduler_enable_schedule <schedule_name>" |
| 87 | + return 1 |
| 88 | + fi |
| 89 | + |
| 90 | + aws_run_commandline "\ |
| 91 | + aws scheduler update-schedule \ |
| 92 | + --name \"$schedule_name\" \ |
| 93 | + --state ENABLED |
| 94 | + " |
| 95 | +} |
0 commit comments