|
21 | 21 | .. code-block:: python |
22 | 22 |
|
23 | 23 | # ``get_iam_policy`` returns a :class:'~google.api_core.iam.Policy`. |
24 | | - policy = resource.get_iam_policy() |
25 | | -
|
26 | | - phred = policy.user("phred@example.com") |
27 | | - admin_group = policy.group("admins@groups.example.com") |
28 | | - account = policy.service_account("account-1234@accounts.example.com") |
29 | | - policy["roles/owner"] = [phred, admin_group, account] |
30 | | - policy["roles/editor"] = policy.authenticated_users() |
31 | | - policy["roles/viewer"] = policy.all_users() |
| 24 | + policy = resource.get_iam_policy(requested_policy_version=3) |
| 25 | +
|
| 26 | + phred = "user:phred@example.com" |
| 27 | + admin_group = "group:admins@groups.example.com" |
| 28 | + account = "serviceAccount:account-1234@accounts.example.com" |
| 29 | +
|
| 30 | + policy.version = 3 |
| 31 | + policy.bindings = [ |
| 32 | + { |
| 33 | + "role": "roles/owner", |
| 34 | + "members": {phred, admin_group, account} |
| 35 | + }, |
| 36 | + { |
| 37 | + "role": "roles/editor", |
| 38 | + "members": "allAuthenticatedUsers" |
| 39 | + }, |
| 40 | + { |
| 41 | + "role": "roles/viewer", |
| 42 | + "members": "allUsers" |
| 43 | + "condition": { |
| 44 | + "title": "requested_time", |
| 45 | + "description": "Requests made before 2021-01-01T00:00:00Z", |
| 46 | + "expression": "request.time < timestamp(\"2021-01-01T00:00:00Z\")" |
| 47 | + } |
| 48 | + } |
| 49 | + ] |
32 | 50 |
|
33 | 51 | resource.set_iam_policy(policy) |
34 | 52 | """ |
@@ -141,12 +159,54 @@ def _contains_conditions(self): |
141 | 159 |
|
142 | 160 | @property |
143 | 161 | def bindings(self): |
144 | | - """Gets the policy's bindings.""" |
| 162 | + """:obj:`list` of :obj:`dict`: The policy's bindings list. |
| 163 | + :obj:`dict` Binding: |
| 164 | + role (str): Role that is assigned to `members`. |
| 165 | + members (:obj:`set` of str): Specifies the identities associated to this binding. |
| 166 | + condition (dict of str:str): Specifies a condition under which this binding will apply. |
| 167 | +
|
| 168 | + :obj:`dict` Condition: |
| 169 | + title (str): Title for the condition. |
| 170 | + description (:obj:str, optional): Description of the condition. |
| 171 | + expression: A CEL expression. |
| 172 | +
|
| 173 | + Note: |
| 174 | + Using conditions in bindings requires the policy's version to be set |
| 175 | + to `3`. |
| 176 | + Accessing the policy using dict operations will raise InvalidOperationException |
| 177 | + when the policy's version is set to 3. Use the policy.bindings getter/setter |
| 178 | + to retrieve and modify the policy's bindings. |
| 179 | +
|
| 180 | + See: |
| 181 | + Policy versions https://cloud.google.com/iam/docs/policies#versions |
| 182 | + Conditions overview https://cloud.google.com/iam/docs/conditions-overview. |
| 183 | +
|
| 184 | + Example: |
| 185 | + .. code-block:: python |
| 186 | + USER = "user:phred@example.com" |
| 187 | + ADMIN_GROUP = "group:admins@groups.example.com" |
| 188 | + SERVICE_ACCOUNT = "serviceAccount:account-1234@accounts.example.com" |
| 189 | +
|
| 190 | + # Set policy's version to 3 before setting bindings containing conditions. |
| 191 | + policy.version = 3 |
| 192 | +
|
| 193 | + policy.bindings = [ |
| 194 | + { |
| 195 | + "role": "roles/viewer", |
| 196 | + "members": {USER, ADMIN_GROUP, SERVICE_ACCOUNT}, |
| 197 | + "condition": { |
| 198 | + "title": "requested_time", |
| 199 | + "description": "Requests made before 2021-01-01T00:00:00Z", # Optional |
| 200 | + "expression": "request.time < timestamp(\"2021-01-01T00:00:00Z\")" |
| 201 | + } |
| 202 | + }, |
| 203 | + ... |
| 204 | + ] |
| 205 | + """ |
145 | 206 | return self._bindings |
146 | 207 |
|
147 | 208 | @bindings.setter |
148 | 209 | def bindings(self, bindings): |
149 | | - """Sets the policy's bindings.""" |
150 | 210 | self._bindings = bindings |
151 | 211 |
|
152 | 212 | @property |
|
0 commit comments