Skip to content

Commit e7654b6

Browse files
committed
Deprecate assignment to legacy role attributes.
1 parent 77ed12b commit e7654b6

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

core/google/cloud/iam.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import collections
21+
import warnings
2122

2223
# Generic IAM roles
2324

@@ -30,6 +31,9 @@
3031
VIEWER_ROLE = 'roles/viewer'
3132
"""Generic role implying rights to access an object."""
3233

34+
_ASSIGNMENT_DEPRECATED_MSG = """\
35+
Assigning to '{}' is deprecated. Replace with 'policy[{}] = members."""
36+
3337

3438
class Policy(collections.MutableMapping):
3539
"""IAM Policy
@@ -84,6 +88,9 @@ def owners(self):
8488
@owners.setter
8589
def owners(self, value):
8690
"""Update owners."""
91+
warnings.warn(
92+
_ASSIGNMENT_DEPRECATED_MSG.format('owners', OWNER_ROLE),
93+
DeprecationWarning)
8794
self._bindings[OWNER_ROLE] = list(value)
8895

8996
@property
@@ -98,6 +105,9 @@ def editors(self):
98105
@editors.setter
99106
def editors(self, value):
100107
"""Update editors."""
108+
warnings.warn(
109+
_ASSIGNMENT_DEPRECATED_MSG.format('editors', EDITOR_ROLE),
110+
DeprecationWarning)
101111
self._bindings[EDITOR_ROLE] = list(value)
102112

103113
@property
@@ -112,6 +122,9 @@ def viewers(self):
112122
@viewers.setter
113123
def viewers(self, value):
114124
"""Update viewers."""
125+
warnings.warn(
126+
_ASSIGNMENT_DEPRECATED_MSG.format('viewers', VIEWER_ROLE),
127+
DeprecationWarning)
115128
self._bindings[VIEWER_ROLE] = list(value)
116129

117130
@staticmethod

core/tests/unit/test_iam.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,58 @@ def test___delitem___miss(self):
7676
with self.assertRaises(KeyError):
7777
del policy['nonesuch']
7878

79+
def test_owners_getter(self):
80+
from google.cloud.iam import OWNER_ROLE
81+
MEMBER = 'user:phred@example.com'
82+
policy = self._make_one()
83+
policy[OWNER_ROLE] = [MEMBER]
84+
self.assertIsInstance(policy.owners, frozenset)
85+
self.assertEqual(list(policy.owners), [MEMBER])
86+
87+
def test_owners_setter(self):
88+
import warnings
89+
from google.cloud.iam import OWNER_ROLE
90+
MEMBER = 'user:phred@example.com'
91+
policy = self._make_one()
92+
with warnings.catch_warnings():
93+
policy.owners = [MEMBER]
94+
self.assertEqual(list(policy[OWNER_ROLE]), [MEMBER])
95+
96+
def test_editors_getter(self):
97+
from google.cloud.iam import EDITOR_ROLE
98+
MEMBER = 'user:phred@example.com'
99+
policy = self._make_one()
100+
policy[EDITOR_ROLE] = [MEMBER]
101+
self.assertIsInstance(policy.editors, frozenset)
102+
self.assertEqual(list(policy.editors), [MEMBER])
103+
104+
def test_editors_setter(self):
105+
import warnings
106+
from google.cloud.iam import EDITOR_ROLE
107+
MEMBER = 'user:phred@example.com'
108+
policy = self._make_one()
109+
with warnings.catch_warnings():
110+
policy.editors = [MEMBER]
111+
self.assertEqual(list(policy[EDITOR_ROLE]), [MEMBER])
112+
113+
def test_viewers_getter(self):
114+
from google.cloud.iam import VIEWER_ROLE
115+
MEMBER = 'user:phred@example.com'
116+
policy = self._make_one()
117+
policy[VIEWER_ROLE] = [MEMBER]
118+
self.assertIsInstance(policy.viewers, frozenset)
119+
self.assertEqual(list(policy.viewers), [MEMBER])
120+
121+
def test_viewers_setter(self):
122+
import warnings
123+
from google.cloud.iam import VIEWER_ROLE
124+
MEMBER = 'user:phred@example.com'
125+
policy = self._make_one()
126+
with warnings.catch_warnings():
127+
warnings.simplefilter('always')
128+
policy.viewers = [MEMBER]
129+
self.assertEqual(list(policy[VIEWER_ROLE]), [MEMBER])
130+
79131
def test_user(self):
80132
EMAIL = 'phred@example.com'
81133
MEMBER = 'user:%s' % (EMAIL,)

pubsub/google/cloud/pubsub/iam.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
https://cloud.google.com/pubsub/access_control#permissions
1818
"""
1919

20+
import warnings
21+
2022
# pylint: disable=unused-import
2123
from google.cloud.iam import OWNER_ROLE # noqa - backward compat
2224
from google.cloud.iam import EDITOR_ROLE # noqa - backward compat
2325
from google.cloud.iam import VIEWER_ROLE # noqa - backward compat
2426
# pylint: enable=unused-import
2527
from google.cloud.iam import Policy as _BasePolicy
28+
from google.cloud.iam import _ASSIGNMENT_DEPRECATED_MSG
2629

2730
# Pubsub-specific IAM roles
2831

@@ -114,6 +117,10 @@ def publishers(self):
114117
@publishers.setter
115118
def publishers(self, value):
116119
"""Update publishers."""
120+
warnings.warn(
121+
_ASSIGNMENT_DEPRECATED_MSG.format(
122+
'publishers', PUBSUB_PUBLISHER_ROLE),
123+
DeprecationWarning)
117124
self._bindings[PUBSUB_PUBLISHER_ROLE] = list(value)
118125

119126
@property
@@ -124,4 +131,8 @@ def subscribers(self):
124131
@subscribers.setter
125132
def subscribers(self, value):
126133
"""Update subscribers."""
134+
warnings.warn(
135+
_ASSIGNMENT_DEPRECATED_MSG.format(
136+
'subscribers', PUBSUB_SUBSCRIBER_ROLE),
137+
DeprecationWarning)
127138
self._bindings[PUBSUB_SUBSCRIBER_ROLE] = list(value)

pubsub/tests/unit/test_iam.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,28 @@ def test_ctor_explicit(self):
5454
self.assertEqual(list(policy.subscribers), [])
5555

5656
def test_publishers_setter(self):
57+
import warnings
5758
from google.cloud.pubsub.iam import (
5859
PUBSUB_PUBLISHER_ROLE,
5960
)
6061
PUBLISHER = 'user:phred@example.com'
6162
policy = self._make_one()
62-
policy.publishers = [PUBLISHER]
63+
with warnings.catch_warnings():
64+
policy.publishers = [PUBLISHER]
6365

6466
self.assertEqual(sorted(policy.publishers), [PUBLISHER])
6567
self.assertEqual(
6668
dict(policy), {PUBSUB_PUBLISHER_ROLE: [PUBLISHER]})
6769

6870
def test_subscribers_setter(self):
71+
import warnings
6972
from google.cloud.pubsub.iam import (
7073
PUBSUB_SUBSCRIBER_ROLE,
7174
)
7275
SUBSCRIBER = 'serviceAccount:1234-abcdef@service.example.com'
7376
policy = self._make_one()
74-
policy.subscribers = [SUBSCRIBER]
77+
with warnings.catch_warnings():
78+
policy.subscribers = [SUBSCRIBER]
7579

7680
self.assertEqual(sorted(policy.subscribers), [SUBSCRIBER])
7781
self.assertEqual(

0 commit comments

Comments
 (0)