From d295f9f86189c13051e296cb795957771e096061 Mon Sep 17 00:00:00 2001 From: peter-zheng-g <43967553+peter-zheng-g@users.noreply.github.com> Date: Mon, 12 Nov 2018 17:26:41 -0800 Subject: [PATCH] [Asset] Add quick start code for ExportAssets API (#1829) * [Asset] Add quick start code for ExportAssets API * [Asset] Minor fix on comment. * [Asset] Fix import for test. * [Asset] Attempt to fix build error * [Asset] Fix code style issue. * [Asset] Fix build failure * [Asset] Minor fix * [Asset] Fix build failure * [Asset] Fix build failure * [Asset] Minor fix. * [Asset] Minor fix on license statement. Remove "All Rights Reserved.". --- asset/cloud-client/quickstart_exportassets.py | 51 +++++++++++++++++++ .../quickstart_exportassets_test.py | 50 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 asset/cloud-client/quickstart_exportassets.py create mode 100644 asset/cloud-client/quickstart_exportassets_test.py diff --git a/asset/cloud-client/quickstart_exportassets.py b/asset/cloud-client/quickstart_exportassets.py new file mode 100644 index 000000000000..da120cd38590 --- /dev/null +++ b/asset/cloud-client/quickstart_exportassets.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +# Copyright 2018 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. + + +import argparse + + +def export_assets(project_id, dump_file_path): + # [START asset_quickstart_exportassets] + from google.cloud import asset_v1beta1 + from google.cloud.asset_v1beta1.proto import asset_service_pb2 + + # TODO project_id = "Your Google Cloud Project ID" + # TODO dump_file_path = "Your asset dump file path" + + client = asset_v1beta1.AssetServiceClient() + parent = client.project_path(project_id) + output_config = asset_service_pb2.OutputConfig() + output_config.gcs_destination.uri = dump_file_path + response = client.export_assets(parent, output_config) + print(response.result) + # [END asset_quickstart_exportassets] + + +if __name__ == '__main__': + + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter + ) + parser.add_argument('project_id', help='Your Google Cloud project ID') + parser.add_argument('dump_file_path', + help='The file ExportAssets API will dump assets to, ' + 'e.g.: gs:///asset_dump_file') + + args = parser.parse_args() + + export_assets(args.project_id, args.dump_file_path) diff --git a/asset/cloud-client/quickstart_exportassets_test.py b/asset/cloud-client/quickstart_exportassets_test.py new file mode 100644 index 000000000000..53e55bd37f2f --- /dev/null +++ b/asset/cloud-client/quickstart_exportassets_test.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +# Copyright 2018 Google LLC. All Rights Reserved. +# +# 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. + +import os + +from google.cloud import storage +import pytest + +import quickstart_exportassets + +PROJECT = os.environ['GCLOUD_PROJECT'] +BUCKET = 'bucket-for-assets' + + +@pytest.fixture(scope='module') +def storage_client(): + yield storage.Client() + + +@pytest.fixture(scope='module') +def asset_bucket(storage_client): + storage_client.create_bucket(BUCKET) + + try: + storage_client.delete_bucket(BUCKET) + except Exception: + pass + + yield BUCKET + + +def test_export_assets(asset_bucket, capsys): + dump_file_path = "gs://", asset_bucket, "/assets-dump.txt" + quickstart_exportassets.export_assets(PROJECT, dump_file_path) + out, _ = capsys.readouterr() + + assert "uri: \"gs://cai-prober-prod-for-assets/phython-test.txt\"" in out