/
event_bridge_scheduler.go
88 lines (79 loc) · 3 KB
/
event_bridge_scheduler.go
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
package projectService
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
awsScheduler "github.com/aws/aws-sdk-go/service/scheduler"
"github.com/dotenx/dotenx/ao-api/config"
"github.com/sirupsen/logrus"
)
func (ps *projectService) CreateEventBridgeScheduleForDomainRegistration(accountId, projectTag, domainName, operationId string) (err error) {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
scheduleName := fmt.Sprintf("dtx_domain_registration_%s", projectTag)
payloadMap := map[string]interface{}{
"account_id": accountId,
"project_tag": projectTag,
"domain_name": domainName,
"operation_id": operationId,
"schedule_name": scheduleName,
"auth_token": config.Configs.Secrets.EventSchedulerToken,
}
payloadBytes, _ := json.Marshal(payloadMap)
payloadStr := string(payloadBytes)
// Create a new EventBridge client
client := awsScheduler.New(session.New(), cfg)
_, err = client.CreateSchedule(&awsScheduler.CreateScheduleInput{
Name: aws.String(scheduleName),
State: aws.String(awsScheduler.ScheduleStateEnabled),
ScheduleExpression: aws.String(config.Configs.App.DomainRegistrationCheckRate),
FlexibleTimeWindow: &awsScheduler.FlexibleTimeWindow{
Mode: aws.String(awsScheduler.FlexibleTimeWindowModeOff),
},
Target: &awsScheduler.Target{
Arn: aws.String(config.Configs.Secrets.DomainRegistrationTargetArn),
RoleArn: aws.String(config.Configs.Secrets.EventSchedulerRoleArn),
Input: aws.String(payloadStr),
},
})
if err != nil {
logrus.Error("Error creating EventBridge schedule:", err.Error())
return
}
return
}
func EventBridgeScheduleExists(scheduleName string) (bool, error) {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
// Create a new EventBridge client
client := awsScheduler.New(session.New(), cfg)
// Prepare input parameters for GetSchedule.
params := &awsScheduler.GetScheduleInput{
Name: &scheduleName,
}
// Check if the schedule exists.
_, err := client.GetSchedule(params)
if err != nil {
// If the schedule does not exist, check if the error is due to "ResourceNotFoundException".
// If it is, it means the schedule does not exist.
if _, ok := err.(*awsScheduler.ResourceNotFoundException); ok {
return false, nil
}
// If it's another error, return it.
return false, err
}
// If the schedule exists, return true.
return true, nil
}