Skip to content

Instantly share code, notes, and snippets.

@27Bslash6
Last active May 4, 2020 03:55
Show Gist options
  • Save 27Bslash6/41489bc4a12253d3042df1d57c53ab70 to your computer and use it in GitHub Desktop.
Save 27Bslash6/41489bc4a12253d3042df1d57c53ab70 to your computer and use it in GitHub Desktop.
import civis
from base64 import b64decode
# from json import loads
import logging
import os
def get_credential_id(email):
credentials = client.credentials.list()
found = credentials[credentials.name == email]
if len(found) > 0:
logging.debug("Found credential: %s" % found["id"][0])
return found["id"][0]
raise "Credential not found: %" % email
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
if "CIVIS_API_KEY" not in os.environ:
raise Exception("CIVIS_API_KEY not found in environment")
logging.info(" > Connecting to Civis API ...")
client = civis.APIClient(return_type="pandas")
if "REDSHIFT_CREDENTIAL_ID" not in os.environ:
raise Exception("REDSHIFT_CREDENTIAL_ID not found in environment")
redshift_credential_id = os.environ["REDSHIFT_CREDENTIAL_ID"]
if "REDSHIFT_DATABASE_ID" not in os.environ:
raise Exception("REDSHIFT_DATABASE_ID not found in environment")
redshift_database_id = os.environ["REDSHIFT_DATABASE_ID"]
print(client.templates.list_scripts())
# @todo: get id dynamically
# Template ID of the 'Import BigQuery Table to Redshift' script
import_template_id = 19224
credential_id = get_credential_id(
"csl-example-dev-civis@global-data-resources.iam.gserviceaccount.com"
)
arguments = {
"GOOGLE_APPLICATION": int(credential_id),
"BQ_TABLE": "global-data-resources.csl_example_dev_reporting.signatures_view",
"GCP_PROJECT": "global-data-resources",
"GCS_BASE": "gs://csl-example-civis-temp/",
"REDSHIFT": {
"database": int(redshift_database_id),
"credential": int(redshift_credential_id),
},
"FORCE_SYNC": True,
"REDSHIFT_SCHEMA": "global_data_csl_sandbox",
"REDSHIFT_TABLE": "example_signatures",
}
result = civis.utils.run_template(import_template_id, arguments=arguments)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment