15
15
16
16
For allowed roles / permissions, see:
17
17
https://cloud.google.com/iam/docs/understanding-roles
18
+
19
+ Example usage:
20
+
21
+ .. code-block:: python
22
+
23
+ # ``get_iam_policy`` returns a :class:'~google.api_core.iam.Policy`.
24
+ policy = resource.get_iam_policy()
25
+
26
+ phred = policy.user("[email protected] ")
27
+ admin_group = policy.group("[email protected] ")
28
+ account = policy.service_account("[email protected] ")
29
+ policy["roles/owner"] = [phred, admin_group, account]
30
+ policy["roles/editor"] = policy.authenticated_users()
31
+ policy["roles/viewer"] = policy.all_users()
32
+
33
+ resource.set_iam_policy(policy)
18
34
"""
19
35
20
36
import collections
@@ -45,11 +61,9 @@ class Policy(collections_abc.MutableMapping):
45
61
See
46
62
https://cloud.google.com/iam/reference/rest/v1/Policy
47
63
48
- :type etag: str
49
- :param etag: ETag used to identify a unique of the policy
50
-
51
- :type version: int
52
- :param version: unique version of the policy
64
+ Args:
65
+ etag (Optional[str]): ETag used to identify a unique of the policy
66
+ version (Optional[int]): unique version of the policy
53
67
"""
54
68
55
69
_OWNER_ROLES = (OWNER_ROLE ,)
@@ -83,7 +97,9 @@ def __delitem__(self, key):
83
97
84
98
@property
85
99
def owners (self ):
86
- """Legacy access to owner role."""
100
+ """Legacy access to owner role.
101
+
102
+ DEPRECATED: use ``policy["roles/owners"]`` instead."""
87
103
result = set ()
88
104
for role in self ._OWNER_ROLES :
89
105
for member in self ._bindings .get (role , ()):
@@ -92,15 +108,19 @@ def owners(self):
92
108
93
109
@owners .setter
94
110
def owners (self , value ):
95
- """Update owners."""
111
+ """Update owners.
112
+
113
+ DEPRECATED: use ``policy["roles/owners"] = value`` instead."""
96
114
warnings .warn (
97
115
_ASSIGNMENT_DEPRECATED_MSG .format ("owners" , OWNER_ROLE ), DeprecationWarning
98
116
)
99
117
self [OWNER_ROLE ] = value
100
118
101
119
@property
102
120
def editors (self ):
103
- """Legacy access to editor role."""
121
+ """Legacy access to editor role.
122
+
123
+ DEPRECATED: use ``policy["roles/editors"]`` instead."""
104
124
result = set ()
105
125
for role in self ._EDITOR_ROLES :
106
126
for member in self ._bindings .get (role , ()):
@@ -109,7 +129,9 @@ def editors(self):
109
129
110
130
@editors .setter
111
131
def editors (self , value ):
112
- """Update editors."""
132
+ """Update editors.
133
+
134
+ DEPRECATED: use ``policy["roles/editors"] = value`` instead."""
113
135
warnings .warn (
114
136
_ASSIGNMENT_DEPRECATED_MSG .format ("editors" , EDITOR_ROLE ),
115
137
DeprecationWarning ,
@@ -118,7 +140,10 @@ def editors(self, value):
118
140
119
141
@property
120
142
def viewers (self ):
121
- """Legacy access to viewer role."""
143
+ """Legacy access to viewer role.
144
+
145
+ DEPRECATED: use ``policy["roles/viewers"]`` instead
146
+ """
122
147
result = set ()
123
148
for role in self ._VIEWER_ROLES :
124
149
for member in self ._bindings .get (role , ()):
@@ -127,7 +152,10 @@ def viewers(self):
127
152
128
153
@viewers .setter
129
154
def viewers (self , value ):
130
- """Update viewers."""
155
+ """Update viewers.
156
+
157
+ DEPRECATED: use ``policy["roles/viewers"] = value`` instead.
158
+ """
131
159
warnings .warn (
132
160
_ASSIGNMENT_DEPRECATED_MSG .format ("viewers" , VIEWER_ROLE ),
133
161
DeprecationWarning ,
@@ -138,77 +166,77 @@ def viewers(self, value):
138
166
def user (email ):
139
167
"""Factory method for a user member.
140
168
141
- :type email: str
142
- :param email: E-mail for this particular user.
169
+ Args:
170
+ email (str) : E-mail for this particular user.
143
171
144
- :rtype: str
145
- :returns : A member string corresponding to the given user.
172
+ Returns:
173
+ str : A member string corresponding to the given user.
146
174
"""
147
175
return "user:%s" % (email ,)
148
176
149
177
@staticmethod
150
178
def service_account (email ):
151
179
"""Factory method for a service account member.
152
180
153
- :type email: str
154
- :param email: E-mail for this particular service account.
181
+ Args:
182
+ email (str) : E-mail for this particular service account.
155
183
156
- :rtype: str
157
- :returns : A member string corresponding to the given service account.
184
+ Returns:
185
+ str : A member string corresponding to the given service account.
158
186
"""
159
187
return "serviceAccount:%s" % (email ,)
160
188
161
189
@staticmethod
162
190
def group (email ):
163
191
"""Factory method for a group member.
164
192
165
- :type email: str
166
- :param email: An id or e-mail for this particular group.
193
+ Args:
194
+ email (str) : An id or e-mail for this particular group.
167
195
168
- :rtype: str
169
- :returns : A member string corresponding to the given group.
196
+ Returns:
197
+ str : A member string corresponding to the given group.
170
198
"""
171
199
return "group:%s" % (email ,)
172
200
173
201
@staticmethod
174
202
def domain (domain ):
175
203
"""Factory method for a domain member.
176
204
177
- :type domain: str
178
- :param domain: The domain for this member.
205
+ Args:
206
+ domain (str) : The domain for this member.
179
207
180
- :rtype: str
181
- :returns : A member string corresponding to the given domain.
208
+ Returns:
209
+ str : A member string corresponding to the given domain.
182
210
"""
183
211
return "domain:%s" % (domain ,)
184
212
185
213
@staticmethod
186
214
def all_users ():
187
215
"""Factory method for a member representing all users.
188
216
189
- :rtype: str
190
- :returns : A member string representing all users.
217
+ Returns:
218
+ str : A member string representing all users.
191
219
"""
192
220
return "allUsers"
193
221
194
222
@staticmethod
195
223
def authenticated_users ():
196
224
"""Factory method for a member representing all authenticated users.
197
225
198
- :rtype: str
199
- :returns : A member string representing all authenticated users.
226
+ Returns:
227
+ str : A member string representing all authenticated users.
200
228
"""
201
229
return "allAuthenticatedUsers"
202
230
203
231
@classmethod
204
232
def from_api_repr (cls , resource ):
205
- """Create a policy from the resource returned from the API .
233
+ """Factory: create a policy from a JSON resource .
206
234
207
- :type resource: dict
208
- :param resource: resource returned from the ``getIamPolicy`` API.
235
+ Args:
236
+ resource (dict): policy resource returned by ``getIamPolicy`` API.
209
237
210
- :rtype: :class:`Policy`
211
- :returns : the parsed policy
238
+ Returns:
239
+ :class:`Policy` : the parsed policy
212
240
"""
213
241
version = resource .get ("version" )
214
242
etag = resource .get ("etag" )
@@ -220,10 +248,10 @@ def from_api_repr(cls, resource):
220
248
return policy
221
249
222
250
def to_api_repr (self ):
223
- """Construct a Policy resource.
251
+ """Render a JSON policy resource.
224
252
225
- :rtype: dict
226
- :returns : a resource to be passed to the ``setIamPolicy`` API.
253
+ Returns:
254
+ dict : a resource to be passed to the ``setIamPolicy`` API.
227
255
"""
228
256
resource = {}
229
257
0 commit comments