Add support for a second function to log directly to bigquery.
This commit is contained in:
25
terraform/modules/cf_to_pubsub/functions/cf_to_bq/main.py
Normal file
25
terraform/modules/cf_to_pubsub/functions/cf_to_bq/main.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from google.cloud import bigquery
|
||||
|
||||
client = bigquery.Client()
|
||||
|
||||
|
||||
def push_to_pubsub(request_params):
|
||||
errors = client.insert_rows_json(os.environ["BQ_TABLE"], [request_params])
|
||||
if errors != []:
|
||||
raise Exception("Encountered errors while inserting rows: {}".format(errors))
|
||||
|
||||
|
||||
def main(request):
|
||||
request_json = request.get_json(silent=True)
|
||||
request_args = request.args
|
||||
|
||||
if request_json:
|
||||
push_to_pubsub(request_json)
|
||||
elif request_args:
|
||||
push_to_pubsub(request_args)
|
||||
else:
|
||||
return ("No data provided.", 400)
|
||||
return {"status": "ok", "source": "cf_to_bq"}
|
||||
@@ -0,0 +1,2 @@
|
||||
Flask==1.1.2
|
||||
google-cloud-bigquery==2.22.0
|
||||
@@ -4,7 +4,7 @@ import os
|
||||
from google.cloud import pubsub_v1
|
||||
|
||||
publisher = pubsub_v1.PublisherClient()
|
||||
topic = publisher.topic_path(os.environ.get("GCP_PROJECT"), os.environ.get("GCP_TOPIC"))
|
||||
topic = publisher.topic_path(os.environ["GCP_PROJECT"], os.environ["GCP_TOPIC"])
|
||||
|
||||
|
||||
def push_to_pubsub(request_params):
|
||||
@@ -20,5 +20,5 @@ def main(request):
|
||||
elif request_args:
|
||||
push_to_pubsub(request_args)
|
||||
else:
|
||||
raise Exception("No data provided.")
|
||||
return {"status": "ok"}
|
||||
return ("No data provided.", 400)
|
||||
return {"status": "ok", "source": "cf_to_pubsub"}
|
||||
|
||||
Reference in New Issue
Block a user