Skip to content

Commit 8f28cb6

Browse files
peter-zheng-ggguuss
authored andcommitted
[Asset] Add quickstart code for BatchGetAssetsHistory API. (GoogleCloudPlatform#1867)
* [Asset] Test: fix bucket clean up logic. * [Asset] Add quickstart code for BatchGetAssetsHistory API.
1 parent 7e5bd2c commit 8f28cb6

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 batch_get_assets_history(project_id, asset_names):
22+
# [START asset_quickstart_batch_get_assets_history]
23+
from google.cloud import asset_v1beta1
24+
from google.cloud.asset_v1beta1.proto import assets_pb2
25+
from google.cloud.asset_v1beta1 import enums
26+
27+
# TODO project_id = 'Your Google Cloud Project ID'
28+
# TODO asset_names = 'Your asset names list, e.g.:
29+
# ["//storage.googleapis.com/[BUCKET_NAME]",]'
30+
31+
client = asset_v1beta1.AssetServiceClient()
32+
parent = client.project_path(project_id)
33+
content_type = enums.ContentType.RESOURCE
34+
read_time_window = assets_pb2.TimeWindow()
35+
read_time_window.start_time.GetCurrentTime()
36+
response = client.batch_get_assets_history(
37+
parent, content_type, read_time_window, asset_names)
38+
print('assets: {}'.format(response.assets))
39+
# [END asset_quickstart_batch_get_assets_history]
40+
41+
42+
if __name__ == '__main__':
43+
parser = argparse.ArgumentParser(
44+
description=__doc__,
45+
formatter_class=argparse.RawDescriptionHelpFormatter
46+
)
47+
parser.add_argument('project_id', help='Your Google Cloud project ID')
48+
parser.add_argument(
49+
'asset_names',
50+
help='The asset names for which history will be fetched, comma '
51+
'delimited, e.g.: //storage.googleapis.com/[BUCKET_NAME]')
52+
53+
args = parser.parse_args()
54+
55+
asset_name_list = args.asset_names.split(',')
56+
57+
batch_get_assets_history(args.project_id, asset_name_list)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
import os
18+
import time
19+
20+
from google.cloud import storage
21+
import pytest
22+
23+
import quickstart_batchgetassetshistory
24+
25+
PROJECT = os.environ['GCLOUD_PROJECT']
26+
BUCKET = 'assets-{}'.format(int(time.time()))
27+
28+
29+
@pytest.fixture(scope='module')
30+
def storage_client():
31+
yield storage.Client()
32+
33+
34+
@pytest.fixture(scope='module')
35+
def asset_bucket(storage_client):
36+
bucket = storage_client.create_bucket(BUCKET)
37+
38+
yield BUCKET
39+
40+
try:
41+
bucket.delete(force=True)
42+
except Exception as e:
43+
print('Failed to delete bucket{}'.format(BUCKET))
44+
raise e
45+
46+
47+
def test_batch_get_assets_history(asset_bucket, capsys):
48+
bucket_asset_name = '//storage.googleapis.com/{}'.format(BUCKET)
49+
asset_names = [bucket_asset_name, ]
50+
quickstart_batchgetassetshistory.batch_get_assets_history(
51+
PROJECT, asset_names)
52+
out, _ = capsys.readouterr()
53+
54+
if not out:
55+
assert bucket_asset_name in out

asset/cloud-client/quickstart_exportassets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def export_assets(project_id, dump_file_path):
22-
# [START asset_quickstart_exportassets]
22+
# [START asset_quickstart_export_assets]
2323
from google.cloud import asset_v1beta1
2424
from google.cloud.asset_v1beta1.proto import asset_service_pb2
2525

@@ -32,7 +32,7 @@ def export_assets(project_id, dump_file_path):
3232
output_config.gcs_destination.uri = dump_file_path
3333
response = client.export_assets(parent, output_config)
3434
print(response.result())
35-
# [END asset_quickstart_exportassets]
35+
# [END asset_quickstart_export_assets]
3636

3737

3838
if __name__ == '__main__':

0 commit comments

Comments
 (0)