Skip to content

Commit

Permalink
Merge pull request #65 from daichitakahashi/simplePulumi
Browse files Browse the repository at this point in the history
Add new simple EventHubHelper
  • Loading branch information
daichitakahashi authored Dec 15, 2024
2 parents f47c320 + 1db54d7 commit a384093
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
6 changes: 6 additions & 0 deletions iac/pulumi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @cf-eventhub/pulumi

## 0.0.14

### Patch Changes

- 813ba7b: Add new simple EventHubHelper

## 0.0.13

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion iac/pulumi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@cf-eventhub/pulumi",
"main": "src/index.ts",
"type": "commonjs",
"version": "0.0.13",
"version": "0.0.14",
"keywords": [
"pulumi",
"cloudflare"
Expand Down
74 changes: 74 additions & 0 deletions iac/pulumi/src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { RunError, log } from "@pulumi/pulumi";
import type { Config } from "cf-eventhub/core";

type Handler = { service: string; entrypoint?: string };

/**
* Helper to construct EventHub and its Executor
*/
export class EventHubHelper {
destinations: Map<string, Handler | null>;

/**
* @param config Configuration of EventHub routing. This is re-exported from `EventHubHelper.config`.
* @param strict If true, an error is thrown when a bound handler is not found.
*/
constructor(
public config: Config,
private strict: boolean,
) {
this.destinations = config.routes.reduce((acc, route) => {
acc.set(route.destination, null);
return acc;
}, new Map<string, Handler | null>());
}

/**
* Assign handler for the destination.
* Handler should be found in the config.
* @param destination
* @param handler
* @returns
*/
public assignHandler(destination: string, handler: Handler) {
const d = this.destinations.get(destination);
if (d === undefined) {
log.warn(
`EventHubHelper: destination ${destination} is not found in the config.`,
);
return;
}
if (!d) {
this.destinations.set(destination, handler);
}
}

/**
* Get all registered handlers.
* If there are destinations that are not assigned, it will log a warning or throw error.
* @returns Array of handler bindings.
*/
public getHandlers() {
const handlers = [...this.destinations.entries()];
const notRegistered = handlers
.filter(([, handler]) => handler === null)
.map(([destination]) => destination);
if (notRegistered.length > 0) {
const message =
notRegistered.length === 1
? `EventHubHelper: destination ${notRegistered[0]} is not registered.`
: `EventHubHelper: destinations ${notRegistered.join(", ")} are not registered.`;
if (this.strict) {
throw new RunError(message);
}
log.warn(message);
}
return handlers
.filter((h): h is [string, Handler] => h[1] !== null)
.map(([destination, { service, entrypoint }]) => ({
binding: destination,
service,
entrypoint,
}));
}
}
1 change: 1 addition & 0 deletions iac/pulumi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Queue, WorkersCronTrigger } from "@pulumi/cloudflare";
import { type Input, jsonStringify } from "@pulumi/pulumi";
import type { Config, ConfigInput } from "cf-eventhub/core";
export * from "./helper";

export type HandlerWorkerScriptBinding = {
readonly name: Input<string>;
Expand Down

0 comments on commit a384093

Please sign in to comment.