Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 3e935de

Browse files
yuyifan-googlebusunkim96
authored andcommitted
feat(asset): Add sample code for two new RPCs. [(#4080)](GoogleCloudPlatform/python-docs-samples#4080)
1 parent f57321f commit 3e935de

5 files changed

+204
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 search_all_iam_policies(scope, query=None, page_size=None):
22+
# [START asset_quickstart_search_all_iam_policies]
23+
from google.cloud import asset_v1
24+
25+
client = asset_v1.AssetServiceClient()
26+
response = client.search_all_iam_policies(
27+
scope, query=query, page_size=page_size)
28+
for page in response.pages:
29+
for policy in page:
30+
print(policy)
31+
break
32+
# [END asset_quickstart_search_all_iam_policies]
33+
34+
35+
if __name__ == '__main__':
36+
parser = argparse.ArgumentParser(
37+
description=__doc__,
38+
formatter_class=argparse.RawDescriptionHelpFormatter)
39+
parser.add_argument(
40+
'scope',
41+
help='The search is limited to the resources within the scope.')
42+
parser.add_argument('--query', help='The query statement.')
43+
parser.add_argument(
44+
'--page_size',
45+
type=int,
46+
help='The page size for search result pagination.')
47+
args = parser.parse_args()
48+
search_all_iam_policies(args.scope, args.query, args.page_size)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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_searchalliampolicies
20+
21+
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
22+
23+
24+
def test_search_all_iam_policies(capsys):
25+
scope = "projects/{}".format(PROJECT)
26+
query = "policy:roles/owner"
27+
quickstart_searchalliampolicies.search_all_iam_policies(scope, query=query)
28+
out, _ = capsys.readouterr()
29+
assert "roles/owner" in out
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 search_all_resources(scope,
22+
query=None,
23+
asset_types=None,
24+
page_size=None,
25+
order_by=None):
26+
# [START asset_quickstart_search_all_resources]
27+
from google.cloud import asset_v1
28+
29+
client = asset_v1.AssetServiceClient()
30+
response = client.search_all_resources(
31+
scope,
32+
query=query,
33+
asset_types=asset_types,
34+
page_size=page_size,
35+
order_by=order_by)
36+
for page in response.pages:
37+
for resource in page:
38+
print(resource)
39+
break
40+
# [END asset_quickstart_search_all_resources]
41+
42+
43+
if __name__ == '__main__':
44+
parser = argparse.ArgumentParser(
45+
description=__doc__,
46+
formatter_class=argparse.RawDescriptionHelpFormatter)
47+
parser.add_argument(
48+
'scope',
49+
help='The search is limited to the resources within the scope.')
50+
parser.add_argument('--query', help='The query statement.')
51+
parser.add_argument(
52+
'--asset_types', nargs='+', help='A list of asset types to search for.')
53+
parser.add_argument(
54+
'--page_size',
55+
type=int,
56+
help='The page size for search result pagination.')
57+
parser.add_argument(
58+
'--order_by',
59+
help='Fields specifying the sorting order of the results.')
60+
args = parser.parse_args()
61+
search_all_resources(args.scope, args.query, args.asset_types,
62+
args.page_size, args.order_by)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
import uuid
19+
20+
import backoff
21+
from google.api_core.exceptions import NotFound
22+
from google.cloud import bigquery
23+
import pytest
24+
25+
import quickstart_searchallresources
26+
27+
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
28+
DATASET = 'dataset_{}'.format(uuid.uuid4().hex)
29+
30+
31+
@pytest.fixture(scope='module')
32+
def bigquery_client():
33+
yield bigquery.Client()
34+
35+
36+
@pytest.fixture(scope='module')
37+
def asset_dataset(bigquery_client):
38+
dataset = bigquery_client.create_dataset(DATASET)
39+
40+
yield DATASET
41+
42+
try:
43+
bigquery_client.delete_dataset(dataset)
44+
except NotFound as e:
45+
print('Failed to delete dataset {}'.format(DATASET))
46+
raise e
47+
48+
49+
def test_search_all_resources(asset_dataset, capsys):
50+
scope = "projects/{}".format(PROJECT)
51+
query = "name:{}".format(DATASET)
52+
53+
# Dataset creation takes some time to propagate, so the dataset is not
54+
# immediately searchable. Need some time before the snippet will pass.
55+
@backoff.on_exception(
56+
backoff.expo, (AssertionError), max_time=30
57+
)
58+
def eventually_consistent_test():
59+
quickstart_searchallresources.search_all_resources(scope, query=query)
60+
out, _ = capsys.readouterr()
61+
62+
assert DATASET in out
63+
64+
eventually_consistent_test()

samples/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ google-cloud-storage==1.28.1
22
google-cloud-asset==1.1.0
33
google-cloud-resource-manager==0.30.2
44
google-cloud-pubsub==1.5.0
5+
google-cloud-bigquery==1.25.0

0 commit comments

Comments
 (0)