|
18 | 18 | from google.iam.v1 import iam_policy_pb2, policy_pb2
|
19 | 19 |
|
20 | 20 |
|
21 |
| -def quickstart(project_id: str, member: str) -> None: |
22 |
| - """Gets a policy, adds a member, prints their permissions, and removes the member. |
| 21 | +def quickstart(project_id: str, principal: str) -> None: |
| 22 | + """Demonstrates basic IAM operations. |
23 | 23 |
|
24 |
| - project_id: ID or number of the Google Cloud project you want to use. |
25 |
| - member: The principals requesting the access. |
| 24 | + This quickstart shows how to get a project's IAM policy, |
| 25 | + add a principal to a role, list members of a role, |
| 26 | + and remove a principal from a role. |
| 27 | +
|
| 28 | + Args: |
| 29 | + project_id: ID or number of the Google Cloud project you want to use. |
| 30 | + principal: The principal ID requesting the access. |
26 | 31 | """
|
27 | 32 |
|
28 | 33 | # Role to be granted.
|
29 | 34 | role = "roles/logging.logWriter"
|
30 | 35 | crm_service = resourcemanager_v3.ProjectsClient()
|
31 | 36 |
|
32 |
| - # Grants your member the 'Log Writer' role for the project. |
33 |
| - modify_policy_add_role(crm_service, project_id, role, member) |
| 37 | + # Grants your principal the 'Log Writer' role for the project. |
| 38 | + modify_policy_add_role(crm_service, project_id, role, principal) |
34 | 39 |
|
35 |
| - # Gets the project's policy and prints all members with the 'Log Writer' role. |
| 40 | + # Gets the project's policy and prints all principals with the 'Log Writer' role. |
36 | 41 | policy = get_policy(crm_service, project_id)
|
37 | 42 | binding = next(b for b in policy.bindings if b.role == role)
|
38 | 43 | print(f"Role: {(binding.role)}")
|
39 | 44 | print("Members: ")
|
40 | 45 | for m in binding.members:
|
41 | 46 | print(f"[{m}]")
|
42 | 47 |
|
43 |
| - # Removes the member from the 'Log Writer' role. |
44 |
| - modify_policy_remove_member(crm_service, project_id, role, member) |
| 48 | + # Removes the principal from the 'Log Writer' role. |
| 49 | + modify_policy_remove_principal(crm_service, project_id, role, principal) |
45 | 50 |
|
46 | 51 |
|
47 | 52 | def get_policy(
|
@@ -74,48 +79,49 @@ def modify_policy_add_role(
|
74 | 79 | crm_service: resourcemanager_v3.ProjectsClient,
|
75 | 80 | project_id: str,
|
76 | 81 | role: str,
|
77 |
| - member: str, |
| 82 | + principal: str, |
78 | 83 | ) -> None:
|
79 | 84 | """Adds a new role binding to a policy."""
|
80 | 85 |
|
81 | 86 | policy = get_policy(crm_service, project_id)
|
82 | 87 |
|
83 | 88 | for bind in policy.bindings:
|
84 | 89 | if bind.role == role:
|
85 |
| - bind.members.append(member) |
| 90 | + bind.members.append(principal) |
86 | 91 | break
|
87 | 92 | else:
|
88 | 93 | binding = policy_pb2.Binding()
|
89 | 94 | binding.role = role
|
90 |
| - binding.members.append(member) |
| 95 | + binding.members.append(principal) |
91 | 96 | policy.bindings.append(binding)
|
92 | 97 |
|
93 | 98 | set_policy(crm_service, project_id, policy)
|
94 | 99 |
|
95 | 100 |
|
96 |
| -def modify_policy_remove_member( |
| 101 | +def modify_policy_remove_principal( |
97 | 102 | crm_service: resourcemanager_v3.ProjectsClient,
|
98 | 103 | project_id: str,
|
99 | 104 | role: str,
|
100 |
| - member: str, |
| 105 | + principal: str, |
101 | 106 | ) -> None:
|
102 |
| - """Removes a member from a role binding.""" |
| 107 | + """Removes a principal from a role binding.""" |
103 | 108 |
|
104 | 109 | policy = get_policy(crm_service, project_id)
|
105 | 110 |
|
106 | 111 | for bind in policy.bindings:
|
107 | 112 | if bind.role == role:
|
108 |
| - if member in bind.members: |
109 |
| - bind.members.remove(member) |
| 113 | + if principal in bind.members: |
| 114 | + bind.members.remove(principal) |
110 | 115 | break
|
111 | 116 |
|
112 | 117 | set_policy(crm_service, project_id, policy)
|
113 | 118 |
|
114 | 119 |
|
115 | 120 | if __name__ == "__main__":
|
116 |
| - # TODO: Replace with your project ID |
| 121 | + # TODO: Replace with your project ID. |
117 | 122 | project_id = "your-project-id"
|
118 |
| - # TODO: Replace with the ID of your member in the form 'user:[email protected]'. |
119 |
| - member = "your-member" |
120 |
| - quickstart(project_id, member) |
| 123 | + # TODO: Replace with the ID of your principal. |
| 124 | + # For examples, see https://cloud.google.com/iam/docs/principal-identifiers |
| 125 | + principal = "your-principal" |
| 126 | + quickstart(project_id, principal) |
121 | 127 | # [END iam_quickstart]
|
0 commit comments