Skip to content

Commit 7e07eae

Browse files
authored
feat: add sample code for ListAssets v1p5beta1 (#4251)
## Description Fixes #4250 Note: It's a good idea to open an issue first for discussion. ## Checklist - [x] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file) - [x] **Tests** pass: `nox -s py-3.6` (see [Test Enviroment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [x] **Lint** pass: `nox -s lint` (see [Test Enviroment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [x] Please **merge** this PR for me once it is approved.
1 parent c9bf218 commit 7e07eae

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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 list_assets(project_id, asset_types, page_size):
22+
# [START asset_quickstart_list_assets]
23+
from google.cloud import asset_v1p5beta1
24+
from google.cloud.asset_v1p5beta1 import enums
25+
26+
# TODO project_id = 'Your Google Cloud Project ID'
27+
# TODO asset_types = 'Your asset type list, e.g.,
28+
# ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]'
29+
# TODO page_size = 'Num of assets in one page, which must be between 1 and
30+
# 1000 (both inclusively)'
31+
32+
project_resource = 'projects/{}'.format(project_id)
33+
content_type = enums.ContentType.RESOURCE
34+
client = asset_v1p5beta1.AssetServiceClient()
35+
36+
# Call ListAssets v1p5beta1 to list assets.
37+
response = client.list_assets(
38+
parent=project_resource, read_time=None, asset_types=asset_types,
39+
content_type=content_type, page_size=page_size)
40+
41+
for page in response.pages:
42+
for asset in page:
43+
print(asset)
44+
# [END asset_quickstart_list_assets]
45+
46+
47+
if __name__ == '__main__':
48+
parser = argparse.ArgumentParser(
49+
description=__doc__,
50+
formatter_class=argparse.RawDescriptionHelpFormatter
51+
)
52+
parser.add_argument('project_id', help='Your Google Cloud project ID')
53+
parser.add_argument(
54+
'asset_types',
55+
help='The types of the assets to list, comma delimited, e.g., '
56+
'storage.googleapis.com/Bucket')
57+
parser.add_argument(
58+
'page_size',
59+
help='Num of assets in one page, which must be between 1 and 1000 '
60+
'(both inclusively)')
61+
62+
args = parser.parse_args()
63+
64+
asset_type_list = args.asset_types.split(',')
65+
66+
list_assets(args.project_id, asset_type_list, int(args.page_size))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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+
19+
import quickstart_listassets
20+
21+
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
22+
23+
24+
def test_list_assets(capsys):
25+
quickstart_listassets.list_assets(project_id=PROJECT, asset_types=[], page_size=10)
26+
out, _ = capsys.readouterr()
27+
assert 'asset' in out

0 commit comments

Comments
 (0)