Skip to content

Commit

Permalink
trigger test topic/subscription creation implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarcasses committed Mar 20, 2019
1 parent bcdb3ff commit 37d4f60
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 17 deletions.
52 changes: 50 additions & 2 deletions plugins/gcp-pub-subs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ let consume = require("pluginbot/effects/consume")
let bcrypt = require("bcryptjs")
let fetch = require("node-fetch")
let fs = require("fs")
const crypto = require("crypto")

function* run(config, provide, channels) {
let db = yield consume(channels.database)
Expand Down Expand Up @@ -97,7 +96,9 @@ function* run(config, provide, channels) {
try {
const messageIds = await Promise.all(
pubsubs[eventName].map(({ client, topic }) =>
client.topic(topic).publish(Buffer.from(JSON.stringify(event)))
client
.topic(topic, { autoCreate: true })
.publish(Buffer.from(JSON.stringify(event)))
)
)
console.log(`message with id ${messageIds} sent`)
Expand Down Expand Up @@ -168,13 +169,60 @@ function* run(config, provide, channels) {
res.json({ reload: "success" })
}

const testTrigger = async (req, res, next) => {
const { id, event } = req.params
console.log("[GCP-PUB-SUB] testing trigger id:", id, " event:", event)
try {
const messages = []
// get the correspondent client
const { client: pubsub, topic: topicName } = pubsubs[event].filter(
c => `${c.id}` === id
)[0]
messages.push("const pubsub = new PubSub({...})")
// Creates the new topic
const topic = await pubsub.topic(topicName, { autoCreate: true })

messages.push(`const topicName = "${topicName}"`)
messages.push(
"const topic = await pubsub.topic(topicName, { autoCreate: true })"
)
messages.push(`=> Topic ${topic.name} created.`)
const subscriptionName = "servicebot-test-subscription"
const subscription = await topic
.subscription(subscriptionName)
.get({ autoCreate: true })

messages.push(`const subscriptionName = "servicebot-test-subscription"`)
messages.push(
`const subscription = await topic.createSubscription(subscriptionName)`
)
messages.push(`=> Subscription ${subscriptionName} created.`)

await pubsub.subscription(subscriptionName).delete()
messages.push(`await pubsub.subscription(subscriptionName).delete();`)
messages.push(`=> Subscription ${subscriptionName} deleted.`)

res.json({ test: "success", messages })
} catch (e) {
next(e)
}
}

const routeDefinition = [
{
endpoint: "/gcp-pub-sub/reload",
method: "get",
middleware: [reloadTriggers],
permissions: [],
description: "Reload and set all the triggers"
},
{
endpoint: "/gcp-pub-sub/test/:id/:event",
method: "get",
middleware: [testTrigger],
permissions: [],
description:
"Check if the trigger can create a topic, push a message to it and read it"
}
]

Expand Down
4 changes: 4 additions & 0 deletions plugins/gcp-pub-subs/stylesheets/gcp-pub-subs.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ textarea[name="service_account_key"] {
min-height: 300px;
}

.test-results {
font-size: 14px;
}

.page-servicebot-gcp-pub-subs {
padding: 40px 45px;
min-height: 705px;
Expand Down
126 changes: 126 additions & 0 deletions plugins/gcp-pub-subs/stylesheets/prism.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* PrismJS 1.15.0
https://prismjs.com/download.html#themes=prism-okaidia&languages=markup+css+clike+javascript */
/**
* okaidia theme for JavaScript, CSS and HTML
* Loosely based on Monokai textmate theme by http://www.monokai.nl/
* @author ocodia
*/

code[class*="language-"],
pre[class*="language-"] {
color: #f8f8f2;
background: none;
text-shadow: 0 1px rgba(0, 0, 0, 0.3);
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
font-size: 1em;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.5;

-moz-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;

-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}

/* Code blocks */
pre[class*="language-"] {
padding: 1em;
margin: .5em 0;
overflow: auto;
border-radius: 0.3em;
}

:not(pre) > code[class*="language-"],
pre[class*="language-"] {
background: #272822;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: slategray;
}

.token.punctuation {
color: #f8f8f2;
}

.namespace {
opacity: .7;
}

.token.property,
.token.tag,
.token.constant,
.token.symbol,
.token.deleted {
color: #f92672;
}

.token.boolean,
.token.number {
color: #ae81ff;
}

.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
color: #a6e22e;
}

.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string,
.token.variable {
color: #f8f8f2;
}

.token.atrule,
.token.attr-value,
.token.function,
.token.class-name {
color: #e6db74;
}

.token.keyword {
color: #66d9ef;
}

.token.regex,
.token.important {
color: #fd971f;
}

.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}

.token.entity {
cursor: help;
}

Loading

0 comments on commit 37d4f60

Please sign in to comment.