-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'Subscription.test_iam_permissions' API wrapper. #1646
Merged
tseaver
merged 3 commits into
googleapis:master
from
tseaver:pubsub-subscription-test_iam_permissions
Mar 23, 2016
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ IAM Policy | |
|
||
.. automodule:: gcloud.pubsub.iam | ||
:members: | ||
:member-order: bysource | ||
:undoc-members: | ||
:show-inheritance: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,11 +99,13 @@ Test permissions allowed by the current IAM policy on a topic: | |
.. doctest:: | ||
|
||
>>> from gcloud import pubsub | ||
>>> from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
>>> client = pubsub.Client() | ||
>>> topic = client.topic('topic_name') | ||
>>> topic.test_iam_permissions( | ||
... ['roles/reader', 'roles/writer', 'roles/owner']) # API request | ||
['roles/reader', 'roles/writer'] | ||
>>> allowed = topic.test_iam_permissions( | ||
... [READER_ROLE, WRITER_ROLE, OWNER_ROLE]) # API request | ||
>>> allowed == [READER_ROLE, WRITER_ROLE] | ||
True | ||
|
||
|
||
Publish messages to a topic | ||
|
@@ -341,3 +343,17 @@ Update the IAM policy for a subscription: | |
>>> policy = subscription.get_iam_policy() # API request | ||
>>> policy.writers.add(policy.group('[email protected]')) | ||
>>> subscription.set_iam_policy(policy) # API request | ||
|
||
Test permissions allowed by the current IAM policy on a subscription: | ||
|
||
.. doctest:: | ||
|
||
>>> from gcloud import pubsub | ||
>>> from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
>>> client = pubsub.Client() | ||
>>> topic = client.topic('topic_name') | ||
>>> subscription = topic.subscription('subscription_name') | ||
>>> allowed = subscription.test_iam_permissions( | ||
... [READER_ROLE, WRITER_ROLE, OWNER_ROLE]) # API request | ||
>>> allowed == [READER_ROLE, WRITER_ROLE] | ||
True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ def test_from_api_repr_only_etag(self): | |
self.assertEqual(list(policy.readers), []) | ||
|
||
def test_from_api_repr_complete(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'user:[email protected]' | ||
OWNER2 = 'group:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -98,9 +98,9 @@ def test_from_api_repr_complete(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
klass = self._getTargetClass() | ||
|
@@ -134,7 +134,7 @@ def test_to_api_repr_only_etag(self): | |
self.assertEqual(policy.to_api_repr(), {'etag': 'DEADBEEF'}) | ||
|
||
def test_to_api_repr_full(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'group:[email protected]' | ||
OWNER2 = 'user:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -145,9 +145,9 @@ def test_to_api_repr_full(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
policy = self._makeOne('DEADBEEF', 17) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,7 +485,7 @@ def test_delete_w_alternate_client(self): | |
self.assertEqual(req['path'], '/%s' % SUB_PATH) | ||
|
||
def test_get_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'user:[email protected]' | ||
OWNER2 = 'group:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -496,9 +496,9 @@ def test_get_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
PROJECT = 'PROJECT' | ||
|
@@ -557,7 +557,7 @@ def test_get_iam_policy_w_alternate_client(self): | |
self.assertEqual(req['path'], '/%s' % PATH) | ||
|
||
def test_set_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
from gcloud.pubsub.iam import Policy | ||
OWNER1 = 'group:[email protected]' | ||
OWNER2 = 'user:[email protected]' | ||
|
@@ -569,9 +569,9 @@ def test_set_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
RESPONSE = POLICY.copy() | ||
|
@@ -641,6 +641,61 @@ def test_set_iam_policy_w_alternate_client(self): | |
self.assertEqual(req['path'], '/%s' % PATH) | ||
self.assertEqual(req['data'], {}) | ||
|
||
def test_test_iam_permissions_w_bound_client(self): | ||
PROJECT = 'PROJECT' | ||
TOPIC_NAME = 'topic_name' | ||
SUB_NAME = 'sub_name' | ||
PATH = 'projects/%s/subscriptions/%s:testIamPermissions' % ( | ||
PROJECT, SUB_NAME) | ||
ROLES = ['roles/reader', 'roles/writer', 'roles/owner'] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
RESPONSE = { | ||
'permissions': ROLES[:-1], | ||
} | ||
conn = _Connection(RESPONSE) | ||
CLIENT = _Client(project=PROJECT, connection=conn) | ||
topic = _Topic(TOPIC_NAME, client=CLIENT) | ||
subscription = self._makeOne(SUB_NAME, topic) | ||
|
||
allowed = subscription.test_iam_permissions(ROLES) | ||
|
||
self.assertEqual(allowed, ROLES[:-1]) | ||
self.assertEqual(len(conn._requested), 1) | ||
req = conn._requested[0] | ||
self.assertEqual(req['method'], 'POST') | ||
self.assertEqual(req['path'], '/%s' % PATH) | ||
self.assertEqual(req['data'], REQUESTED) | ||
|
||
def test_test_iam_permissions_w_alternate_client(self): | ||
PROJECT = 'PROJECT' | ||
TOPIC_NAME = 'topic_name' | ||
SUB_NAME = 'sub_name' | ||
PATH = 'projects/%s/subscriptions/%s:testIamPermissions' % ( | ||
PROJECT, SUB_NAME) | ||
ROLES = ['roles/reader', 'roles/writer', 'roles/owner'] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
RESPONSE = {} | ||
conn1 = _Connection() | ||
CLIENT1 = _Client(project=PROJECT, connection=conn1) | ||
conn2 = _Connection(RESPONSE) | ||
CLIENT2 = _Client(project=PROJECT, connection=conn2) | ||
topic = _Topic(TOPIC_NAME, client=CLIENT1) | ||
subscription = self._makeOne(SUB_NAME, topic) | ||
|
||
allowed = subscription.test_iam_permissions(ROLES, client=CLIENT2) | ||
|
||
self.assertEqual(len(allowed), 0) | ||
self.assertEqual(len(conn1._requested), 0) | ||
self.assertEqual(len(conn2._requested), 1) | ||
req = conn2._requested[0] | ||
self.assertEqual(req['method'], 'POST') | ||
self.assertEqual(req['path'], '/%s' % PATH) | ||
self.assertEqual(req['data'], REQUESTED) | ||
|
||
|
||
class _Connection(object): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -453,7 +453,7 @@ def test_list_subscriptions_missing_key(self): | |
self.assertEqual(req['query_params'], {}) | ||
|
||
def test_get_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'user:[email protected]' | ||
OWNER2 = 'group:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -464,9 +464,9 @@ def test_get_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
TOPIC_NAME = 'topic_name' | ||
|
@@ -522,7 +522,7 @@ def test_get_iam_policy_w_alternate_client(self): | |
|
||
def test_set_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import Policy | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'group:[email protected]' | ||
OWNER2 = 'user:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -533,9 +533,9 @@ def test_set_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
RESPONSE = POLICY.copy() | ||
|
@@ -602,12 +602,12 @@ def test_set_iam_policy_w_alternate_client(self): | |
self.assertEqual(req['data'], {}) | ||
|
||
def test_test_iam_permissions_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
TOPIC_NAME = 'topic_name' | ||
PROJECT = 'PROJECT' | ||
PATH = 'projects/%s/topics/%s:testIamPermissions' % ( | ||
PROJECT, TOPIC_NAME) | ||
ROLES = [_READER_ROLE, _WRITER_ROLE, _OWNER_ROLE] | ||
ROLES = [READER_ROLE, WRITER_ROLE, OWNER_ROLE] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
|
@@ -628,12 +628,12 @@ def test_test_iam_permissions_w_bound_client(self): | |
self.assertEqual(req['data'], REQUESTED) | ||
|
||
def test_test_iam_permissions_w_alternate_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
TOPIC_NAME = 'topic_name' | ||
PROJECT = 'PROJECT' | ||
PATH = 'projects/%s/topics/%s:testIamPermissions' % ( | ||
PROJECT, TOPIC_NAME) | ||
ROLES = [_READER_ROLE, _WRITER_ROLE, _OWNER_ROLE] | ||
ROLES = [READER_ROLE, WRITER_ROLE, OWNER_ROLE] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.