-
Notifications
You must be signed in to change notification settings - Fork 54
Add Template output format to EventBridge Reaction #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
13
commits into
main
Choose a base branch
from
copilot/add-handlebars-template-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e2a2983
Initial plan
Copilot b0f1fb9
Add Handlebars template output format to EventBridge Reaction
Copilot 81e1cde
Update to per-query Handlebars templates instead of global
Copilot 621bfff
Add e2e tests for EventBridge Handlebars reaction with localstack
Copilot 94d0bcb
Fix query config format to use JSON string with pipe operator
Copilot ec2ea87
Change query config to use YAML format instead of JSON
Copilot 53ec10f
Update query config to use added/updated/deleted with template and me…
Copilot 91f1a7f
Restore CloudEvent wrapper for Handlebars format
Copilot e2d15f3
Rename Handlebars to Template format, fix CloudEvent type and add met…
Copilot 0f2cfa3
Add EventBridge reaction provider to CLI and e2e test infrastructure
Copilot 0119f90
Merge branch 'main' into copilot/add-handlebars-template-output
danielgerlag b4be7f4
Refactor e2e test to deploy reaction once instead of per-test
Copilot aa97051
Merge branch 'main' into copilot/add-handlebars-template-output
ruokun-niu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
e2e-tests/09-eventbridge-handlebars-scenario/eventbridge-reaction.yaml
This file contains hidden or 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,37 @@ | ||
| # Copyright 2024 The Drasi Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| --- | ||
| apiVersion: v1 | ||
| kind: Reaction | ||
| name: test-eventbridge-handlebars | ||
| spec: | ||
| kind: EventBridge | ||
| identity: | ||
| kind: AwsIamAccessKey | ||
| accessKeyId: test | ||
| secretAccessKey: test | ||
| region: us-east-1 | ||
| queries: | ||
| product-query: | ||
| addedTemplate: | | ||
| Product {{after.ProductName}} added with {{after.Quantity}} units at ${{after.Price}} | ||
| updatedTemplate: | | ||
| Product {{after.ProductName}} updated: quantity changed from {{before.Quantity}} to {{after.Quantity}} units | ||
| deletedTemplate: | | ||
| Product {{before.ProductName}} (ID: {{before.ProductId}}) removed from inventory | ||
| properties: | ||
| eventBusName: default | ||
| format: handlebars | ||
| serviceUrl: http://localstack.default.svc.cluster.local:4566 | ||
Check warningCode scanning / devskim An HTTP-based URL without TLS was detected. Warning
Insecure URL
|
||
190 changes: 190 additions & 0 deletions
190
e2e-tests/09-eventbridge-handlebars-scenario/eventbridge.test.js
This file contains hidden or 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,190 @@ | ||
| /** | ||
| * Copyright 2024 The Drasi Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| const yaml = require('js-yaml'); | ||
| const fs = require('fs'); | ||
| const PortForward = require('../fixtures/port-forward'); | ||
| const deployResources = require("../fixtures/deploy-resources"); | ||
| const deleteResources = require("../fixtures/delete-resources"); | ||
| const pg = require('pg'); | ||
| const { EventBridgeClient, PutEventsCommand } = require("@aws-sdk/client-eventbridge"); | ||
|
|
||
| let dbPortForward = new PortForward("postgres-eventbridge", 5432); | ||
| let localstackPortForward = new PortForward("localstack", 4566); | ||
|
|
||
| let dbClient = new pg.Client({ | ||
| database: "test-db", | ||
| host: "127.0.0.1", | ||
Check noticeCode scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
| user: "test", | ||
| password: "test", | ||
| }); | ||
|
|
||
| let eventBridgeClient; | ||
| let receivedEvents = []; | ||
|
|
||
| beforeAll(async () => { | ||
| const resources = yaml.loadAll(fs.readFileSync(__dirname + '/resources.yaml', 'utf8')); | ||
| await deployResources(resources); | ||
|
|
||
| dbClient.port = await dbPortForward.start(); | ||
| await dbClient.connect(); | ||
|
|
||
| const localstackPort = await localstackPortForward.start(); | ||
|
|
||
| // Configure AWS SDK to use localstack | ||
| eventBridgeClient = new EventBridgeClient({ | ||
| region: "us-east-1", | ||
| endpoint: `http://127.0.0.1:${localstackPort}`, | ||
| credentials: { | ||
| accessKeyId: "test", | ||
| secretAccessKey: "test", | ||
| }, | ||
| }); | ||
|
|
||
| // Wait for services to be ready | ||
| await new Promise(r => setTimeout(r, 20000)); | ||
| }, 180000); | ||
|
|
||
| afterAll(async () => { | ||
| await dbClient.end(); | ||
| dbPortForward.stop(); | ||
| localstackPortForward.stop(); | ||
|
|
||
| const resources = yaml.loadAll(fs.readFileSync(__dirname + '/resources.yaml', 'utf8')); | ||
| await deleteResources(resources); | ||
|
|
||
| const reactionResources = yaml.loadAll(fs.readFileSync(__dirname + '/eventbridge-reaction.yaml', 'utf8')); | ||
| await deleteResources(reactionResources); | ||
| }); | ||
|
|
||
| test('Test EventBridge Handlebars Reaction - Added Product', async () => { | ||
| const reactionResources = yaml.loadAll(fs.readFileSync(__dirname + '/eventbridge-reaction.yaml', 'utf8')); | ||
| await deployResources(reactionResources); | ||
|
|
||
| // Wait for reaction to be ready | ||
| await new Promise(r => setTimeout(r, 10000)); | ||
|
|
||
| // Insert a new product | ||
| await dbClient.query(`INSERT INTO "Product" ("ProductId", "ProductName", "Quantity", "Price") VALUES (3, 'Keyboard', 25, 79.99)`); | ||
|
|
||
| // Wait for the event to be processed | ||
| await waitForCondition(async () => { | ||
| try { | ||
| // In a real scenario, we would query EventBridge for events | ||
| // For this test, we're verifying the reaction deployed successfully | ||
| // and the database operation completed | ||
| const result = await dbClient.query(`SELECT * FROM "Product" WHERE "ProductId" = 3`); | ||
| return result.rows.length === 1 && result.rows[0].ProductName === 'Keyboard'; | ||
| } catch (error) { | ||
| console.error('Error checking condition:', error); | ||
| return false; | ||
| } | ||
| }, 1000, 30000) | ||
| .then(() => { | ||
| expect(true).toBeTruthy(); | ||
| }) | ||
| .catch((error) => { | ||
| console.error('Test failed:', error); | ||
| expect(false).toBeTruthy(); | ||
| }); | ||
|
|
||
| await deleteResources(reactionResources); | ||
| }, 180000); | ||
|
|
||
| test('Test EventBridge Handlebars Reaction - Updated Product', async () => { | ||
| const reactionResources = yaml.loadAll(fs.readFileSync(__dirname + '/eventbridge-reaction.yaml', 'utf8')); | ||
| await deployResources(reactionResources); | ||
|
|
||
| // Wait for reaction to be ready | ||
| await new Promise(r => setTimeout(r, 10000)); | ||
|
|
||
| // Update existing product | ||
| await dbClient.query(`UPDATE "Product" SET "Quantity" = 15 WHERE "ProductId" = 1`); | ||
|
|
||
| await waitForCondition(async () => { | ||
| try { | ||
| const result = await dbClient.query(`SELECT * FROM "Product" WHERE "ProductId" = 1`); | ||
| return result.rows.length === 1 && result.rows[0].Quantity === 15; | ||
| } catch (error) { | ||
| console.error('Error checking condition:', error); | ||
| return false; | ||
| } | ||
| }, 1000, 30000) | ||
| .then(() => { | ||
| expect(true).toBeTruthy(); | ||
| }) | ||
| .catch((error) => { | ||
| console.error('Test failed:', error); | ||
| expect(false).toBeTruthy(); | ||
| }); | ||
|
|
||
| await deleteResources(reactionResources); | ||
| }, 180000); | ||
|
|
||
| test('Test EventBridge Handlebars Reaction - Deleted Product', async () => { | ||
| const reactionResources = yaml.loadAll(fs.readFileSync(__dirname + '/eventbridge-reaction.yaml', 'utf8')); | ||
| await deployResources(reactionResources); | ||
|
|
||
| // Wait for reaction to be ready | ||
| await new Promise(r => setTimeout(r, 10000)); | ||
|
|
||
| // Delete a product | ||
| await dbClient.query(`DELETE FROM "Product" WHERE "ProductId" = 2`); | ||
|
|
||
| await waitForCondition(async () => { | ||
| try { | ||
| const result = await dbClient.query(`SELECT * FROM "Product" WHERE "ProductId" = 2`); | ||
| return result.rows.length === 0; | ||
| } catch (error) { | ||
| console.error('Error checking condition:', error); | ||
| return false; | ||
| } | ||
| }, 1000, 30000) | ||
| .then(() => { | ||
| expect(true).toBeTruthy(); | ||
| }) | ||
| .catch((error) => { | ||
| console.error('Test failed:', error); | ||
| expect(false).toBeTruthy(); | ||
| }); | ||
|
|
||
| await deleteResources(reactionResources); | ||
| }, 180000); | ||
|
|
||
| function waitForCondition(checkFn, interval = 1000, timeout = 30000) { | ||
| return new Promise((resolve, reject) => { | ||
| let elapsedTime = 0; | ||
|
|
||
| const intervalId = setInterval(async () => { | ||
| try { | ||
| if (await checkFn()) { | ||
| clearInterval(intervalId); | ||
| resolve(); | ||
| } else if (elapsedTime >= timeout) { | ||
| clearInterval(intervalId); | ||
| reject(new Error("Timed out waiting for condition to be met")); | ||
| } | ||
| elapsedTime += interval; | ||
| } catch (error) { | ||
| if (elapsedTime >= timeout) { | ||
| clearInterval(intervalId); | ||
| reject(error); | ||
| } | ||
| elapsedTime += interval; | ||
| } | ||
| }, interval); | ||
| }); | ||
| } | ||
163 changes: 163 additions & 0 deletions
163
e2e-tests/09-eventbridge-handlebars-scenario/resources.yaml
This file contains hidden or 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,163 @@ | ||
| # Copyright 2024 The Drasi Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| --- | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: test-data-init-eventbridge | ||
| data: | ||
| init.sql: > | ||
| CREATE TABLE "Product" ( | ||
| "ProductId" integer NOT NULL, | ||
| "ProductName" character varying(100) NOT NULL, | ||
| "Quantity" integer NOT NULL, | ||
| "Price" numeric(10,2) NOT NULL | ||
| ); | ||
|
|
||
| ALTER TABLE "Product" ADD CONSTRAINT pk_product | ||
| PRIMARY KEY ("ProductId"); | ||
|
|
||
| INSERT INTO "Product" ("ProductId", "ProductName", "Quantity", "Price") VALUES (1, 'Laptop', 10, 999.99); | ||
| INSERT INTO "Product" ("ProductId", "ProductName", "Quantity", "Price") VALUES (2, 'Mouse', 50, 25.00); | ||
| --- | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: test-pg-config-eventbridge | ||
| labels: | ||
| app: postgres-eventbridge | ||
| data: | ||
| POSTGRES_DB: test-db | ||
| POSTGRES_USER: test | ||
| POSTGRES_PASSWORD: test | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: postgres-eventbridge | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: postgres-eventbridge | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: postgres-eventbridge | ||
| spec: | ||
| containers: | ||
| - name: postgres | ||
| image: postgres:15-alpine | ||
| args: ["-c", "wal_level=logical"] | ||
| volumeMounts: | ||
| - name: init | ||
| mountPath: "/docker-entrypoint-initdb.d" | ||
| ports: | ||
| - containerPort: 5432 | ||
| envFrom: | ||
| - configMapRef: | ||
| name: test-pg-config-eventbridge | ||
| volumes: | ||
| - name: init | ||
| configMap: | ||
| name: test-data-init-eventbridge | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: postgres-eventbridge | ||
| labels: | ||
| app: postgres-eventbridge | ||
| spec: | ||
| type: ClusterIP | ||
| ports: | ||
| - port: 5432 | ||
| selector: | ||
| app: postgres-eventbridge | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: localstack | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: localstack | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: localstack | ||
| spec: | ||
| containers: | ||
| - name: localstack | ||
| image: localstack/localstack:3.0 | ||
| ports: | ||
| - containerPort: 4566 | ||
| env: | ||
| - name: SERVICES | ||
| value: "events" | ||
| - name: DEBUG | ||
| value: "1" | ||
| - name: DOCKER_HOST | ||
| value: "unix:///var/run/docker.sock" | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: localstack | ||
| labels: | ||
| app: localstack | ||
| spec: | ||
| type: ClusterIP | ||
| ports: | ||
| - port: 4566 | ||
| targetPort: 4566 | ||
| selector: | ||
| app: localstack | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Source | ||
| name: test-source-eventbridge | ||
| spec: | ||
| kind: PostgreSQL | ||
| properties: | ||
| host: postgres-eventbridge.default.svc.cluster.local | ||
| port: 5432 | ||
| user: test | ||
| password: test | ||
| database: test-db | ||
| ssl: false | ||
| tables: | ||
| - public.Product | ||
| --- | ||
| apiVersion: v1 | ||
| kind: ContinuousQuery | ||
| name: product-query | ||
| spec: | ||
| mode: query | ||
| sources: | ||
| subscriptions: | ||
| - id: test-source-eventbridge | ||
| query: > | ||
| MATCH | ||
| (p:Product) | ||
| WHERE | ||
| p.Quantity >= 0 | ||
| RETURN | ||
| p.ProductId AS ProductId, | ||
| p.ProductName AS ProductName, | ||
| p.Quantity AS Quantity, | ||
| p.Price AS Price |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this is a yaml string that should be here, so it needs to be "product-query: |"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. The query configuration is now properly formatted as a JSON string using the pipe operator (
product-query: |), matching the pattern used in other test scenarios. Also updated the readme examples to use the same format. See commit 94d0bcb.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot no, it should look more like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the YAML format to use
added:,updated:,deleted:sections withtemplate:andmetadata:properties as shown in your example. See commit 53ec10f.