Skip to content

Commit 796eac7

Browse files
committed
fix: limit list size in test
1 parent 799906d commit 796eac7

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

asset/snippets/quickstart_listassets.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import argparse
1919

2020

21-
def list_assets(project_id, asset_types, page_size, content_type):
21+
def list_assets(project_id, asset_types, page_size, content_type, max_results=0):
2222
# [START asset_quickstart_list_assets]
2323
from google.cloud import asset_v1
2424

@@ -28,6 +28,7 @@ def list_assets(project_id, asset_types, page_size, content_type):
2828
# TODO page_size = 'Num of assets in one page, which must be between 1 and
2929
# 1000 (both inclusively)'
3030
# TODO content_type ="Content type to list"
31+
# max_results - total number of assets to return. max_results=0 returns all
3132

3233
project_resource = "projects/{}".format(project_id)
3334
client = asset_v1.AssetServiceClient()
@@ -43,13 +44,12 @@ def list_assets(project_id, asset_types, page_size, content_type):
4344
}
4445
)
4546

46-
# DEBUGGING - remove before merging. See if timeouts are because there are so many assets being listed
47-
count = 0 # DEBUGGING
47+
count = 0
4848
for asset in response:
4949
print(asset)
50-
count += 1 # DEBUGGING
51-
if count > 3 * page_size: # DEBUGGING
52-
break # DEBUGGING - three pages is plenty for a test
50+
count += 1
51+
if max_results > 0 and count >= max_results:
52+
break
5353
# [END asset_quickstart_list_assets]
5454

5555

@@ -74,4 +74,6 @@ def list_assets(project_id, asset_types, page_size, content_type):
7474

7575
asset_type_list = args.asset_types.split(",")
7676

77-
list_assets(args.project_id, asset_type_list, int(args.page_size), args.content_type)
77+
list_assets(
78+
args.project_id, asset_type_list, int(args.page_size), args.content_type
79+
)

asset/snippets/quickstart_listassets_test.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,21 @@ def test_list_assets(capsys):
2525
from google.cloud import asset_v1
2626

2727
quickstart_listassets.list_assets(
28-
project_id=PROJECT, asset_types=["iam.googleapis.com/Role"], page_size=10,
29-
content_type=asset_v1.ContentType.RESOURCE)
28+
project_id=PROJECT,
29+
asset_types=["iam.googleapis.com/Role"],
30+
page_size=10,
31+
content_type=asset_v1.ContentType.RESOURCE,
32+
max_results=30,
33+
)
3034
out, _ = capsys.readouterr()
3135
assert "asset" in out
3236

3337
quickstart_listassets.list_assets(
34-
project_id=PROJECT, asset_types=[], page_size=10, content_type=asset_v1.ContentType.RELATIONSHIP)
38+
project_id=PROJECT,
39+
asset_types=[],
40+
page_size=10,
41+
content_type=asset_v1.ContentType.RELATIONSHIP,
42+
max_results=30,
43+
)
3544
out_r, _ = capsys.readouterr()
3645
assert "asset" in out_r

0 commit comments

Comments
 (0)