Skip to content

Commit 794ecb9

Browse files
authored
feat: bring eventbridge (#42)
This adds an [EventBridge](https://aws.amazon.com/eventbridge/) library. This implements the simulator version on top of the `cloud.Topic` and adds a subset of the [available filters](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns-content-based-filtering.html), namely: - Wildcard Matching: For simplicity, a basic wildcard match where * can represent any sequence of characters. - Comparison Operations: string operations "begins with", "ends with", and "equals-ignore-case". ### progress - [x] simulator - [x] tf-aws ### sim view ![Screenshot 2023-12-22 at 21 51 28](https://github.com/winglang/winglibs/assets/136789/13359c97-29c1-4cf2-a4a4-2f6c39ef1c08)
1 parent 3c56e43 commit 794ecb9

20 files changed

+3184
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: eventbridge-pull
2+
on:
3+
pull_request:
4+
paths:
5+
- eventbridge/**
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v3
12+
with:
13+
sparse-checkout: eventbridge
14+
- name: Setup Node.js
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: 18.x
18+
registry-url: https://registry.npmjs.org
19+
- name: Install winglang
20+
run: npm i -g winglang
21+
- name: Install dependencies
22+
run: npm install --include=dev
23+
working-directory: eventbridge
24+
- name: Test
25+
run: wing test
26+
working-directory: eventbridge
27+
- name: Pack
28+
run: wing pack
29+
working-directory: eventbridge
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: eventbridge-release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- eventbridge/**
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v3
14+
with:
15+
sparse-checkout: eventbridge
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v3
18+
with:
19+
node-version: 18.x
20+
registry-url: https://registry.npmjs.org
21+
- name: Install winglang
22+
run: npm i -g winglang
23+
- name: Install dependencies
24+
run: npm install --include=dev
25+
working-directory: eventbridge
26+
- name: Test
27+
run: wing test
28+
working-directory: eventbridge
29+
- name: Pack
30+
run: wing pack
31+
working-directory: eventbridge
32+
- name: Publish
33+
run: npm publish --access=public --registry https://registry.npmjs.org --tag
34+
latest *.tgz
35+
working-directory: eventbridge
36+
env:
37+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

eventbridge/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
node_modules/

eventbridge/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Wing
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

eventbridge/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# eventbridge
2+
3+
A Wing library for working with [Amazon EventBridge](https://aws.amazon.com/eventbridge/).
4+
5+
## Prerequisites
6+
7+
* [winglang](https://winglang.io)
8+
9+
## Installation
10+
11+
```sh
12+
npm i @winglibs/eventbridge
13+
```
14+
15+
## Usage
16+
17+
```js
18+
bring eventbridge;
19+
20+
let bus = new eventbridge.Bus();
21+
22+
bus.subscribeFunction("github.pull-request.created", inflight (event) => {
23+
log("subscribed event received {Json.stringify(event)}");
24+
}, {
25+
"detail-type": [{"prefix": "pull-request."}],
26+
"source": ["github.com"],
27+
});
28+
29+
new cloud.Function(inflight () => {
30+
bus.putEvents([{
31+
detailType: "pull-request.created",
32+
resources: ["test"],
33+
source: "github.com",
34+
version: "0",
35+
detail: {
36+
"test": "test",
37+
},
38+
}]);
39+
});
40+
41+
```
42+
43+
## License
44+
45+
This library is licensed under the [MIT License](./LICENSE).

eventbridge/lib.test.w

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
bring cloud;
2+
bring expect;
3+
bring util;
4+
bring "./lib.w" as lib;
5+
bring "./types.w" as types;
6+
7+
let eventBridge = new lib.Bus(name: "test");
8+
9+
class InboundGithubEvents {
10+
pub bucket: cloud.Bucket;
11+
12+
new() {
13+
this.bucket = new cloud.Bucket();
14+
let counter = new cloud.Counter();
15+
16+
eventBridge.subscribeFunction("github.pull-request.created", inflight (event) => {
17+
log("subscribed event received {Json.stringify(event)}");
18+
this.bucket.put("test-{counter.inc()}", Json.stringify(event));
19+
}, {
20+
"detail-type": [{"prefix": "pull-request."}],
21+
"source": ["github.com"],
22+
});
23+
}
24+
}
25+
26+
class Environments {
27+
pub bucket: cloud.Bucket;
28+
new() {
29+
let queue = new cloud.Queue();
30+
this.bucket = new cloud.Bucket();
31+
32+
queue.setConsumer(inflight (event) => {
33+
log("subscribed event {event} received {Json.stringify(event)}");
34+
this.bucket.put("environment", event);
35+
});
36+
37+
eventBridge.subscribeQueue("environments.created", queue, {
38+
"detail-type": [{"prefix": "myTest"}],
39+
source: ["myTest"],
40+
});
41+
}
42+
}
43+
44+
let github = new InboundGithubEvents();
45+
let env = new Environments();
46+
47+
test "publish to eventbridge" {
48+
log("publishing to eventbridge");
49+
eventBridge.putEvents([{
50+
detailType: "pull-request.created",
51+
resources: ["test"],
52+
source: "github.com",
53+
version: "0",
54+
detail: {
55+
"test": "test",
56+
},
57+
}]);
58+
59+
log("published");
60+
61+
util.waitUntil(inflight () => {
62+
log("checking bucket for event");
63+
return github.bucket.exists("test-0");
64+
}, {
65+
timeout: 60s,
66+
});
67+
68+
log("after wait");
69+
let published = types.Event.fromJson(github.bucket.getJson("test-0"));
70+
expect.equal("pull-request.created", published.detailType);
71+
expect.equal("test", published.resources.at(0));
72+
expect.equal("github.com", published.source);
73+
74+
expect.equal(0, env.bucket.list().length);
75+
76+
eventBridge.putEvents([{
77+
detailType: "myTest.check",
78+
resources: ["test"],
79+
source: "myTest",
80+
version: "0",
81+
detail: {
82+
"fake": "env",
83+
},
84+
}]);
85+
86+
log("published 2nd event");
87+
88+
util.waitUntil(inflight () => {
89+
log("checking environment bucket for event");
90+
return env.bucket.exists("environment");
91+
}, {
92+
timeout: 60s,
93+
});
94+
95+
// cant deserialize events coming from queue (see https://github.com/winglang/wing/issues/3686)
96+
let published2 = env.bucket.getJson("environment");
97+
expect.equal("myTest.check", published2.get("detail-type"));
98+
expect.equal("test", published2.get("resources").getAt(0));
99+
expect.equal("myTest", published2.get("source"));
100+
}

eventbridge/lib.w

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
bring cloud;
2+
bring util;
3+
bring "./types.w" as types;
4+
bring "./platform/sim" as sim;
5+
bring "./platform/tfaws" as aws;
6+
bring "./platform/awscdk" as awscdk;
7+
8+
/**
9+
Wing resource for Amazon EventBridge.
10+
*/
11+
pub class Bus impl types.IBus {
12+
inner: types.IBus;
13+
14+
new(props: types.BusProps) {
15+
let target = util.env("WING_TARGET");
16+
if target == "sim" {
17+
this.inner = new sim.Bus(props) as "sim";
18+
} elif target == "tf-aws" {
19+
this.inner = new aws.Bus(props) as "tf-aws";
20+
} elif target == "awscdk" {
21+
this.inner = new awscdk.Bus(props) as "awscdk";
22+
} else {
23+
throw "Unsupported target {target}";
24+
}
25+
}
26+
27+
pub inflight putEvents(events: Array<types.PublishEvent>): void {
28+
this.inner.putEvents(events);
29+
}
30+
31+
pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
32+
this.inner.subscribeFunction(name, handler, pattern);
33+
}
34+
pub subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void {
35+
this.inner.subscribeQueue(name, queue, pattern);
36+
}
37+
}

0 commit comments

Comments
 (0)