-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VEGA-2332 - emit lpa updated event (#113)
* Create client to send events to aws eventbridge vega-2332#minor * Unit test eventBridge client and add relevant modules * Send event when lpa is created Adds config module from aws-sdk-go but might not need it depending on how we load aws config into the client, eg if we use env vars to store credentials and endpoint. Build fails as I need to work out how to import the event package into the lambda * Send event when lpa is updated and refactor * Refactor client unit tests * Fix update lambda unit tests * Add test for failed event update vega-2332#minor * Point eventbridge client to endpoint in env vars * Code review refactorings of renaming and testing * Reinstate package vers messed by rebase
- Loading branch information
Showing
9 changed files
with
296 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package event | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
|
||
"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" | ||
) | ||
|
||
const source = "opg.poas.lpastore" | ||
|
||
type EventBridgeClient interface { | ||
PutEvents(ctx context.Context, params *eventbridge.PutEventsInput, optFns ...func(*eventbridge.Options)) (*eventbridge.PutEventsOutput, error) | ||
} | ||
|
||
type Client struct { | ||
eventBusName string | ||
svc EventBridgeClient | ||
} | ||
|
||
func NewClient(cfg aws.Config, eventBusName string) *Client { | ||
return &Client{ | ||
svc: eventbridge.NewFromConfig(cfg, func (o *eventbridge.Options) { | ||
o.BaseEndpoint = aws.String(os.Getenv("AWS_EVENTBRIDGE_ENDPOINT")) | ||
}), | ||
eventBusName: eventBusName, | ||
} | ||
} | ||
|
||
func (c *Client) SendLpaUpdated(ctx context.Context, event LpaUpdated) error { | ||
return c.send(ctx, "lpa-updated", event) | ||
} | ||
|
||
func (c *Client) send(ctx context.Context, eventType string, detail any) error { | ||
|
||
v, err := json.Marshal(detail) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.svc.PutEvents(ctx, &eventbridge.PutEventsInput{ | ||
Entries: []types.PutEventsRequestEntry{{ | ||
EventBusName: aws.String(c.eventBusName), | ||
Source: aws.String(source), | ||
DetailType: aws.String(eventType), | ||
Detail: aws.String(string(v)), | ||
}}, | ||
}) | ||
|
||
return err | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package event | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"testing" | ||
|
||
"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/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type mockEventBridgeClient struct { | ||
mock.Mock | ||
} | ||
|
||
func (_m *mockEventBridgeClient) PutEvents(ctx context.Context, params *eventbridge.PutEventsInput, optFns ...func(*eventbridge.Options)) (*eventbridge.PutEventsOutput, error) { | ||
var r0 *eventbridge.PutEventsOutput | ||
var r1 error = errors.New("err") | ||
|
||
return r0, r1 | ||
} | ||
|
||
func TestClientSendEvent(t *testing.T) { | ||
ctx := context.Background() | ||
expectedError := errors.New("err") | ||
|
||
event := LpaUpdated{ Uid: "M-1234-1234-1234", ChangeType: "CREATED" } | ||
data, _ := json.Marshal(event) | ||
|
||
mockClient := &mockEventBridgeClient{} | ||
mockClient.On("PutEvents", mock.Anything, &eventbridge.PutEventsInput{ | ||
Entries: []types.PutEventsRequestEntry{{ | ||
EventBusName: aws.String("my-bus"), | ||
Source: aws.String("opg.poas.lpastore"), | ||
DetailType: aws.String("lpa-updated"), | ||
Detail: aws.String(string(data)), | ||
}}, | ||
}). | ||
Return(nil, expectedError) | ||
|
||
svc := &Client{svc: mockClient, eventBusName: "my-bus"} | ||
err := svc.SendLpaUpdated(ctx, event) | ||
|
||
assert.Equal(t, expectedError, err) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package event | ||
|
||
type LpaUpdated struct { | ||
Uid string `json:"uid"` | ||
ChangeType string `json:"changeType"` | ||
} |
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
Oops, something went wrong.