diff --git a/packages/google-cloud-asset/samples/snippets/quickstart_exportassets.py b/packages/google-cloud-asset/samples/snippets/quickstart_exportassets.py new file mode 100644 index 000000000000..da120cd38590 --- /dev/null +++ b/packages/google-cloud-asset/samples/snippets/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/packages/google-cloud-asset/samples/snippets/quickstart_exportassets_test.py b/packages/google-cloud-asset/samples/snippets/quickstart_exportassets_test.py new file mode 100644 index 000000000000..53e55bd37f2f --- /dev/null +++ b/packages/google-cloud-asset/samples/snippets/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