Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3bb45e

Browse files
authored
Add usage example for 'google.api_core.iam.Polcy'. (#6855)
Napoleon-ize docstrings in 'google.api_core.iam'. Order the 'core/iam' docs page entries in source order. Closes #6161.
1 parent 92b7f71 commit f3bb45e

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

api_core/google/api_core/iam.py

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
1616
For allowed roles / permissions, see:
1717
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)
1834
"""
1935

2036
import collections
@@ -45,11 +61,9 @@ class Policy(collections_abc.MutableMapping):
4561
See
4662
https://cloud.google.com/iam/reference/rest/v1/Policy
4763
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
5367
"""
5468

5569
_OWNER_ROLES = (OWNER_ROLE,)
@@ -83,7 +97,9 @@ def __delitem__(self, key):
8397

8498
@property
8599
def owners(self):
86-
"""Legacy access to owner role."""
100+
"""Legacy access to owner role.
101+
102+
DEPRECATED: use ``policy["roles/owners"]`` instead."""
87103
result = set()
88104
for role in self._OWNER_ROLES:
89105
for member in self._bindings.get(role, ()):
@@ -92,15 +108,19 @@ def owners(self):
92108

93109
@owners.setter
94110
def owners(self, value):
95-
"""Update owners."""
111+
"""Update owners.
112+
113+
DEPRECATED: use ``policy["roles/owners"] = value`` instead."""
96114
warnings.warn(
97115
_ASSIGNMENT_DEPRECATED_MSG.format("owners", OWNER_ROLE), DeprecationWarning
98116
)
99117
self[OWNER_ROLE] = value
100118

101119
@property
102120
def editors(self):
103-
"""Legacy access to editor role."""
121+
"""Legacy access to editor role.
122+
123+
DEPRECATED: use ``policy["roles/editors"]`` instead."""
104124
result = set()
105125
for role in self._EDITOR_ROLES:
106126
for member in self._bindings.get(role, ()):
@@ -109,7 +129,9 @@ def editors(self):
109129

110130
@editors.setter
111131
def editors(self, value):
112-
"""Update editors."""
132+
"""Update editors.
133+
134+
DEPRECATED: use ``policy["roles/editors"] = value`` instead."""
113135
warnings.warn(
114136
_ASSIGNMENT_DEPRECATED_MSG.format("editors", EDITOR_ROLE),
115137
DeprecationWarning,
@@ -118,7 +140,10 @@ def editors(self, value):
118140

119141
@property
120142
def viewers(self):
121-
"""Legacy access to viewer role."""
143+
"""Legacy access to viewer role.
144+
145+
DEPRECATED: use ``policy["roles/viewers"]`` instead
146+
"""
122147
result = set()
123148
for role in self._VIEWER_ROLES:
124149
for member in self._bindings.get(role, ()):
@@ -127,7 +152,10 @@ def viewers(self):
127152

128153
@viewers.setter
129154
def viewers(self, value):
130-
"""Update viewers."""
155+
"""Update viewers.
156+
157+
DEPRECATED: use ``policy["roles/viewers"] = value`` instead.
158+
"""
131159
warnings.warn(
132160
_ASSIGNMENT_DEPRECATED_MSG.format("viewers", VIEWER_ROLE),
133161
DeprecationWarning,
@@ -138,77 +166,77 @@ def viewers(self, value):
138166
def user(email):
139167
"""Factory method for a user member.
140168
141-
:type email: str
142-
:param email: E-mail for this particular user.
169+
Args:
170+
email (str): E-mail for this particular user.
143171
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.
146174
"""
147175
return "user:%s" % (email,)
148176

149177
@staticmethod
150178
def service_account(email):
151179
"""Factory method for a service account member.
152180
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.
155183
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.
158186
"""
159187
return "serviceAccount:%s" % (email,)
160188

161189
@staticmethod
162190
def group(email):
163191
"""Factory method for a group member.
164192
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.
167195
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.
170198
"""
171199
return "group:%s" % (email,)
172200

173201
@staticmethod
174202
def domain(domain):
175203
"""Factory method for a domain member.
176204
177-
:type domain: str
178-
:param domain: The domain for this member.
205+
Args:
206+
domain (str): The domain for this member.
179207
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.
182210
"""
183211
return "domain:%s" % (domain,)
184212

185213
@staticmethod
186214
def all_users():
187215
"""Factory method for a member representing all users.
188216
189-
:rtype: str
190-
:returns: A member string representing all users.
217+
Returns:
218+
str: A member string representing all users.
191219
"""
192220
return "allUsers"
193221

194222
@staticmethod
195223
def authenticated_users():
196224
"""Factory method for a member representing all authenticated users.
197225
198-
:rtype: str
199-
:returns: A member string representing all authenticated users.
226+
Returns:
227+
str: A member string representing all authenticated users.
200228
"""
201229
return "allAuthenticatedUsers"
202230

203231
@classmethod
204232
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.
206234
207-
:type resource: dict
208-
:param resource: resource returned from the ``getIamPolicy`` API.
235+
Args:
236+
resource (dict): policy resource returned by ``getIamPolicy`` API.
209237
210-
:rtype: :class:`Policy`
211-
:returns: the parsed policy
238+
Returns:
239+
:class:`Policy`: the parsed policy
212240
"""
213241
version = resource.get("version")
214242
etag = resource.get("etag")
@@ -220,10 +248,10 @@ def from_api_repr(cls, resource):
220248
return policy
221249

222250
def to_api_repr(self):
223-
"""Construct a Policy resource.
251+
"""Render a JSON policy resource.
224252
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.
227255
"""
228256
resource = {}
229257

docs/core/iam.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Identity and Access Management
44
.. automodule:: google.api_core.iam
55
:members:
66
:show-inheritance:
7+
:member-order: bysource

0 commit comments

Comments
 (0)