Skip to content

Commit

Permalink
chore(samples): Adds code examples to create, get, list, update and d…
Browse files Browse the repository at this point in the history
…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
jeffreyaiatgoogle and dandhlee authored Nov 18, 2022
1 parent 28f51da commit 1df7389
Show file tree
Hide file tree
Showing 12 changed files with 400 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/google-cloud-asset/samples/snippets/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
from google.cloud import pubsub_v1
import pytest

import quickstart_create_saved_query
import quickstart_createfeed
import quickstart_delete_saved_query
import quickstart_deletefeed


Expand Down Expand Up @@ -88,3 +90,36 @@ def deleter():
quickstart_deletefeed.delete_feed(feed_name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")


@pytest.fixture(scope="module")
def test_saved_query():
saved_query_id = f"saved-query-{uuid.uuid4().hex}"

@backoff.on_exception(backoff.expo, InternalServerError, max_time=60)
def create_saved_query():
return quickstart_create_saved_query.create_saved_query(
PROJECT, saved_query_id, "description foo"
)

saved_query = create_saved_query()

yield saved_query

try:
quickstart_delete_saved_query.delete_saved_query(saved_query.name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")


@pytest.fixture(scope="module")
def saved_query_deleter():
saved_querys_to_delete = []

yield saved_querys_to_delete

for saved_query_name in saved_querys_to_delete:
try:
quickstart_delete_saved_query.delete_saved_query(saved_query_name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def batch_get_effective_iam_policies(resource_names, scope):
# [START asset_quickstart_batch_get_effective_iam_policies]
from google.cloud import asset_v1

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

client = asset_v1.AssetServiceClient()
Expand Down
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)
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)
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)
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
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)
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
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)
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
Loading

0 comments on commit 1df7389

Please sign in to comment.