-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
115 lines (95 loc) · 3.07 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
yaml "gopkg.in/yaml.v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/aws/aws-sdk-go/service/lambda"
)
// Version of the maekawa
var Version = "0.5.1"
func main() {
var (
apply, dryrun, version bool
file, awsRegion, awsProfile string
err error
cweRulesBeforeApply, cweRulesAfterApply *cloudwatchevents.ListRulesOutput
)
flag.BoolVar(&apply, "apply", false, "apply to CloudWatch Events")
flag.BoolVar(&dryrun, "dry-run", false, "dry-run")
flag.BoolVar(&version, "version", false, "show maekawa version")
flag.BoolVar(&version, "v", false, "show maekawa version (shorthand)")
flag.StringVar(&file, "file", "config.yml", "file path to setting yaml")
flag.StringVar(&file, "f", "config.yml", "file path to setting yaml (shorthand)")
flag.StringVar(&awsRegion, "region", os.Getenv("AWS_REGION"), "aws region")
flag.StringVar(&awsProfile, "profile", os.Getenv("AWS_PROFILE"), "aws profile")
flag.Parse()
if version {
fmt.Printf("maekawa version %s\n", Version)
os.Exit(0)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(awsRegion),
},
Profile: awsProfile,
}))
cweClient := cloudwatchevents.New(sess)
lambdaClient := lambda.New(sess)
cweRulesBeforeApply, err = cweClient.ListRules(nil)
if err != nil {
fmt.Println("API error\n", err)
os.Exit(1)
}
describedRules := Rules{}
err = loadYaml(file, &describedRules)
if err != nil {
fmt.Println("File error\n", err)
os.Exit(1)
}
describedRules.Rules = AssociateRules(cweRulesBeforeApply.Rules, describedRules.Rules)
for i, rule := range describedRules.Rules {
t, _ := fetchActualTargetsByRule(cweClient, rule)
describedRules.Rules[i].Targets = AssociateTargets(t, describedRules.Rules[i].Targets)
}
CheckIsNeedUpdateOrDelete(describedRules.Rules)
displayWhatWillChange(describedRules.Rules)
if apply && !dryrun {
err = updateCloudWatchEvents(cweClient, describedRules.Rules)
if err != nil {
fmt.Println("API error\n", err)
os.Exit(1)
}
err = removePermissonFromLambda(lambdaClient, describedRules.Rules)
if err != nil {
fmt.Println("API error\n", err)
os.Exit(1)
}
// Grant permission to invoke lambda function from CloudWatch Events
cweRulesAfterApply, err = cweClient.ListRules(nil)
describedRules.Rules = AssociateRules(cweRulesAfterApply.Rules, describedRules.Rules)
for i, rule := range describedRules.Rules {
t, _ := fetchActualTargetsByRule(cweClient, rule)
describedRules.Rules[i].Targets = AssociateTargets(t, describedRules.Rules[i].Targets)
}
err = addPermissionToLambdaFromCloudWatchEvents(lambdaClient, describedRules.Rules)
if err != nil {
fmt.Println("Grant permission error\n", err)
}
}
}
func loadYaml(file string, r *Rules) error {
buf, err := ioutil.ReadFile(file)
if err != nil {
return err
}
err = yaml.Unmarshal(buf, &r)
if err != nil {
return err
}
return nil
}