This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
eventUtils.go
142 lines (120 loc) · 4.57 KB
/
eventUtils.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
package api
import (
"context"
"net/http"
"strings"
"time"
"github.com/keptn/go-utils/pkg/api/models"
v2 "github.com/keptn/go-utils/pkg/api/utils/v2"
"github.com/keptn/go-utils/pkg/common/httputils"
)
type EventsV1Interface interface {
// GetEvents returns all events matching the properties in the passed filter object.
GetEvents(filter *EventFilter) ([]*models.KeptnContextExtendedCE, *models.Error)
// GetEventsWithRetry tries to retrieve events matching the passed filter.
GetEventsWithRetry(filter *EventFilter, maxRetries int, retrySleepTime time.Duration) ([]*models.KeptnContextExtendedCE, error)
}
// EventHandler handles services
type EventHandler struct {
eventHandler *v2.EventHandler
BaseURL string
AuthToken string
AuthHeader string
HTTPClient *http.Client
Scheme string
}
// EventFilter allows to filter events based on the provided properties
type EventFilter struct {
Project string
Stage string
Service string
EventType string
KeptnContext string
EventID string
PageSize string
NumberOfPages int
FromTime string
}
// NewEventHandler returns a new EventHandler
func NewEventHandler(baseURL string) *EventHandler {
return NewEventHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}
// NewEventHandlerWithHTTPClient returns a new EventHandler that uses the specified http.Client
func NewEventHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *EventHandler {
return &EventHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
HTTPClient: httpClient,
Scheme: "http",
eventHandler: v2.NewEventHandlerWithHTTPClient(baseURL, httpClient),
}
}
const mongodbDatastoreServiceBaseUrl = "mongodb-datastore"
// NewAuthenticatedEventHandler returns a new EventHandler that authenticates at the endpoint via the provided token
// Deprecated: use APISet instead
func NewAuthenticatedEventHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *EventHandler {
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Transport = wrapOtelTransport(getClientTransport(httpClient.Transport))
return createAuthenticatedEventHandler(baseURL, authToken, authHeader, httpClient, scheme)
}
func createAuthenticatedEventHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *EventHandler {
v2EventHandler := v2.NewAuthenticatedEventHandler(baseURL, authToken, authHeader, httpClient, scheme)
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, mongodbDatastoreServiceBaseUrl) {
baseURL += "/" + mongodbDatastoreServiceBaseUrl
}
return &EventHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Scheme: scheme,
eventHandler: v2EventHandler,
}
}
func (e *EventHandler) getBaseURL() string {
return e.BaseURL
}
func (e *EventHandler) getAuthToken() string {
return e.AuthToken
}
func (e *EventHandler) getAuthHeader() string {
return e.AuthHeader
}
func (e *EventHandler) getHTTPClient() *http.Client {
return e.HTTPClient
}
// GetEvents returns all events matching the properties in the passed filter object.
func (e *EventHandler) GetEvents(filter *EventFilter) ([]*models.KeptnContextExtendedCE, *models.Error) {
e.ensureHandlerIsSet()
return e.eventHandler.GetEvents(context.TODO(), toV2EventFilter(filter), v2.EventsGetEventsOptions{})
}
// GetEventsWithRetry tries to retrieve events matching the passed filter.
func (e *EventHandler) GetEventsWithRetry(filter *EventFilter, maxRetries int, retrySleepTime time.Duration) ([]*models.KeptnContextExtendedCE, error) {
e.ensureHandlerIsSet()
return e.eventHandler.GetEventsWithRetry(context.TODO(), toV2EventFilter(filter), maxRetries, retrySleepTime, v2.EventsGetEventsWithRetryOptions{})
}
func toV2EventFilter(filter *EventFilter) *v2.EventFilter {
return &v2.EventFilter{
Project: filter.Project,
Stage: filter.Stage,
Service: filter.Service,
EventType: filter.EventType,
KeptnContext: filter.KeptnContext,
EventID: filter.EventID,
PageSize: filter.PageSize,
NumberOfPages: filter.NumberOfPages,
FromTime: filter.FromTime,
}
}
func (e *EventHandler) ensureHandlerIsSet() {
if e.eventHandler != nil {
return
}
if e.AuthToken != "" {
e.eventHandler = v2.NewAuthenticatedEventHandler(e.BaseURL, e.AuthToken, e.AuthHeader, e.HTTPClient, e.Scheme)
} else {
e.eventHandler = v2.NewEventHandlerWithHTTPClient(e.BaseURL, e.HTTPClient)
}
}