Skip to content

Commit

Permalink
feat: Add makePulumiCallback (#5)
Browse files Browse the repository at this point in the history
Add a specialized callback function to make it easer to connect with Pulumi.
  • Loading branch information
simenandre committed Sep 15, 2021
1 parent 416ea55 commit b71b2b8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
29 changes: 29 additions & 0 deletions examples/pulumi-slack-api.ts
@@ -0,0 +1,29 @@
import * as gcp from '@pulumi/gcp';
import * as pulumi from '@pulumi/pulumi';
import { makePulumiCallback } from 'gcl-slack';

const name = 'slack';

export const topic = new gcp.pubsub.Topic(name, {}, { provider });

const config = new pulumi.Config(name);

topic.onMessagePublished(name, {
region: 'europe-west1',
runtime: 'nodejs14',
environmentVariables: {
SLACK_TOKEN: config.require('token'),
},
callback: makePulumiCallback('api'),
});

const logSink = new gcp.logging.ProjectSink(name, {
filter: 'operation.producer="github.com/bjerkio/nestjs-slack@v1"',
destination: pulumi.interpolate`pubsub.googleapis.com/${topic.id}`,
});

new gcp.pubsub.TopicIAMMember(name, {
topic: topic.name,
role: 'roles/pubsub.publisher',
member: logSink.writerIdentity,
});
16 changes: 2 additions & 14 deletions examples/pulumi.ts → examples/pulumi-slack-webhook.ts
@@ -1,7 +1,6 @@
import * as gcp from '@pulumi/gcp';
import * as pulumi from '@pulumi/pulumi';
import { makeHandler } from 'gcl-slack';
import { handlePubSubMessage } from 'pubsub-http-handler';
import { makePulumiCallback } from 'gcl-slack';

const name = 'slack';

Expand All @@ -15,18 +14,7 @@ topic.onMessagePublished(name, {
environmentVariables: {
WEBHOOK_URL: config.require('webhook-url'),
},
callback: async (message) => {
const handler = makeHandler({
type: 'webhook',
webhookOptions: { url: process.env.WEBHOOK_URL ?? '' },
});

await handlePubSubMessage({
message,
handler,
parseJson: true,
});
},
callback: makePulumiCallback('webhook'),
});

const logSink = new gcp.logging.ProjectSink(name, {
Expand Down
45 changes: 44 additions & 1 deletion src/index.ts
@@ -1,7 +1,8 @@
import { FastifyPluginAsync } from 'fastify';
import * as phh from 'pubsub-http-handler';
import { handlePubSubMessage } from 'pubsub-http-handler';
import { makeHandler } from './handler';
import { SlackConfig } from './types';
import { SlackConfig, SlackRequestType } from './types';
export { makeHandler } from './handler';

export const makePubSubCloudFunctions = (
Expand All @@ -22,3 +23,45 @@ export const makePubSubServer = (
): phh.CreatePubSubHandlerResponse => {
return phh.createPubSubServer(makeHandler(config));
};

export type PulumiCallbackFun = (message: any) => Promise<any>;

export const makePulumiCallback = (
type: SlackRequestType,
): PulumiCallbackFun => {
if (type === 'api') {
return async (message: any): Promise<void> => {
const token = process.env.SLACK_TOKEN;
if (!token) {
throw new Error('Slack token is missing');
}

await handlePubSubMessage({
message,
handler: makeHandler({
type: 'api',
apiOptions: { token },
}),
parseJson: true,
});
};
} else if (type === 'webhook') {
return async (message: any): Promise<void> => {
const url = process.env.WEBHOOK_URL;
if (!url) {
throw new Error('Slack webhook URL is missing');
}

await handlePubSubMessage({
message,
handler: makeHandler({
type: 'webhook',
webhookOptions: { url },
}),
parseJson: true,
});
};
}

throw new Error('Wrong configuration');
};

0 comments on commit b71b2b8

Please sign in to comment.