Skip to content

Commit

Permalink
feat(eventbridge)!: new eventbridge api (#122)
Browse files Browse the repository at this point in the history
* Optional name property
* changed `subscribeFunction` to `onEvent`
* `putEvents` is now variadic
  • Loading branch information
eladcon authored Mar 25, 2024
1 parent fa129e9 commit 9a301cc
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 25 deletions.
12 changes: 9 additions & 3 deletions eventbridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ bring eventbridge;

let bus = new eventbridge.Bus(name: "my-bus");

bus.subscribeFunction("github.pull-request.created", inflight (event) => {
bus.onEvent("github.pull-request.created", inflight (event) => {
log("subscribed event received {Json.stringify(event)}");
}, {
"detail-type": [{"prefix": "pull-request."}],
"source": ["github.com"],
});

new cloud.Function(inflight () => {
bus.putEvents([{
bus.putEvents({
detailType: "pull-request.created",
resources: ["test"],
source: "github.com",
version: "0",
detail: {
"test": "test",
},
}]);
});
});
```

## Parameters

* eventBridgeName - `str` - Optional. Name of an existing EventBridge to reference.

#### Usage

```sh
wing compile -t @winglang/platform-awscdk -v eventBridgeName="my-bus" main.w
```

## License

This library is licensed under the [MIT License](./LICENSE).
10 changes: 5 additions & 5 deletions eventbridge/lib.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class InboundGithubEvents {
this.bucket = new cloud.Bucket();
let counter = new cloud.Counter();

eventBridge.subscribeFunction("github.pull-request.created", inflight (event) => {
eventBridge.onEvent("github.pull-request.created", inflight (event) => {
log("subscribed event received {Json.stringify(event)}");
this.bucket.put("test-{counter.inc()}", Json.stringify(event));
}, {
Expand Down Expand Up @@ -46,15 +46,15 @@ let env = new Environments();

test "publish to eventbridge" {
log("publishing to eventbridge");
eventBridge.putEvents([{
eventBridge.putEvents({
detailType: "pull-request.created",
resources: ["test"],
source: "github.com",
version: "0",
detail: {
"test": "test",
},
}]);
});

log("published");

Expand All @@ -73,15 +73,15 @@ test "publish to eventbridge" {

expect.equal(0, env.bucket.list().length);

eventBridge.putEvents([{
eventBridge.putEvents({
detailType: "myTest.check",
resources: ["test"],
source: "myTest",
version: "0",
detail: {
"fake": "env",
},
}]);
});

log("published 2nd event");

Expand Down
8 changes: 4 additions & 4 deletions eventbridge/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bring "./platform/awscdk" as awscdk;
pub class Bus impl types.IBus {
inner: types.IBus;

new(props: types.BusProps) {
new(props: types.BusProps?) {
let target = util.env("WING_TARGET");
if target == "sim" {
this.inner = new sim.Bus(props) as "sim";
Expand All @@ -24,12 +24,12 @@ pub class Bus impl types.IBus {
}
}

pub inflight putEvents(events: Array<types.PublishEvent>): void {
pub inflight putEvents(...events: Array<types.PublishEvent>): void {
this.inner.putEvents(events);
}

pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
this.inner.subscribeFunction(name, handler, pattern);
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
this.inner.onEvent(name, handler, pattern);
}
pub subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void {
this.inner.subscribeQueue(name, queue, pattern);
Expand Down
4 changes: 2 additions & 2 deletions eventbridge/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion eventbridge/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/eventbridge",
"description": "Amazon EventBridge library for Wing",
"version": "0.0.4",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand Down
6 changes: 3 additions & 3 deletions eventbridge/platform/awscdk/eventbridge.w
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ pub class Bus impl types.IBus {

eventBridge: cdk.aws_events.IEventBus;

new(props: types.BusProps) {
new(props: types.BusProps?) {
let app = nodeof(this).app;
// TODO: use typed properties when its available
if let eventBridgeName = app.parameters.value("eventBridgeName") {
this.eventBridge = cdk.aws_events.EventBus.fromEventBusName(this, "EventBridge", eventBridgeName);
} else {
this.eventBridge = new cdk.aws_events.EventBus(eventBusName: props.name) as "EventBridge";
this.eventBridge = new cdk.aws_events.EventBus(eventBusName: props?.name ?? "eventbridge-{this.node.addr.substring(0, 8)}") as "EventBridge";
}
}

pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
// event will be json of type `types.Event`
let funk = new cloud.Function(inflight (event) => {
// since wing structs don't supoort custom serialization we need to do it manually
Expand Down
2 changes: 1 addition & 1 deletion eventbridge/platform/sim/bus.w
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub class EventBridgeBus {
topic: cloud.Topic;
var handlerCount: num;

new(props: types.BusProps) {
new(props: types.BusProps?) {
this.topic = new cloud.Topic() as "EventBridge";

this.handlerCount = 0;
Expand Down
4 changes: 2 additions & 2 deletions eventbridge/platform/sim/eventbridge.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ bring "./bus.w" as bus;
pub class Bus impl types.IBus {
bus: bus.EventBridgeBus;

new(props: types.BusProps) {
new(props: types.BusProps?) {
this.bus = new bus.EventBridgeBus(props);
}

pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
class FnRule {
new(bus: bus.EventBridgeBus) {
let onMessageHandler = bus.subscribe(inflight (event) => {
Expand Down
6 changes: 3 additions & 3 deletions eventbridge/platform/tfaws/eventbridge.w
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub class Bus impl types.IBus {
busName: str;
busArn: str;

new(props: types.BusProps) {
new(props: types.BusProps?) {
let app = nodeof(this).app;
// TODO: use typed properties when its available
if let eventBridgeName = app.parameters.value("eventBridgeName") {
Expand All @@ -19,13 +19,13 @@ pub class Bus impl types.IBus {
this.busName = bus.name;
this.busArn = bus.arn;
} else {
let bus = new tfAws.cloudwatchEventBus.CloudwatchEventBus(name: props.name) as "EventBridge";
let bus = new tfAws.cloudwatchEventBus.CloudwatchEventBus(name: props?.name ?? "eventbridge-{this.node.addr.substring(0, 8)}") as "EventBridge";
this.busName = bus.name;
this.busArn = bus.arn;
}
}

pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
let rule = new tfAws.cloudwatchEventRule.CloudwatchEventRule(
name: name,
eventBusName: this.busName,
Expand Down
2 changes: 1 addition & 1 deletion eventbridge/types.w
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ pub struct PublishEvent {

pub interface IBus extends std.IResource {
inflight putEvents(events: Array<PublishEvent>): void;
subscribeFunction(name: str, handler: inflight (Event): void, pattern: Json): void;
onEvent(name: str, handler: inflight (Event): void, pattern: Json): void;
subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void;
}

0 comments on commit 9a301cc

Please sign in to comment.