forked from GoogleCloudPlatform/cloud-foundation-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GoogleCloudPlatform#402 from arsenyspb/feature/sai…
…ebq-file-exporter Add optional file exporter for NDJSON, CSV from Bigquery
- Loading branch information
Showing
12 changed files
with
284 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
cloud-operations/scheduled-asset-inventory-export-bq/cffile/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
'''Cloud Function module to export BQ table as JSON. | ||
This module is designed to be plugged in a Cloud Function, attached to Cloud | ||
Scheduler trigger to create a JSON of IP to hostname mappings from BigQuery. | ||
''' | ||
|
||
import base64 | ||
import datetime | ||
import json | ||
import logging | ||
import os | ||
import warnings | ||
|
||
from google.api_core.exceptions import GoogleAPIError | ||
from google.cloud import bigquery | ||
|
||
import click | ||
|
||
import googleapiclient.discovery | ||
import googleapiclient.errors | ||
|
||
|
||
def _configure_logging(verbose=True): | ||
'''Basic logging configuration. | ||
Args: | ||
verbose: enable verbose logging | ||
''' | ||
level = logging.DEBUG if verbose else logging.INFO | ||
logging.basicConfig(level=level) | ||
warnings.filterwarnings('ignore', r'.*end user credentials.*', UserWarning) | ||
|
||
@click.command() | ||
@click.option('--bucket', required=True, help='GCS bucket for export') | ||
@click.option('--filename', required=True, help='Path and filename with extension to export e.g. folder/export.json .') | ||
@click.option('--format', required=True, help='The exported file format, e.g. NEWLINE_DELIMITED_JSON or CSV.') | ||
@click.option('--bq-dataset', required=True, help='Bigquery dataset where table for export is located.') | ||
@click.option('--bq-table', required=True, help='Bigquery table to export.') | ||
@click.option('--verbose', is_flag=True, help='Verbose output') | ||
def main_cli(bucket=None, filename=None, format=None, bq_dataset=None, bq_table=None, verbose=False): | ||
'''Trigger Cloud Asset inventory export from Bigquery to file. Data will be stored in | ||
the dataset specified on a dated table with the name specified. | ||
''' | ||
try: | ||
_main(bucket, filename, format, bq_dataset, bq_table, verbose) | ||
except RuntimeError: | ||
logging.exception('exception raised') | ||
|
||
def main(event, context): | ||
'Cloud Function entry point.' | ||
try: | ||
data = json.loads(base64.b64decode(event['data']).decode('utf-8')) | ||
print(data) | ||
_main(**data) | ||
# uncomment once https://issuetracker.google.com/issues/155215191 is fixed | ||
# except RuntimeError: | ||
# raise | ||
except Exception: | ||
logging.exception('exception in cloud function entry point') | ||
|
||
|
||
def _main(bucket=None, filename=None, format=None, bq_dataset=None, bq_table=None, verbose=False): | ||
'Module entry point used by cli and cloud function wrappers.' | ||
|
||
_configure_logging(verbose) | ||
client = bigquery.Client() | ||
destination_uri = 'gs://{}/{}'.format(bucket, filename) | ||
dataset_ref = client.dataset(bq_dataset) | ||
table_ref = dataset_ref.table(bq_table) | ||
job_config = bigquery.job.ExtractJobConfig() | ||
job_config.destination_format = ( | ||
getattr(bigquery.DestinationFormat, format) ) | ||
extract_job = client.extract_table( | ||
table_ref, destination_uri, job_config=job_config | ||
) | ||
try: | ||
extract_job.result() | ||
except (GoogleAPIError, googleapiclient.errors.HttpError) as e: | ||
logging.debug('API Error: %s', e, exc_info=True) | ||
raise RuntimeError( | ||
'Error exporting BQ table %s as a file' % bq_table, e) | ||
|
||
|
||
if __name__ == '__main__': | ||
main_cli() |
3 changes: 3 additions & 0 deletions
3
cloud-operations/scheduled-asset-inventory-export-bq/cffile/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
google-api-python-client>=1.10.1 | ||
google-cloud-monitoring>=1.1.0 | ||
google-cloud-bigquery |
Binary file added
BIN
+53.5 KB
cloud-operations/scheduled-asset-inventory-export-bq/diagram_optional.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+131 Bytes
tests/cloud_operations/scheduled_asset_inventory_export_bq/fixture/bundle_cffile.zip
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.