-
Notifications
You must be signed in to change notification settings - Fork 11
/
action_triggered_event_handler.go
59 lines (47 loc) · 2.06 KB
/
action_triggered_event_handler.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
package handler
import (
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
"github.com/keptn/go-utils/pkg/sdk"
"time"
)
type ActionTriggeredEventHandler struct {
}
func NewActionTriggeredEventHandler() *ActionTriggeredEventHandler {
return &ActionTriggeredEventHandler{}
}
// Execute handles action.triggered events
// TODO: Add in your handler code
func (g *ActionTriggeredEventHandler) Execute(k sdk.IKeptn, event sdk.KeptnEvent) (interface{}, *sdk.Error) {
k.Logger().Infof("Handling Action Triggered Event: %s", event.ID)
actionTriggeredEvent := &keptnv2.ActionTriggeredEventData{}
if err := keptnv2.Decode(event.Data, actionTriggeredEvent); err != nil {
return nil, &sdk.Error{Err: err, StatusType: keptnv2.StatusErrored, ResultType: keptnv2.ResultFailed, Message: "failed to decode action.triggered event: " + err.Error()}
}
k.Logger().Infof("Action=%s", actionTriggeredEvent.Action.Action)
// check if action is supported
if actionTriggeredEvent.Action.Action == "action-xyz" {
k.Logger().Info("Action remediation triggered")
// -----------------------------------------------------
// TODO: Implement your remediation action here
// -----------------------------------------------------
time.Sleep(1 * time.Second) // Example: Wait 5 seconds. Maybe the problem fixes itself.
// Return finished event
finishedEventData := getActionFinishedEvent(keptnv2.ResultPass, keptnv2.StatusSucceeded, *actionTriggeredEvent, "")
return finishedEventData, nil
}
k.Logger().Infof("Retrieved unknown action %s, skipping...", actionTriggeredEvent.Action.Action)
return nil, nil
}
func getActionFinishedEvent(result keptnv2.ResultType, status keptnv2.StatusType, actionTriggeredEvent keptnv2.ActionTriggeredEventData, message string) keptnv2.ActionFinishedEventData {
return keptnv2.ActionFinishedEventData{
EventData: keptnv2.EventData{
Project: actionTriggeredEvent.Project,
Stage: actionTriggeredEvent.Stage,
Service: actionTriggeredEvent.Service,
Labels: actionTriggeredEvent.Labels,
Status: status,
Result: result,
Message: message,
},
}
}