Skip to content

Commit 469adec

Browse files
donmccaslandarithmetic1728Sita04gcf-owl-bot[bot]nicain
authored
feat: migrate code from googleapis/python-iam (#8497)
* feat!: migrate to microgenerator (#26) * docs(samples): add deny samples and tests (#209) * docs(samples): init add deny samples and tests * docs(samples): added requirements.txt * docs(samples): minor update and refactoring * added nox files * added comments and minor refactoring * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added region tags * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added region tags * modified comments acc to review * modified comments acc to review * updated env var * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * modified acc to review comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * modified acc to review comments * added init.py * updated acc to review comments Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: nicain <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#217) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#218) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update dependency google-cloud-iam to v2.8.2 (#225) * chore: detect samples tests in nested directories (#236) Source-Link: googleapis/synthtool@50db768 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:e09366bdf0fd9c8976592988390b24d53583dd9f002d476934da43725adbb978 * feat: Add client for IAM Deny v2 API (#230) * feat: Create the public IAM Deny v2 API PiperOrigin-RevId: 470600752 Source-Link: googleapis/googleapis@dac66f6 Source-Link: googleapis/googleapis-gen@729529e Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzI5NTI5ZWRjMTAzZTQ1MDg3ZmZhZTgzNTNlYWYwMDlhZDdmZThjMiJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * regenerate files using cl/470713093 * workaround docstring formatting issue * add pytest to samples CI * lint * fix import statement in samples/snippets * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): migrate samples from iam_v2beta to iam_v2 * update required checks to include samples * use GOOGLE_CLOUD_PROJECT * fix imports in samples/snippets * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add pytest * chore(python): prepare for release of the iam/v2 python client PiperOrigin-RevId: 471240188 Source-Link: googleapis/googleapis@ea847a1 Source-Link: googleapis/googleapis-gen@6f1e4cd Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNmYxZTRjZDAxM2FiMjkxNDc3MzgyNmU2OGIyYTJkMDc2MzAzMGEzOSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat: Bump gapic-generator-python version to 1.3.0 PiperOrigin-RevId: 472561635 Source-Link: googleapis/googleapis@332ecf5 Source-Link: googleapis/googleapis-gen@4313d68 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNDMxM2Q2ODI4ODBmZDlkNzI0NzI5MTE2NGQ0ZTlkM2Q1YmQ5ZjE3NyJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: use gapic-generator-python 1.3.1 PiperOrigin-RevId: 472772457 Source-Link: googleapis/googleapis@855b74d Source-Link: googleapis/googleapis-gen@b64b1e7 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiYjY0YjFlN2RhM2UxMzhmMTVjYTM2MTU1MmVmMDU0NWU1NDg5MWI0ZiJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: integrate gapic-generator-python-1.4.1 and enable more py_test targets PiperOrigin-RevId: 473833416 Source-Link: googleapis/googleapis@565a550 Source-Link: googleapis/googleapis-gen@1ee1a06 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMWVlMWEwNmM2ZGUzY2E4Yjg0MzU3MmMxZmRlMDU0OGY4NDIzNjk4OSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * updated test to delete stale policies and avoid quota error * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat!: remove ListApplicablePolicies PiperOrigin-RevId: 475955031 Source-Link: googleapis/googleapis@65376f4 Source-Link: googleapis/googleapis-gen@c8504e9 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiYzg1MDRlOTc4OTFlZDllNjY0Y2Y2ODI3MGQ3ZTYxYmVjMTYwZmU1NyJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * samples: wait for the operation to complete * samples: minor refactoring * use project `python-docs-samples-tests` Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Sita Lakshmi Sangameswaran <[email protected]> Co-authored-by: SitaLakshmi <[email protected]> * chore(deps): update all dependencies (#244) * removing noxfile.py, adding CODEOWNERS and blunderbuss config * fixing up test infra * test infra fix * testing with secrets Co-authored-by: arithmetic1728 <[email protected]> Co-authored-by: Sita Lakshmi Sangameswaran <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: nicain <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: WhiteSource Renovate <[email protected]> Co-authored-by: WhiteSource Renovate <[email protected]> Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: SitaLakshmi <[email protected]> Co-authored-by: Maciej Strzelczyk <[email protected]> Co-authored-by: Karl Weinmeister <[email protected]>
1 parent 53d451e commit 469adec

15 files changed

+576
-1
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
/functions/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
5454
/functions/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
5555
/healthcare/**/* @noerog @GoogleCloudPlatform/python-samples-reviewers
56-
/iam/**/* @GoogleCloudPlatform/python-samples-reviewers
56+
/iam/api-client/**/* @GoogleCloudPlatform/python-samples-reviewers
57+
/iam/cloud-client/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
5758
/iap/**/* @GoogleCloudPlatform/python-samples-reviewers
5859
/iot/**/* @gcseh @GoogleCloudPlatform/api-iot @GoogleCloudPlatform/python-samples-reviewers
5960
/jobs/**/* @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ assign_issues_by:
7878
- 'api: healthcare'
7979
to:
8080
- noerog
81+
- labels:
82+
- 'api: iam'
83+
to:
84+
- GoogleCloudPlatform/dee-infra
8185
- labels:
8286
- 'api: iot'
8387
- 'api: cloudiot'

iam/cloud-client/AUTHORING_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md

iam/cloud-client/CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/CONTRIBUTING.md

iam/cloud-client/snippets/__init__.py

Whitespace-only changes.

iam/cloud-client/snippets/conftest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import re
17+
import uuid
18+
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
21+
import pytest
22+
from snippets.create_deny_policy import create_deny_policy
23+
from snippets.delete_deny_policy import delete_deny_policy
24+
25+
PROJECT_ID = os.environ["IAM_PROJECT_ID"]
26+
GOOGLE_APPLICATION_CREDENTIALS = os.environ["IAM_CREDENTIALS"]
27+
28+
29+
@pytest.fixture
30+
def deny_policy(capsys: "pytest.CaptureFixture[str]") -> None:
31+
policy_id = f"test-deny-policy-{uuid.uuid4()}"
32+
33+
# Delete any existing policies. Otherwise it might throw quota issue.
34+
delete_existing_deny_policies(PROJECT_ID, "test-deny-policy")
35+
36+
# Create the Deny policy.
37+
create_deny_policy(PROJECT_ID, policy_id)
38+
39+
yield policy_id
40+
41+
# Delete the Deny policy and assert if deleted.
42+
delete_deny_policy(PROJECT_ID, policy_id)
43+
out, _ = capsys.readouterr()
44+
assert re.search(f"Deleted the deny policy: {policy_id}", out)
45+
46+
47+
def delete_existing_deny_policies(project_id: str, delete_name_prefix: str) -> None:
48+
policies_client = iam_v2.PoliciesClient()
49+
50+
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}"
51+
52+
request = types.ListPoliciesRequest()
53+
request.parent = f"policies/{attachment_point}/denypolicies"
54+
for policy in policies_client.list_policies(request=request):
55+
if delete_name_prefix in policy.name:
56+
delete_deny_policy(PROJECT_ID, str(policy.name).rsplit("/", 1)[-1])
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This file contains code samples that demonstrate how to create IAM deny policies.
16+
17+
# [START iam_create_deny_policy]
18+
19+
20+
def create_deny_policy(project_id: str, policy_id: str) -> None:
21+
from google.cloud import iam_v2
22+
from google.cloud.iam_v2 import types
23+
24+
"""
25+
Create a deny policy.
26+
You can add deny policies to organizations, folders, and projects.
27+
Each of these resources can have up to 5 deny policies.
28+
29+
Deny policies contain deny rules, which specify the following:
30+
1. The permissions to deny and/or exempt.
31+
2. The principals that are denied, or exempted from denial.
32+
3. An optional condition on when to enforce the deny rules.
33+
34+
Params:
35+
project_id: ID or number of the Google Cloud project you want to use.
36+
policy_id: Specify the ID of the deny policy you want to create.
37+
"""
38+
policies_client = iam_v2.PoliciesClient()
39+
40+
# Each deny policy is attached to an organization, folder, or project.
41+
# To work with deny policies, specify the attachment point.
42+
#
43+
# Its format can be one of the following:
44+
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID
45+
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID
46+
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID
47+
#
48+
# The attachment point is identified by its URL-encoded resource name. Hence, replace
49+
# the "/" with "%2F".
50+
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}"
51+
52+
deny_rule = types.DenyRule()
53+
# Add one or more principals who should be denied the permissions specified in this rule.
54+
# For more information on allowed values, see: https://cloud.google.com/iam/help/deny/principal-identifiers
55+
deny_rule.denied_principals = ["principalSet://goog/public:all"]
56+
57+
# Optionally, set the principals who should be exempted from the
58+
# list of denied principals. For example, if you want to deny certain permissions
59+
# to a group but exempt a few principals, then add those here.
60+
# deny_rule.exception_principals = ["principalSet://goog/group/[email protected]"]
61+
62+
# Set the permissions to deny.
63+
# The permission value is of the format: service_fqdn/resource.action
64+
# For the list of supported permissions, see: https://cloud.google.com/iam/help/deny/supported-permissions
65+
deny_rule.denied_permissions = [
66+
"cloudresourcemanager.googleapis.com/projects.delete"
67+
]
68+
69+
# Optionally, add the permissions to be exempted from this rule.
70+
# Meaning, the deny rule will not be applicable to these permissions.
71+
# deny_rule.exception_permissions = ["cloudresourcemanager.googleapis.com/projects.create"]
72+
73+
# Set the condition which will enforce the deny rule.
74+
# If this condition is true, the deny rule will be applicable. Else, the rule will not be enforced.
75+
# The expression uses Common Expression Language syntax (CEL).
76+
# Here we block access based on tags.
77+
#
78+
# Here, we create a deny rule that denies the cloudresourcemanager.googleapis.com/projects.delete permission to everyone except [email protected] for resources that are tagged test.
79+
# A tag is a key-value pair that can be attached to an organization, folder, or project.
80+
# For more info, see: https://cloud.google.com/iam/docs/deny-access#create-deny-policy
81+
deny_rule.denial_condition = {
82+
"expression": "!resource.matchTag('12345678/env', 'test')"
83+
}
84+
85+
# Add the deny rule and a description for it.
86+
policy_rule = types.PolicyRule()
87+
policy_rule.description = "block all principals from deleting projects, unless the principal is a member of [email protected] and the project being deleted has a tag with the value test"
88+
policy_rule.deny_rule = deny_rule
89+
90+
policy = types.Policy()
91+
policy.display_name = "Restrict project deletion access"
92+
policy.rules = [policy_rule]
93+
94+
# Set the policy resource path, policy rules and a unique ID for the policy.
95+
request = types.CreatePolicyRequest()
96+
# Construct the full path of the resource's deny policies.
97+
# Its format is: "policies/{attachmentPoint}/denypolicies"
98+
request.parent = f"policies/{attachment_point}/denypolicies"
99+
request.policy = policy
100+
request.policy_id = policy_id
101+
102+
# Build the create policy request and wait for the operation to complete.
103+
result = policies_client.create_policy(request=request).result()
104+
print(f"Created the deny policy: {result.name.rsplit('/')[-1]}")
105+
106+
107+
if __name__ == "__main__":
108+
import uuid
109+
110+
# Your Google Cloud project ID.
111+
project_id = "your-google-cloud-project-id"
112+
# Any unique ID (0 to 63 chars) starting with a lowercase letter.
113+
policy_id = f"deny-{uuid.uuid4()}"
114+
115+
# Test the policy lifecycle.
116+
create_deny_policy(project_id, policy_id)
117+
118+
# [END iam_create_deny_policy]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This file contains code samples that demonstrate how to delete IAM deny policies.
16+
17+
# [START iam_delete_deny_policy]
18+
def delete_deny_policy(project_id: str, policy_id: str) -> None:
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
21+
22+
"""
23+
Delete the policy if you no longer want to enforce the rules in a deny policy.
24+
25+
project_id: ID or number of the Google Cloud project you want to use.
26+
policy_id: The ID of the deny policy you want to retrieve.
27+
"""
28+
policies_client = iam_v2.PoliciesClient()
29+
30+
# Each deny policy is attached to an organization, folder, or project.
31+
# To work with deny policies, specify the attachment point.
32+
#
33+
# Its format can be one of the following:
34+
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID
35+
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID
36+
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID
37+
#
38+
# The attachment point is identified by its URL-encoded resource name. Hence, replace
39+
# the "/" with "%2F".
40+
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}"
41+
42+
request = types.DeletePolicyRequest()
43+
# Construct the full path of the policy.
44+
# Its format is: "policies/{attachmentPoint}/denypolicies/{policyId}"
45+
request.name = f"policies/{attachment_point}/denypolicies/{policy_id}"
46+
47+
# Create the DeletePolicy request.
48+
result = policies_client.delete_policy(request=request).result()
49+
print(f"Deleted the deny policy: {result.name.rsplit('/')[-1]}")
50+
51+
52+
if __name__ == "__main__":
53+
import uuid
54+
55+
# Your Google Cloud project ID.
56+
project_id = "your-google-cloud-project-id"
57+
# Any unique ID (0 to 63 chars) starting with a lowercase letter.
58+
policy_id = f"deny-{uuid.uuid4()}"
59+
60+
delete_deny_policy(project_id, policy_id)
61+
62+
# [END iam_delete_deny_policy]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This file contains code samples that demonstrate how to get IAM deny policies.
16+
17+
# [START iam_get_deny_policy]
18+
from google.cloud import iam_v2
19+
from google.cloud.iam_v2 import Policy, types
20+
21+
22+
def get_deny_policy(project_id: str, policy_id: str) -> Policy:
23+
"""
24+
Retrieve the deny policy given the project ID and policy ID.
25+
26+
project_id: ID or number of the Google Cloud project you want to use.
27+
policy_id: The ID of the deny policy you want to retrieve.
28+
"""
29+
policies_client = iam_v2.PoliciesClient()
30+
31+
# Each deny policy is attached to an organization, folder, or project.
32+
# To work with deny policies, specify the attachment point.
33+
#
34+
# Its format can be one of the following:
35+
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID
36+
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID
37+
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID
38+
#
39+
# The attachment point is identified by its URL-encoded resource name. Hence, replace
40+
# the "/" with "%2F".
41+
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}"
42+
43+
request = types.GetPolicyRequest()
44+
# Construct the full path of the policy.
45+
# Its format is: "policies/{attachmentPoint}/denypolicies/{policyId}"
46+
request.name = f"policies/{attachment_point}/denypolicies/{policy_id}"
47+
48+
# Execute the GetPolicy request.
49+
policy = policies_client.get_policy(request=request)
50+
print(f"Retrieved the deny policy: {policy_id} : {policy}")
51+
return policy
52+
53+
54+
if __name__ == "__main__":
55+
import uuid
56+
57+
# Your Google Cloud project ID.
58+
project_id = "your-google-cloud-project-id"
59+
# Any unique ID (0 to 63 chars) starting with a lowercase letter.
60+
policy_id = f"deny-{uuid.uuid4()}"
61+
62+
policy = get_deny_policy(project_id, policy_id)
63+
64+
# [END iam_get_deny_policy]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This file contains code samples that demonstrate how to list IAM deny policies.
16+
17+
# [START iam_list_deny_policy]
18+
def list_deny_policy(project_id: str) -> None:
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
21+
22+
"""
23+
List all the deny policies that are attached to a resource.
24+
A resource can have up to 5 deny policies.
25+
26+
project_id: ID or number of the Google Cloud project you want to use.
27+
"""
28+
policies_client = iam_v2.PoliciesClient()
29+
30+
# Each deny policy is attached to an organization, folder, or project.
31+
# To work with deny policies, specify the attachment point.
32+
#
33+
# Its format can be one of the following:
34+
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID
35+
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID
36+
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID
37+
#
38+
# The attachment point is identified by its URL-encoded resource name. Hence, replace
39+
# the "/" with "%2F".
40+
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}"
41+
42+
request = types.ListPoliciesRequest()
43+
# Construct the full path of the resource's deny policies.
44+
# Its format is: "policies/{attachmentPoint}/denypolicies"
45+
request.parent = f"policies/{attachment_point}/denypolicies"
46+
47+
# Create a list request and iterate over the returned policies.
48+
policies = policies_client.list_policies(request=request)
49+
50+
for policy in policies:
51+
print(policy.name)
52+
print("Listed all deny policies")
53+
54+
55+
if __name__ == "__main__":
56+
import uuid
57+
58+
# Your Google Cloud project ID.
59+
project_id = "your-google-cloud-project-id"
60+
# Any unique ID (0 to 63 chars) starting with a lowercase letter.
61+
policy_id = f"deny-{uuid.uuid4()}"
62+
63+
list_deny_policy(project_id)
64+
65+
# [END iam_list_deny_policy]

0 commit comments

Comments
 (0)