Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Asset] Add quick start code for ExportAssets API #1829

Merged
merged 11 commits into from
Nov 13, 2018
51 changes: 51 additions & 0 deletions asset/cloud-client/quickstart_exportassets.py
Original file line number Diff line number Diff line change
@@ -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://<bucket-name>/asset_dump_file')

args = parser.parse_args()

export_assets(args.project_id, args.dump_file_path)
50 changes: 50 additions & 0 deletions asset/cloud-client/quickstart_exportassets_test.py
Original file line number Diff line number Diff line change
@@ -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