Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eladcon committed Mar 13, 2024
1 parent 9978071 commit 0a72274
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 67 deletions.
4 changes: 2 additions & 2 deletions eventbridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ bus.subscribeFunction("github.pull-request.created", inflight (event) => {
});

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

```
Expand Down
8 changes: 4 additions & 4 deletions eventbridge/lib.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ let env = new Environments();

test "publish to eventbridge" {
log("publishing to eventbridge");
eventBridge.publish(
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.publish(
eventBridge.putEvents([{
detailType: "myTest.check",
resources: ["test"],
source: "myTest",
version: "0",
detail: {
"fake": "env",
},
);
}]);

log("published 2nd event");

Expand Down
6 changes: 3 additions & 3 deletions eventbridge/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bring "./platform/tfaws" as aws;
bring "./platform/awscdk" as awscdk;

/**
Winglang resource for Amazon EventBridge.
Wing resource for Amazon EventBridge.
*/
pub class Bus impl types.IBus {
inner: types.IBus;
Expand All @@ -24,8 +24,8 @@ pub class Bus impl types.IBus {
}
}

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

pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
Expand Down
107 changes: 95 additions & 12 deletions eventbridge/package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions eventbridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
"constructs": "^10.3.0"
},
"peerDependencies": {
"@aws-sdk/client-eventbridge": "^3.525.0",
"@cdktf/provider-aws": "^18",
"aws-cdk-lib": "^2.109.0",
"cdktf": "^0.19",
"constructs": "^10.3"
},
"dependencies": {
"@aws-sdk/client-eventbridge": "^3.525.0"
}
}
20 changes: 5 additions & 15 deletions eventbridge/platform/aws/publish.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
const { EventBridgeClient, PutEventsCommand } = require("@aws-sdk/client-eventbridge");

const putEvent = async (eventBridgeName, event) => {
console.log(`Publishing event to EventBridge: ${eventBridgeName} - ${JSON.stringify(event)}`);
const client = new EventBridgeClient({ region: process.env.AWS_REGION });
const input = {
Entries: [
{
Source: event.source,
DetailType: event.detailType,
Detail: JSON.stringify(event.detail),
EventBusName: eventBridgeName,
Resources: event.resources,
},
],
};
const client = new EventBridgeClient();

const _putEvent = async (eventBridgeName, input) => {
console.log(`Publishing event to EventBridge: ${eventBridgeName} - ${JSON.stringify(input)}`);
const command = new PutEventsCommand(input);
await client.send(command);
}

module.exports = {
putEvent
_putEvent
}
34 changes: 34 additions & 0 deletions eventbridge/platform/aws/publish.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
bring "../../types.w" as types;

pub struct PutEventCommandEntry {
Source: str;
DetailType: str;
Detail: str;
EventBusName: str;
Resources: Array<str>;
}

pub struct PutEventCommandInput {
Entries: Array<PutEventCommandEntry>;
}

pub class Util {
pub static inflight putEvent(name: str, events: Array<types.PublishEvent>): void {
let entries = MutArray<PutEventCommandEntry>[];
for event in events {
entries.push(PutEventCommandEntry{
Source: event.source,
DetailType: event.detailType,
Detail: Json.stringify(event.detail),
EventBusName: name,
Resources: event.resources,
});
}
let input = {
Entries: entries.copy(),
};
Util._putEvent(name, input);
}

extern "./publish.js" pub static inflight _putEvent(name: str, event: PutEventCommandInput): void;
}
8 changes: 4 additions & 4 deletions eventbridge/platform/awscdk/eventbridge.w
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
bring cloud;
bring aws;
bring "./../../types.w" as types;
bring "../aws/publish.w" as awsUtils;
bring "aws-cdk-lib" as cdk;

pub class Bus impl types.IBus {
extern "../aws/publish.js" pub static inflight putEvent(name: str, event: types.PublishEvent): void;
extern "./helper.js" pub static addRulePermission(handler: str, arn: str): void;

eventBridge: cdk.aws_events.IEventBus;
Expand Down Expand Up @@ -65,14 +65,14 @@ pub class Bus impl types.IBus {
cdkQueue.addToResourcePolicy(statement);
}

pub inflight publish(event: types.PublishEvent): void {
pub inflight putEvents(events: Array<types.PublishEvent>): void {
let name = this.eventBridge.eventBusName;
Bus.putEvent(name, event);
awsUtils.putEvent(name, events);
}

pub onLift(host: std.IInflightHost, ops: Array<str>) {
if let host = aws.Function.from(host) {
if ops.contains("publish") {
if ops.contains("putEvents") {
host.addPolicyStatements(aws.PolicyStatement {
effect: cdk.aws_iam.Effect.ALLOW,
actions: ["events:PutEvents"],
Expand Down
34 changes: 18 additions & 16 deletions eventbridge/platform/sim/bus.w
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ pub class EventBridgeBus {
return handler.onMessageHandler;
}

pub inflight publish(event: types.PublishEvent): void {
let fullEvent = Json {
id: util.uuidv4(),
time: "{datetime.utcNow().toIso()}",
region: "local",
account: "local",
resources: event.resources,
version: event.version,
source: event.source,
"detail-type": event.detailType,
detail: event.detail,
};

let stringified = Json.stringify(fullEvent);
log("EventBridge: published event: " + stringified);
this.topic.publish(stringified);
pub inflight putEvents(events: Array<types.PublishEvent>): void {
for event in events {
let fullEvent = Json {
id: util.uuidv4(),
time: "{datetime.utcNow().toIso()}",
region: "local",
account: "local",
resources: event.resources,
version: event.version,
source: event.source,
"detail-type": event.detailType,
detail: event.detail,
};

let stringified = Json.stringify(fullEvent);
log("EventBridge: published event: " + stringified);
this.topic.publish(stringified);
}
}
}
4 changes: 2 additions & 2 deletions eventbridge/platform/sim/eventbridge.w
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub class Bus impl types.IBus {
let rule = new Rule(this.bus) as "Rule \"{name}\"";
}

pub inflight publish(event: types.PublishEvent): void {
this.bus.publish(event);
pub inflight putEvents(events: Array<types.PublishEvent>): void {
this.bus.putEvents(events);
}
}
9 changes: 4 additions & 5 deletions eventbridge/platform/tfaws/eventbridge.w
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
bring cloud;
bring aws;
bring "./../../types.w" as types;
bring "../aws/publish.w" as awsUtils;
bring "cdktf" as cdktf;
bring "@cdktf/provider-aws" as tfAws;

pub class Bus impl types.IBus {
extern "../aws/publish.js" pub static inflight putEvent(name: str, event: types.PublishEvent): void;

busName: str;
busArn: str;

Expand Down Expand Up @@ -89,14 +88,14 @@ pub class Bus impl types.IBus {
) as "{name}-policy";
}

pub inflight publish(event: types.PublishEvent): void {
pub inflight putEvents(events: Array<types.PublishEvent>): void {
let name = this.busName;
Bus.putEvent(name, event);
awsUtils.putEvent(name, events);
}

pub onLift(host: std.IInflightHost, ops: Array<str>) {
if let host = aws.Function.from(host) {
if ops.contains("publish") {
if ops.contains("putEvents") {
host.addPolicyStatements(aws.PolicyStatement {
actions: ["events:PutEvents"],
resources: [this.busArn],
Expand Down
2 changes: 1 addition & 1 deletion eventbridge/types.w
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct PublishEvent {
}

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

0 comments on commit 0a72274

Please sign in to comment.