Created
May 22, 2023 13:33
-
-
Save huseyinbabal/67c7b4896217e8b8147b6293b863305a to your computer and use it in GitHub Desktop.
Keptn Client Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// client.go | |
package keptn | |
import ( | |
api "github.com/keptn/go-utils/pkg/api/utils/v2" | |
"time" | |
) | |
// Client Keptn client | |
type Client struct { | |
// API refers to Keptn client. https://github.com/keptn/go-utils | |
API *api.APISet | |
} | |
type GetEventsRequest struct { | |
Project string | |
FromTime time.Time | |
} | |
type Event struct { | |
ID string | |
Source string | |
Type string | |
Data Data | |
} | |
type Data struct { | |
Message string | |
Project string | |
Service string | |
Status string | |
Stage string | |
Result string | |
} | |
... | |
// NewClient initializes Keptn client | |
func NewClient(url, token string) (*Client, error) { | |
client, err := api.New(url, api.WithAuthToken(token)) | |
if err != nil { | |
return nil, err | |
} | |
return &Client{ | |
API: client, | |
}, nil | |
} | |
// Events returns only new events. | |
func (c *Client) Events(ctx context.Context, request *GetEventsRequest) ([]Event, error) { | |
fromTime := request.FromTime.UTC().Format(time.RFC3339) | |
var events []Event | |
res, err := c.API.Events().GetEvents(ctx, &api.EventFilter{ | |
Project: request.Project, | |
FromTime: fromTime, | |
}, api.EventsGetEventsOptions{}) | |
if err != nil { | |
return nil, err.ToError() | |
} | |
for _, ev := range res { | |
data := Data{} | |
err := ev.DataAs(&data) | |
if err != nil { | |
return nil, err | |
} | |
events = append(events, Event{ | |
ID: ev.ID, | |
Source: *ev.Source, | |
Type: *ev.Type, | |
Data: data, | |
}) | |
} | |
return events, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment