diff --git a/packages/google-cloud-asset/samples/snippets/quickstart_batchgeteffectiveiampolicy.py b/packages/google-cloud-asset/samples/snippets/quickstart_batchgeteffectiveiampolicy.py new file mode 100644 index 000000000000..3240c64add32 --- /dev/null +++ b/packages/google-cloud-asset/samples/snippets/quickstart_batchgeteffectiveiampolicy.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +# Copyright 2022 Google LLC. All Rights Reserved. +# +# 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 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 resource_names = 'List of resource names' + + client = asset_v1.AssetServiceClient() + + response = client.batch_get_effective_iam_policies( + request={"scope": scope, "names": resource_names} + ) + print(response) + # [END asset_quickstart_batch_get_effective_iam_policies] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + parser.add_argument("resource_names", help="Your specified accessible " + "scope, such as a project, " + "folder or organization") + parser.add_argument("scope", help="Your specified list of resource names") + + args = parser.parse_args() + + batch_get_effective_iam_policies(args.resource_names, args.scope) diff --git a/packages/google-cloud-asset/samples/snippets/quickstart_batchgeteffectiveiampolicy_test.py b/packages/google-cloud-asset/samples/snippets/quickstart_batchgeteffectiveiampolicy_test.py new file mode 100644 index 000000000000..7fdaaa73742c --- /dev/null +++ b/packages/google-cloud-asset/samples/snippets/quickstart_batchgeteffectiveiampolicy_test.py @@ -0,0 +1,31 @@ +#!/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_batchgeteffectiveiampolicy + +PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] + + +def test_batch_get_effective_iam_policies(capsys): + scope = "projects/{}".format(PROJECT) + resource_names = [ + "//cloudresourcemanager.googleapis.com/projects/{}".format(PROJECT)] + quickstart_batchgeteffectiveiampolicy.batch_get_effective_iam_policies( + resource_names, scope) + out, _ = capsys.readouterr() + assert resource_names[0] in out