-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.".
- Loading branch information
1 parent
56244a8
commit d295f9f
Showing
2 changed files
with
101 additions
and
0 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
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) |
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,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 |