Skip to content

Commit 8518cd7

Browse files
chore(samples): Adds code examples to create, get, list, update and delete a SavedQuery (#499)
* Change the comment of BatchGetEffectiveIamPolicies to be more accurate. * feat: Adds code examples to create, get, list, update and delete a SavedQuery. * feat: Adds code examples to create, get, list, update and delete a SavedQuery. * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_update_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_list_saved_queries_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_update_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_update_saved_query_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_get_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_create_saved_query_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_create_saved_query_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_delete_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_delete_saved_query_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_get_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_get_saved_query.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_get_saved_query_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_list_saved_queries.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_list_saved_queries.py Co-authored-by: Dan Lee <[email protected]> * Update samples/snippets/quickstart_list_saved_queries_test.py Co-authored-by: Dan Lee <[email protected]> * feat: Adds code examples to create, get, list, update and delete a SavedQuery. * feat: Adds code examples to create, get, list, update and delete a SavedQuery. * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <[email protected]>
1 parent 97dede1 commit 8518cd7

12 files changed

+400
-1
lines changed

asset/snippets/snippets/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from google.cloud import pubsub_v1
2424
import pytest
2525

26+
import quickstart_create_saved_query
2627
import quickstart_createfeed
28+
import quickstart_delete_saved_query
2729
import quickstart_deletefeed
2830

2931

@@ -88,3 +90,36 @@ def deleter():
8890
quickstart_deletefeed.delete_feed(feed_name)
8991
except NotFound as e:
9092
print(f"Ignoring NotFound: {e}")
93+
94+
95+
@pytest.fixture(scope="module")
96+
def test_saved_query():
97+
saved_query_id = f"saved-query-{uuid.uuid4().hex}"
98+
99+
@backoff.on_exception(backoff.expo, InternalServerError, max_time=60)
100+
def create_saved_query():
101+
return quickstart_create_saved_query.create_saved_query(
102+
PROJECT, saved_query_id, "description foo"
103+
)
104+
105+
saved_query = create_saved_query()
106+
107+
yield saved_query
108+
109+
try:
110+
quickstart_delete_saved_query.delete_saved_query(saved_query.name)
111+
except NotFound as e:
112+
print(f"Ignoring NotFound: {e}")
113+
114+
115+
@pytest.fixture(scope="module")
116+
def saved_query_deleter():
117+
saved_querys_to_delete = []
118+
119+
yield saved_querys_to_delete
120+
121+
for saved_query_name in saved_querys_to_delete:
122+
try:
123+
quickstart_delete_saved_query.delete_saved_query(saved_query_name)
124+
except NotFound as e:
125+
print(f"Ignoring NotFound: {e}")

asset/snippets/snippets/quickstart_batchgeteffectiveiampolicy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def batch_get_effective_iam_policies(resource_names, scope):
2222
# [START asset_quickstart_batch_get_effective_iam_policies]
2323
from google.cloud import asset_v1
2424

25-
# TODO scope = 'Scope for resource names'
25+
# TODO scope = 'project ID/number, folder number or org number'
2626
# TODO resource_names = 'List of resource names'
2727

2828
client = asset_v1.AssetServiceClient()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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 create_saved_query(project_id, saved_query_id, description):
22+
# [START asset_quickstart_create_saved_query]
23+
from google.cloud import asset_v1
24+
25+
# TODO project_id = 'Your Google Cloud Project ID'
26+
# TODO saved_query_id = 'SavedQuery ID you want to create'
27+
client = asset_v1.AssetServiceClient()
28+
parent = f"projects/{project_id}"
29+
saved_query = asset_v1.SavedQuery()
30+
saved_query.description = description
31+
32+
# TODO: customize your saved query based on the guide below.
33+
# https://cloud.google.com/asset-inventory/docs/reference/rest/v1/savedQueries#IamPolicyAnalysisQuery
34+
saved_query.content.iam_policy_analysis_query.scope = parent
35+
query_access_selector = saved_query.content.iam_policy_analysis_query.access_selector
36+
query_access_selector.permissions.append("iam.serviceAccounts.actAs")
37+
38+
response = client.create_saved_query(
39+
request={
40+
"parent": parent,
41+
"saved_query_id": saved_query_id,
42+
"saved_query": saved_query,
43+
}
44+
)
45+
print(f"saved_query: {response}")
46+
# [END asset_quickstart_create_saved_query]
47+
return response
48+
49+
50+
if __name__ == "__main__":
51+
parser = argparse.ArgumentParser(
52+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
53+
)
54+
parser.add_argument("project_id", help="Your Google Cloud project ID")
55+
parser.add_argument("saved_query_id", help="SavedQuery ID you want to create")
56+
parser.add_argument("description", help="The description of the saved_query")
57+
args = parser.parse_args()
58+
create_saved_query(args.project_id, args.saved_query_id, args.description)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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 quickstart_create_saved_query
21+
22+
23+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
SAVED_QUERY_ID = f"saved-query-{uuid.uuid4().hex}"
25+
26+
27+
def test_create_saved_query(capsys, saved_query_deleter):
28+
saved_query = quickstart_create_saved_query.create_saved_query(
29+
PROJECT, SAVED_QUERY_ID, "saved query foo")
30+
saved_query_deleter.append(saved_query.name)
31+
expected_resource_name_suffix = f"savedQueries/{SAVED_QUERY_ID}"
32+
assert saved_query.name.endswith(expected_resource_name_suffix)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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 delete_saved_query(saved_query_name):
22+
# [START asset_quickstart_delete_saved_query]
23+
from google.cloud import asset_v1
24+
25+
# TODO saved_query_name = 'SavedQuery name you want to delete'
26+
27+
client = asset_v1.AssetServiceClient()
28+
client.delete_saved_query(request={"name": saved_query_name})
29+
print("deleted_saved_query")
30+
# [END asset_quickstart_delete_saved_query]
31+
32+
33+
if __name__ == "__main__":
34+
parser = argparse.ArgumentParser(
35+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
36+
)
37+
parser.add_argument("saved_query_name", help="SavedQuery name you want to delete")
38+
args = parser.parse_args()
39+
delete_saved_query(args.saved_query_name)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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_delete_saved_query
20+
21+
22+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
24+
25+
def test_delete_saved_query(capsys, test_saved_query):
26+
27+
quickstart_delete_saved_query.delete_saved_query(test_saved_query.name)
28+
29+
out, _ = capsys.readouterr()
30+
assert "deleted_saved_query" in out
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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 get_saved_query(saved_query_name):
22+
# [START asset_quickstart_get_saved_query]
23+
from google.cloud import asset_v1
24+
25+
# TODO saved_query_name = 'SavedQuery Name you want to get'
26+
27+
client = asset_v1.AssetServiceClient()
28+
response = client.get_saved_query(request={"name": saved_query_name})
29+
print(f"gotten_saved_query: {response}")
30+
# [END asset_quickstart_get_saved_query]
31+
32+
33+
if __name__ == "__main__":
34+
parser = argparse.ArgumentParser(
35+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
36+
)
37+
parser.add_argument("saved_query_name", help="SavedQuery Name you want to get")
38+
args = parser.parse_args()
39+
get_saved_query(args.saved_query_name)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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 quickstart_get_saved_query
18+
19+
20+
def test_get_saved_query(capsys, test_saved_query):
21+
quickstart_get_saved_query.get_saved_query(test_saved_query.name)
22+
out, _ = capsys.readouterr()
23+
24+
assert "gotten_saved_query" in out
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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_saved_queries(parent_resource):
22+
# [START asset_quickstart_list_saved_queries]
23+
from google.cloud import asset_v1
24+
25+
# TODO parent_resource = 'Parent resource you want to list all saved_queries'
26+
27+
client = asset_v1.AssetServiceClient()
28+
response = client.list_saved_queries(request={"parent": parent_resource})
29+
print(f"saved_queries: {response.saved_queries}")
30+
# [END asset_quickstart_list_saved_queries]
31+
32+
33+
if __name__ == "__main__":
34+
parser = argparse.ArgumentParser(
35+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
36+
)
37+
parser.add_argument(
38+
"parent_resource", help="Parent resource you want to list all saved_queries"
39+
)
40+
args = parser.parse_args()
41+
list_saved_queries(args.parent_resource)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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_list_saved_queries
20+
21+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
23+
24+
def test_list_saved_queries(capsys):
25+
parent_resource = f"projects/{PROJECT}"
26+
quickstart_list_saved_queries.list_saved_queries(parent_resource)
27+
out, _ = capsys.readouterr()
28+
assert "saved_queries" in out

0 commit comments

Comments
 (0)