Skip to content

Commit

Permalink
docs(samples): add batch_get_effective_iam_policies sample code (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
haozhang-google authored and dandhlee committed Nov 29, 2022
1 parent 094e6d9 commit a08ab03
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
48 changes: 48 additions & 0 deletions asset/snippets/snippets/quickstart_batchgeteffectiveiampolicy.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a08ab03

Please sign in to comment.