Skip to content

Commit d295f9f

Browse files
peter-zheng-ggguuss
authored andcommitted
[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.".
1 parent 56244a8 commit d295f9f

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import argparse
19+
20+
21+
def export_assets(project_id, dump_file_path):
22+
# [START asset_quickstart_exportassets]
23+
from google.cloud import asset_v1beta1
24+
from google.cloud.asset_v1beta1.proto import asset_service_pb2
25+
26+
# TODO project_id = "Your Google Cloud Project ID"
27+
# TODO dump_file_path = "Your asset dump file path"
28+
29+
client = asset_v1beta1.AssetServiceClient()
30+
parent = client.project_path(project_id)
31+
output_config = asset_service_pb2.OutputConfig()
32+
output_config.gcs_destination.uri = dump_file_path
33+
response = client.export_assets(parent, output_config)
34+
print(response.result)
35+
# [END asset_quickstart_exportassets]
36+
37+
38+
if __name__ == '__main__':
39+
40+
parser = argparse.ArgumentParser(
41+
description=__doc__,
42+
formatter_class=argparse.RawDescriptionHelpFormatter
43+
)
44+
parser.add_argument('project_id', help='Your Google Cloud project ID')
45+
parser.add_argument('dump_file_path',
46+
help='The file ExportAssets API will dump assets to, '
47+
'e.g.: gs://<bucket-name>/asset_dump_file')
48+
49+
args = parser.parse_args()
50+
51+
export_assets(args.project_id, args.dump_file_path)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
from google.cloud import storage
20+
import pytest
21+
22+
import quickstart_exportassets
23+
24+
PROJECT = os.environ['GCLOUD_PROJECT']
25+
BUCKET = 'bucket-for-assets'
26+
27+
28+
@pytest.fixture(scope='module')
29+
def storage_client():
30+
yield storage.Client()
31+
32+
33+
@pytest.fixture(scope='module')
34+
def asset_bucket(storage_client):
35+
storage_client.create_bucket(BUCKET)
36+
37+
try:
38+
storage_client.delete_bucket(BUCKET)
39+
except Exception:
40+
pass
41+
42+
yield BUCKET
43+
44+
45+
def test_export_assets(asset_bucket, capsys):
46+
dump_file_path = "gs://", asset_bucket, "/assets-dump.txt"
47+
quickstart_exportassets.export_assets(PROJECT, dump_file_path)
48+
out, _ = capsys.readouterr()
49+
50+
assert "uri: \"gs://cai-prober-prod-for-assets/phython-test.txt\"" in out

0 commit comments

Comments
 (0)