Skip to content

Commit 825f82c

Browse files
gcf-owl-bot[bot]partheaSita04
authored
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: https://github.com/googleapis/googleapis-gen/commit/729529edc103e45087ffae8353eaf009ad7fe8c2 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: https://github.com/googleapis/googleapis-gen/commit/6f1e4cd013ab2914773826e68b2a2d0763030a39 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: https://github.com/googleapis/googleapis-gen/commit/4313d682880fd9d7247291164d4e9d3d5bd9f177 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: https://github.com/googleapis/googleapis-gen/commit/b64b1e7da3e138f15ca361552ef0545e54891b4f 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: https://github.com/googleapis/googleapis-gen/commit/1ee1a06c6de3ca8b843572c1fde0548f84236989 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: https://github.com/googleapis/googleapis-gen/commit/c8504e97891ed9e664cf68270d7e61bec160fe57 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]>
1 parent 2830899 commit 825f82c

10 files changed

+56
-37
lines changed

iam/cloud-client/__init__.py

Whitespace-only changes.

iam/cloud-client/snippets/conftest.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616
import re
1717
import uuid
1818

19-
from _pytest.capture import CaptureFixture
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
2021
import pytest
21-
22-
from create_deny_policy import create_deny_policy
23-
from delete_deny_policy import delete_deny_policy
22+
from samples.snippets.create_deny_policy import create_deny_policy
23+
from samples.snippets.delete_deny_policy import delete_deny_policy
2424

2525
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
2626
GOOGLE_APPLICATION_CREDENTIALS = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
2727

2828

2929
@pytest.fixture
30-
def deny_policy(capsys: CaptureFixture) -> None:
31-
policy_id = f"limit-project-deletion-{uuid.uuid4()}"
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")
3235

3336
# Create the Deny policy.
3437
create_deny_policy(PROJECT_ID, policy_id)
@@ -39,3 +42,15 @@ def deny_policy(capsys: CaptureFixture) -> None:
3942
delete_deny_policy(PROJECT_ID, policy_id)
4043
out, _ = capsys.readouterr()
4144
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])

iam/cloud-client/snippets/create_deny_policy.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919

2020
def create_deny_policy(project_id: str, policy_id: str) -> None:
21-
from google.cloud import iam_v2beta
22-
from google.cloud.iam_v2beta import types
23-
from google.type import expr_pb2
21+
from google.cloud import iam_v2
22+
from google.cloud.iam_v2 import types
2423

2524
"""
2625
Create a deny policy.
@@ -36,7 +35,7 @@ def create_deny_policy(project_id: str, policy_id: str) -> None:
3635
project_id: ID or number of the Google Cloud project you want to use.
3736
policy_id: Specify the ID of the deny policy you want to create.
3837
"""
39-
policies_client = iam_v2beta.PoliciesClient()
38+
policies_client = iam_v2.PoliciesClient()
4039

4140
# Each deny policy is attached to an organization, folder, or project.
4241
# To work with deny policies, specify the attachment point.
@@ -100,9 +99,9 @@ def create_deny_policy(project_id: str, policy_id: str) -> None:
10099
request.policy = policy
101100
request.policy_id = policy_id
102101

103-
# Build the create policy request.
104-
policies_client.create_policy(request=request)
105-
print(f"Created the deny policy: {policy_id}")
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]}")
106105

107106

108107
if __name__ == "__main__":

iam/cloud-client/snippets/delete_deny_policy.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
# [START iam_delete_deny_policy]
1818
def delete_deny_policy(project_id: str, policy_id: str) -> None:
19-
from google.cloud import iam_v2beta
20-
from google.cloud.iam_v2beta import types
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
2121

2222
"""
2323
Delete the policy if you no longer want to enforce the rules in a deny policy.
2424
2525
project_id: ID or number of the Google Cloud project you want to use.
2626
policy_id: The ID of the deny policy you want to retrieve.
2727
"""
28-
policies_client = iam_v2beta.PoliciesClient()
28+
policies_client = iam_v2.PoliciesClient()
2929

3030
# Each deny policy is attached to an organization, folder, or project.
3131
# To work with deny policies, specify the attachment point.
@@ -45,8 +45,8 @@ def delete_deny_policy(project_id: str, policy_id: str) -> None:
4545
request.name = f"policies/{attachment_point}/denypolicies/{policy_id}"
4646

4747
# Create the DeletePolicy request.
48-
policies_client.delete_policy(request=request)
49-
print(f"Deleted the deny policy: {policy_id}")
48+
result = policies_client.delete_policy(request=request).result()
49+
print(f"Deleted the deny policy: {result.name.rsplit('/')[-1]}")
5050

5151

5252
if __name__ == "__main__":

iam/cloud-client/snippets/get_deny_policy.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
# This file contains code samples that demonstrate how to get IAM deny policies.
1616

1717
# [START iam_get_deny_policy]
18-
def get_deny_policy(project_id: str, policy_id: str):
19-
from google.cloud import iam_v2beta
20-
from google.cloud.iam_v2beta import Policy, types
18+
from google.cloud import iam_v2
19+
from google.cloud.iam_v2 import Policy, types
2120

21+
22+
def get_deny_policy(project_id: str, policy_id: str) -> Policy:
2223
"""
2324
Retrieve the deny policy given the project ID and policy ID.
2425
2526
project_id: ID or number of the Google Cloud project you want to use.
2627
policy_id: The ID of the deny policy you want to retrieve.
2728
"""
28-
policies_client = iam_v2beta.PoliciesClient()
29+
policies_client = iam_v2.PoliciesClient()
2930

3031
# Each deny policy is attached to an organization, folder, or project.
3132
# To work with deny policies, specify the attachment point.

iam/cloud-client/snippets/list_deny_policies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
# [START iam_list_deny_policy]
1818
def list_deny_policy(project_id: str) -> None:
19-
from google.cloud import iam_v2beta
20-
from google.cloud.iam_v2beta import types
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
2121

2222
"""
2323
List all the deny policies that are attached to a resource.
2424
A resource can have up to 5 deny policies.
2525
2626
project_id: ID or number of the Google Cloud project you want to use.
2727
"""
28-
policies_client = iam_v2beta.PoliciesClient()
28+
policies_client = iam_v2.PoliciesClient()
2929

3030
# Each deny policy is attached to an organization, folder, or project.
3131
# To work with deny policies, specify the attachment point.

iam/cloud-client/snippets/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# build specific Cloud project. You can also use your own string
3232
# to use your own Cloud project.
3333
# "gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34-
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
34+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
3535
# A dictionary you want to inject into your test. Don't put any
3636
# secrets here. These values will override predefined values.
3737
"envs": {},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==7.1.2

iam/cloud-client/snippets/test_deny_policies.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,35 @@
1515
import os
1616
import re
1717

18-
from _pytest.capture import CaptureFixture
18+
import pytest
1919
from samples.snippets.get_deny_policy import get_deny_policy
2020
from samples.snippets.list_deny_policies import list_deny_policy
2121
from samples.snippets.update_deny_policy import update_deny_policy
2222

23-
PROJECT_ID = os.environ["PROJECT_ID"]
23+
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
2424
GOOGLE_APPLICATION_CREDENTIALS = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
2525

2626

27-
def test_retrieve_policy(capsys: CaptureFixture, deny_policy) -> None:
27+
def test_retrieve_policy(
28+
capsys: "pytest.CaptureFixture[str]", deny_policy: str
29+
) -> None:
2830
# Test policy retrieval, given the policy id.
2931
get_deny_policy(PROJECT_ID, deny_policy)
3032
out, _ = capsys.readouterr()
3133
assert re.search(f"Retrieved the deny policy: {deny_policy}", out)
3234

3335

34-
def test_list_policies(capsys: CaptureFixture, deny_policy) -> None:
36+
def test_list_policies(capsys: "pytest.CaptureFixture[str]", deny_policy: str) -> None:
3537
# Check if the created policy is listed.
3638
list_deny_policy(PROJECT_ID)
3739
out, _ = capsys.readouterr()
3840
assert re.search(deny_policy, out)
3941
assert re.search("Listed all deny policies", out)
4042

4143

42-
def test_update_deny_policy(capsys: CaptureFixture, deny_policy) -> None:
44+
def test_update_deny_policy(
45+
capsys: "pytest.CaptureFixture[str]", deny_policy: str
46+
) -> None:
4347
# Check if the policy rule is updated.
4448
policy = get_deny_policy(PROJECT_ID, deny_policy)
4549
update_deny_policy(PROJECT_ID, deny_policy, policy.etag)

iam/cloud-client/snippets/update_deny_policy.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
# [START iam_update_deny_policy]
1818
def update_deny_policy(project_id: str, policy_id: str, etag: str) -> None:
19-
from google.cloud import iam_v2beta
20-
from google.cloud.iam_v2beta import types
21-
from google.type import expr_pb2
19+
from google.cloud import iam_v2
20+
from google.cloud.iam_v2 import types
2221

2322
"""
2423
Update the deny rules and/ or its display name after policy creation.
@@ -30,7 +29,7 @@ def update_deny_policy(project_id: str, policy_id: str, etag: str) -> None:
3029
etag: Etag field that identifies the policy version. The etag changes each time
3130
you update the policy. Get the etag of an existing policy by performing a GetPolicy request.
3231
"""
33-
policies_client = iam_v2beta.PoliciesClient()
32+
policies_client = iam_v2.PoliciesClient()
3433

3534
# Each deny policy is attached to an organization, folder, or project.
3635
# To work with deny policies, specify the attachment point.
@@ -94,8 +93,8 @@ def update_deny_policy(project_id: str, policy_id: str, etag: str) -> None:
9493
request = types.UpdatePolicyRequest()
9594
request.policy = policy
9695

97-
policies_client.update_policy(request=request)
98-
print(f"Updated the deny policy: {policy_id}")
96+
result = policies_client.update_policy(request=request).result()
97+
print(f"Updated the deny policy: {result.name.rsplit('/')[-1]}")
9998

10099

101100
if __name__ == "__main__":

0 commit comments

Comments
 (0)