Skip to content

Commit

Permalink
wrapped subscription events
Browse files Browse the repository at this point in the history
  • Loading branch information
max-ostapenko committed Dec 11, 2024
1 parent d7f1d0f commit 56814c4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
17 changes: 10 additions & 7 deletions infra/dataform-export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,37 @@ async function callRunJob (payload = {}) {
*/
functions.http('dataform-export', async (req, res) => {
try {
console.log(JSON.stringify(req.body))
const message = req.body
const message = req.body.message
if (!message) {
console.log(`no message received: ${JSON.stringify(req.body)}`)
res.status(400).send('Bad Request: no message received')
return
}

const query = message.protoPayload.serviceData.jobCompletedEvent.job.jobConfiguration.query.query
const messageData = (message.data && JSON.parse(Buffer.from(message.data, 'base64').toString('utf-8'))) || message
if (!messageData) {
console.info(JSON.stringify(message))
res.status(400).send('Bad Request: invalid message format')
}

const query = messageData.protoPayload.serviceData.jobCompletedEvent.job.jobConfiguration.query.query
if (!query) {
console.log(`no query found: ${JSON.stringify(message)}`)
console.log(`no query found: ${JSON.stringify(messageData)}`)
res.status(400).send('Bad Request: no query found')
return
}

const regex = /\/\* ({"dataform_trigger":.+) \*\//
const reportConfig = regex.exec(query)
if (!reportConfig) {
console.log(`no trigger config found: ${query}`)
res.status(400).send('Bad Request: no trigger config found')
return
}

const eventData = JSON.parse(reportConfig[1])
await callRunJob(eventData)

res.status(200).send('OK')
} catch (error) {
console.log(JSON.stringify(req.body))
console.error(error)
res.status(500).send('Internal Server Error')
}
Expand Down
3 changes: 0 additions & 3 deletions infra/tf/function_dataform_export.tf
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ resource "google_pubsub_subscription" "dataform_export" {
audience = google_cloudfunctions2_function.dataform_export.service_config[0].uri
service_account_email = local.function_identity
}
no_wrapper {
write_metadata = false
}
}
retry_policy {
maximum_backoff = "60s"
Expand Down
2 changes: 1 addition & 1 deletion infra/tf/functions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ locals {
}

resource "google_project_iam_member" "project" {
for_each = toset(["roles/bigquery.jobUser", "roles/dataform.serviceAgent", "roles/run.invoker"])
for_each = toset(["roles/bigquery.jobUser", "roles/dataform.serviceAgent", "roles/run.invoker", "roles/run.jobsExecutorWithOverrides"])

project = local.project
role = each.value
Expand Down

0 comments on commit 56814c4

Please sign in to comment.