forked from spezam/eventbridge-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventbridge.go
164 lines (141 loc) · 3.94 KB
/
eventbridge.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/service/eventbridge/types"
"github.com/fatih/color"
)
type eventbridgeClient struct {
client *eventbridge.Client
eventBusName string
}
func newEventbridgeClient(cfg aws.Config, eventBusName string) *eventbridgeClient {
return &eventbridgeClient{
client: eventbridge.NewFromConfig(cfg),
eventBusName: eventBusName,
}
}
func (e *eventbridgeClient) testEventPattern(ctx context.Context, inputEvent, eventRule string) error {
// list eventbridge rules filtered by prefix
resp, err := e.client.ListRules(ctx, &eventbridge.ListRulesInput{
EventBusName: &e.eventBusName,
NamePrefix: aws.String(eventRule),
})
if err != nil {
return err
}
if len(resp.Rules) < 1 {
log.Printf("no event rule with prefix: %s", eventRule)
return nil
}
log.Printf("event rules matching the event:")
for _, r := range resp.Rules {
// skip schedule rules since EventPattern is nil
if r.EventPattern == nil {
continue
}
res, err := e.client.TestEventPattern(ctx, &eventbridge.TestEventPatternInput{
Event: aws.String(inputEvent),
EventPattern: r.EventPattern,
})
if err != nil {
return err
}
if res.Result == false {
log.Printf("%s: %s", *r.Name, color.RedString("✘"))
continue
}
log.Printf("%s: %s", *r.Name, color.GreenString("✔"))
}
return nil
}
func (e *eventbridgeClient) createRule(ctx context.Context, eventPattern string) (string, error) {
res, err := e.client.PutRule(ctx, &eventbridge.PutRuleInput{
Name: aws.String(namespace + "-" + runID),
Description: aws.String(fmt.Sprintf("[%s] temp rule", namespace)),
EventBusName: aws.String(e.eventBusName),
EventPattern: aws.String(eventPattern),
State: types.RuleStateEnabled,
})
if err != nil {
log.Printf("eventbridge.CreateRule error: %s", err)
return "", err
}
return *res.RuleArn, nil
}
func (e *eventbridgeClient) deleteRule(ctx context.Context) error {
_, err := e.client.DeleteRule(ctx, &eventbridge.DeleteRuleInput{
EventBusName: aws.String(e.eventBusName),
Force: true,
Name: aws.String(namespace + "-" + runID),
})
if err != nil {
log.Printf("eventbridge.DeleteRule error: %s", err)
return err
}
return err
}
func (e *eventbridgeClient) putEvent(ctx context.Context, event string) error {
log.Printf("putting event: %s", event)
ev := struct {
Source string `json:"source"`
Detail string `json:"detail"`
DetailType string `json:"detail-type"`
}{}
err := json.Unmarshal([]byte(event), &ev)
if err != nil {
return err
}
resp, err := e.client.PutEvents(ctx, &eventbridge.PutEventsInput{
Entries: []types.PutEventsRequestEntry{
{
Source: aws.String(ev.Source),
Detail: aws.String(ev.Detail),
DetailType: aws.String(ev.DetailType),
EventBusName: aws.String(e.eventBusName),
},
},
})
if err != nil {
return err
}
if resp.FailedEntryCount > 0 {
return fmt.Errorf("%s", *resp.Entries[0].ErrorMessage)
}
return nil
}
func (e *eventbridgeClient) putTarget(ctx context.Context, sqsArn string) error {
_, err := e.client.PutTargets(ctx, &eventbridge.PutTargetsInput{
Rule: aws.String(namespace + "-" + runID),
EventBusName: aws.String(e.eventBusName),
Targets: []types.Target{
{
Id: aws.String(namespace + "-" + runID),
Arn: aws.String(sqsArn),
},
},
})
if err != nil {
log.Printf("eventbridge.PutTarget error %s", err)
return err
}
return err
}
func (e *eventbridgeClient) removeTarget(ctx context.Context) error {
_, err := e.client.RemoveTargets(ctx, &eventbridge.RemoveTargetsInput{
Ids: []string{
namespace + "-" + runID,
},
Rule: aws.String(namespace + "-" + runID),
EventBusName: aws.String(e.eventBusName),
})
if err != nil {
log.Printf("eventbridge.RemoveTarget error %s", err)
return err
}
return err
}