Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions iam/cloud-client/snippets/modify_policy_add_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@


def modify_policy_add_member(
project_id: str, role: str, member: str
project_id: str, role: str, principal: str
) -> policy_pb2.Policy:
"""
Add a principal to certain role in project policy.

project_id: ID or number of the Google Cloud project you want to use.
role: role to which principal need to be added.
member: The principal requesting access.
principal: The principal requesting access.

For principal ID formats, see https://cloud.google.com/iam/docs/principal-identifiers
"""
policy = get_project_policy(project_id)

for bind in policy.bindings:
if bind.role == role:
bind.members.append(member)
bind.members.append(principal)
break

return set_project_policy(project_id, policy)
Expand All @@ -52,4 +52,4 @@ def modify_policy_add_member(
role = "roles/viewer"
member = f"serviceAccount:test-service-account@{project_id}.iam.gserviceaccount.com"

modify_policy_add_member(project_id, role, member)
modify_policy_add_member(project_id, role, principal)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The variable principal is not defined. Should this be using the member variable defined on line 53? Or should we rename that variable to principal to match the function parameter?

Suggested change
modify_policy_add_member(project_id, role, principal)
modify_policy_add_member(project_id, role, member)

10 changes: 5 additions & 5 deletions iam/cloud-client/snippets/modify_policy_remove_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@


def modify_policy_remove_member(
project_id: str, role: str, member: str
project_id: str, role: str, principal: str
) -> policy_pb2.Policy:
"""
Remove a principal from certain role in project policy.

project_id: ID or number of the Google Cloud project you want to use.
role: role to revoke.
member: The principal to revoke access from.
principal: The principal to revoke access from.

For principal ID formats, see https://cloud.google.com/iam/docs/principal-identifiers
"""
Expand All @@ -35,7 +35,7 @@ def modify_policy_remove_member(
for bind in policy.bindings:
if bind.role == role:
if member in bind.members:
bind.members.remove(member)
bind.members.remove(principal)
Comment on lines 37 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The variable member is still used here. To maintain consistency, should this be changed to principal to match the function parameter and docstring?

Suggested change
if member in bind.members:
bind.members.remove(member)
bind.members.remove(principal)
if principal in bind.members:
bind.members.remove(principal)

break

return set_project_policy(project_id, policy, False)
Expand All @@ -51,6 +51,6 @@ def modify_policy_remove_member(
# Your Google Cloud project ID.
project_id = "test-project-id"
role = "roles/viewer"
member = f"serviceAccount:test-service-account@{project_id}.iam.gserviceaccount.com"
principal = f"serviceAccount:test-service-account@{project_id}.iam.gserviceaccount.com"

modify_policy_remove_member(project_id, role, member)
modify_policy_remove_member(project_id, role, principal)