diff --git a/samples/snippets/quickstart_searchalliampolicies.py b/samples/snippets/quickstart_searchalliampolicies.py new file mode 100644 index 00000000..334f1dda --- /dev/null +++ b/samples/snippets/quickstart_searchalliampolicies.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +# Copyright 2020 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 search_all_iam_policies(scope, query=None, page_size=None): + # [START asset_quickstart_search_all_iam_policies] + from google.cloud import asset_v1 + + client = asset_v1.AssetServiceClient() + response = client.search_all_iam_policies( + scope, query=query, page_size=page_size) + for page in response.pages: + for policy in page: + print(policy) + break + # [END asset_quickstart_search_all_iam_policies] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument( + 'scope', + help='The search is limited to the resources within the scope.') + parser.add_argument('--query', help='The query statement.') + parser.add_argument( + '--page_size', + type=int, + help='The page size for search result pagination.') + args = parser.parse_args() + search_all_iam_policies(args.scope, args.query, args.page_size) diff --git a/samples/snippets/quickstart_searchalliampolicies_test.py b/samples/snippets/quickstart_searchalliampolicies_test.py new file mode 100644 index 00000000..e1068cb9 --- /dev/null +++ b/samples/snippets/quickstart_searchalliampolicies_test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +# Copyright 2020 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_searchalliampolicies + +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] + + +def test_search_all_iam_policies(capsys): + scope = "projects/{}".format(PROJECT) + query = "policy:roles/owner" + quickstart_searchalliampolicies.search_all_iam_policies(scope, query=query) + out, _ = capsys.readouterr() + assert "roles/owner" in out diff --git a/samples/snippets/quickstart_searchallresources.py b/samples/snippets/quickstart_searchallresources.py new file mode 100644 index 00000000..7668ffc1 --- /dev/null +++ b/samples/snippets/quickstart_searchallresources.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright 2020 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 search_all_resources(scope, + query=None, + asset_types=None, + page_size=None, + order_by=None): + # [START asset_quickstart_search_all_resources] + from google.cloud import asset_v1 + + client = asset_v1.AssetServiceClient() + response = client.search_all_resources( + scope, + query=query, + asset_types=asset_types, + page_size=page_size, + order_by=order_by) + for page in response.pages: + for resource in page: + print(resource) + break + # [END asset_quickstart_search_all_resources] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument( + 'scope', + help='The search is limited to the resources within the scope.') + parser.add_argument('--query', help='The query statement.') + parser.add_argument( + '--asset_types', nargs='+', help='A list of asset types to search for.') + parser.add_argument( + '--page_size', + type=int, + help='The page size for search result pagination.') + parser.add_argument( + '--order_by', + help='Fields specifying the sorting order of the results.') + args = parser.parse_args() + search_all_resources(args.scope, args.query, args.asset_types, + args.page_size, args.order_by) diff --git a/samples/snippets/quickstart_searchallresources_test.py b/samples/snippets/quickstart_searchallresources_test.py new file mode 100644 index 00000000..ae79a69f --- /dev/null +++ b/samples/snippets/quickstart_searchallresources_test.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Copyright 2020 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 backoff +from google.api_core.exceptions import NotFound +from google.cloud import bigquery +import pytest + +import quickstart_searchallresources + +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] +DATASET = 'dataset_{}'.format(uuid.uuid4().hex) + + +@pytest.fixture(scope='module') +def bigquery_client(): + yield bigquery.Client() + + +@pytest.fixture(scope='module') +def asset_dataset(bigquery_client): + dataset = bigquery_client.create_dataset(DATASET) + + yield DATASET + + try: + bigquery_client.delete_dataset(dataset) + except NotFound as e: + print('Failed to delete dataset {}'.format(DATASET)) + raise e + + +def test_search_all_resources(asset_dataset, capsys): + scope = "projects/{}".format(PROJECT) + query = "name:{}".format(DATASET) + + # Dataset creation takes some time to propagate, so the dataset is not + # immediately searchable. Need some time before the snippet will pass. + @backoff.on_exception( + backoff.expo, (AssertionError), max_time=30 + ) + def eventually_consistent_test(): + quickstart_searchallresources.search_all_resources(scope, query=query) + out, _ = capsys.readouterr() + + assert DATASET in out + + eventually_consistent_test() diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index bac55b5d..1424289b 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -2,3 +2,4 @@ google-cloud-storage==1.28.1 google-cloud-asset==1.1.0 google-cloud-resource-manager==0.30.2 google-cloud-pubsub==1.5.0 +google-cloud-bigquery==1.25.0