-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(samples): Adds code examples to create, get, list, update and d…
…elete 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 <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_update_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_list_saved_queries_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_update_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_update_saved_query_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_get_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_create_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_create_saved_query_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_create_saved_query_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_delete_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_delete_saved_query_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_get_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_get_saved_query.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_get_saved_query_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_list_saved_queries.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_list_saved_queries.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update samples/snippets/quickstart_list_saved_queries_test.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * 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 <71398022+dandhlee@users.noreply.github.com>
- Loading branch information
1 parent
28f51da
commit 1df7389
Showing
12 changed files
with
400 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/google-cloud-asset/samples/snippets/quickstart_create_saved_query.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import argparse | ||
|
||
|
||
def create_saved_query(project_id, saved_query_id, description): | ||
# [START asset_quickstart_create_saved_query] | ||
from google.cloud import asset_v1 | ||
|
||
# TODO project_id = 'Your Google Cloud Project ID' | ||
# TODO saved_query_id = 'SavedQuery ID you want to create' | ||
client = asset_v1.AssetServiceClient() | ||
parent = f"projects/{project_id}" | ||
saved_query = asset_v1.SavedQuery() | ||
saved_query.description = description | ||
|
||
# TODO: customize your saved query based on the guide below. | ||
# https://cloud.google.com/asset-inventory/docs/reference/rest/v1/savedQueries#IamPolicyAnalysisQuery | ||
saved_query.content.iam_policy_analysis_query.scope = parent | ||
query_access_selector = saved_query.content.iam_policy_analysis_query.access_selector | ||
query_access_selector.permissions.append("iam.serviceAccounts.actAs") | ||
|
||
response = client.create_saved_query( | ||
request={ | ||
"parent": parent, | ||
"saved_query_id": saved_query_id, | ||
"saved_query": saved_query, | ||
} | ||
) | ||
print(f"saved_query: {response}") | ||
# [END asset_quickstart_create_saved_query] | ||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("project_id", help="Your Google Cloud project ID") | ||
parser.add_argument("saved_query_id", help="SavedQuery ID you want to create") | ||
parser.add_argument("description", help="The description of the saved_query") | ||
args = parser.parse_args() | ||
create_saved_query(args.project_id, args.saved_query_id, args.description) |
32 changes: 32 additions & 0 deletions
32
packages/google-cloud-asset/samples/snippets/quickstart_create_saved_query_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import uuid | ||
|
||
import quickstart_create_saved_query | ||
|
||
|
||
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
SAVED_QUERY_ID = f"saved-query-{uuid.uuid4().hex}" | ||
|
||
|
||
def test_create_saved_query(capsys, saved_query_deleter): | ||
saved_query = quickstart_create_saved_query.create_saved_query( | ||
PROJECT, SAVED_QUERY_ID, "saved query foo") | ||
saved_query_deleter.append(saved_query.name) | ||
expected_resource_name_suffix = f"savedQueries/{SAVED_QUERY_ID}" | ||
assert saved_query.name.endswith(expected_resource_name_suffix) |
39 changes: 39 additions & 0 deletions
39
packages/google-cloud-asset/samples/snippets/quickstart_delete_saved_query.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import argparse | ||
|
||
|
||
def delete_saved_query(saved_query_name): | ||
# [START asset_quickstart_delete_saved_query] | ||
from google.cloud import asset_v1 | ||
|
||
# TODO saved_query_name = 'SavedQuery name you want to delete' | ||
|
||
client = asset_v1.AssetServiceClient() | ||
client.delete_saved_query(request={"name": saved_query_name}) | ||
print("deleted_saved_query") | ||
# [END asset_quickstart_delete_saved_query] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("saved_query_name", help="SavedQuery name you want to delete") | ||
args = parser.parse_args() | ||
delete_saved_query(args.saved_query_name) |
30 changes: 30 additions & 0 deletions
30
packages/google-cloud-asset/samples/snippets/quickstart_delete_saved_query_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import quickstart_delete_saved_query | ||
|
||
|
||
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
|
||
|
||
def test_delete_saved_query(capsys, test_saved_query): | ||
|
||
quickstart_delete_saved_query.delete_saved_query(test_saved_query.name) | ||
|
||
out, _ = capsys.readouterr() | ||
assert "deleted_saved_query" in out |
39 changes: 39 additions & 0 deletions
39
packages/google-cloud-asset/samples/snippets/quickstart_get_saved_query.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import argparse | ||
|
||
|
||
def get_saved_query(saved_query_name): | ||
# [START asset_quickstart_get_saved_query] | ||
from google.cloud import asset_v1 | ||
|
||
# TODO saved_query_name = 'SavedQuery Name you want to get' | ||
|
||
client = asset_v1.AssetServiceClient() | ||
response = client.get_saved_query(request={"name": saved_query_name}) | ||
print(f"gotten_saved_query: {response}") | ||
# [END asset_quickstart_get_saved_query] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("saved_query_name", help="SavedQuery Name you want to get") | ||
args = parser.parse_args() | ||
get_saved_query(args.saved_query_name) |
24 changes: 24 additions & 0 deletions
24
packages/google-cloud-asset/samples/snippets/quickstart_get_saved_query_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import quickstart_get_saved_query | ||
|
||
|
||
def test_get_saved_query(capsys, test_saved_query): | ||
quickstart_get_saved_query.get_saved_query(test_saved_query.name) | ||
out, _ = capsys.readouterr() | ||
|
||
assert "gotten_saved_query" in out |
41 changes: 41 additions & 0 deletions
41
packages/google-cloud-asset/samples/snippets/quickstart_list_saved_queries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import argparse | ||
|
||
|
||
def list_saved_queries(parent_resource): | ||
# [START asset_quickstart_list_saved_queries] | ||
from google.cloud import asset_v1 | ||
|
||
# TODO parent_resource = 'Parent resource you want to list all saved_queries' | ||
|
||
client = asset_v1.AssetServiceClient() | ||
response = client.list_saved_queries(request={"parent": parent_resource}) | ||
print(f"saved_queries: {response.saved_queries}") | ||
# [END asset_quickstart_list_saved_queries] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument( | ||
"parent_resource", help="Parent resource you want to list all saved_queries" | ||
) | ||
args = parser.parse_args() | ||
list_saved_queries(args.parent_resource) |
28 changes: 28 additions & 0 deletions
28
packages/google-cloud-asset/samples/snippets/quickstart_list_saved_queries_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import quickstart_list_saved_queries | ||
|
||
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
|
||
|
||
def test_list_saved_queries(capsys): | ||
parent_resource = f"projects/{PROJECT}" | ||
quickstart_list_saved_queries.list_saved_queries(parent_resource) | ||
out, _ = capsys.readouterr() | ||
assert "saved_queries" in out |
Oops, something went wrong.