-
Notifications
You must be signed in to change notification settings - Fork 6.6k
docs(iam): Update comments and terminology in IAM samples #13010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36066a8
Update modify_policy_add_member.py comments
melaniedejong 9a0e403
Update modify_policy_remove_member.py
melaniedejong 0489a1a
Update quickstart.py comments
melaniedejong af9c528
Update iam_modify_policy_add_role.py
melaniedejong f9e3efc
Apply code review suggestions to replace "member" with "principal" in…
melaniedejong 98de6b8
Update iam/cloud-client/snippets/quickstart.py doc string
melaniedejong e506016
Update quickstart.py: replace "member" with "principal" in additional…
melaniedejong 0e9619f
Replace "member" with "principal" to fix failing test
melaniedejong afc6303
Replace "member" with "principal" to fix failing test
melaniedejong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,30 +18,38 @@ | |
from google.iam.v1 import iam_policy_pb2, policy_pb2 | ||
|
||
|
||
def quickstart(project_id: str, member: str) -> None: | ||
"""Gets a policy, adds a member, prints their permissions, and removes the member. | ||
def quickstart(project_id: str, principal: str) -> None: | ||
"""Demonstrates basic IAM operations. | ||
|
||
This quickstart shows how to get a project's IAM policy, add a principal to a role, list members of a role, and remove a principal from a role. | ||
|
||
Args: | ||
project_id: The ID or number of the Google Cloud project. | ||
principal: The principal ID. | ||
""" | ||
"""Gets a policy, adds a principal, prints their permissions, and removes the principal. | ||
|
||
project_id: ID or number of the Google Cloud project you want to use. | ||
member: The principals requesting the access. | ||
principal: The principal requesting the access. | ||
""" | ||
|
||
# Role to be granted. | ||
role = "roles/logging.logWriter" | ||
crm_service = resourcemanager_v3.ProjectsClient() | ||
|
||
# Grants your member the 'Log Writer' role for the project. | ||
modify_policy_add_role(crm_service, project_id, role, member) | ||
# Grants your principal the 'Log Writer' role for the project. | ||
modify_policy_add_role(crm_service, project_id, role, principal) | ||
|
||
# Gets the project's policy and prints all members with the 'Log Writer' role. | ||
# Gets the project's policy and prints all principals with the 'Log Writer' role. | ||
policy = get_policy(crm_service, project_id) | ||
binding = next(b for b in policy.bindings if b.role == role) | ||
print(f"Role: {(binding.role)}") | ||
print("Members: ") | ||
iennae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for m in binding.members: | ||
print(f"[{m}]") | ||
|
||
# Removes the member from the 'Log Writer' role. | ||
modify_policy_remove_member(crm_service, project_id, role, member) | ||
# Removes the principal from the 'Log Writer' role. | ||
modify_policy_remove_member(crm_service, project_id, role, principal) | ||
|
||
|
||
def get_policy( | ||
|
@@ -74,20 +82,20 @@ def modify_policy_add_role( | |
crm_service: resourcemanager_v3.ProjectsClient, | ||
project_id: str, | ||
role: str, | ||
member: str, | ||
principal: str, | ||
) -> None: | ||
"""Adds a new role binding to a policy.""" | ||
|
||
policy = get_policy(crm_service, project_id) | ||
|
||
for bind in policy.bindings: | ||
if bind.role == role: | ||
bind.members.append(member) | ||
bind.members.append(principal) | ||
break | ||
else: | ||
binding = policy_pb2.Binding() | ||
binding.role = role | ||
binding.members.append(member) | ||
binding.members.append(principal) | ||
policy.bindings.append(binding) | ||
|
||
set_policy(crm_service, project_id, policy) | ||
|
@@ -97,16 +105,16 @@ def modify_policy_remove_member( | |
crm_service: resourcemanager_v3.ProjectsClient, | ||
project_id: str, | ||
role: str, | ||
member: str, | ||
principal: str, | ||
) -> None: | ||
"""Removes a member from a role binding.""" | ||
"""Removes a principal from a role binding.""" | ||
|
||
policy = get_policy(crm_service, project_id) | ||
|
||
for bind in policy.bindings: | ||
if bind.role == role: | ||
if member in bind.members: | ||
bind.members.remove(member) | ||
if principal in bind.members: | ||
bind.members.remove(principal) | ||
break | ||
|
||
set_policy(crm_service, project_id, policy) | ||
|
@@ -115,7 +123,8 @@ def modify_policy_remove_member( | |
if __name__ == "__main__": | ||
# TODO: replace with your project ID | ||
project_id = "your-project-id" | ||
# TODO: Replace with the ID of your member in the form 'user:[email protected]'. | ||
member = "your-member" | ||
quickstart(project_id, member) | ||
# TODO: Replace with the ID of your principal. | ||
# For examples, see https://cloud.google.com/iam/docs/principal-identifiers | ||
melaniedejong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
principal = "your-principal" | ||
quickstart(project_id, principal) | ||
# [END iam_quickstart] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.