| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -311,3 +311,21 @@ Fetch messages for a pull subscription without blocking (none pending): | |||
| 311 | 311 | >>> messages = [recv[1] for recv in received] | |
| 312 | 312 | >>> [message.message_id for message in messages] | |
| 313 | 313 | [] | |
| 314 | + | ||
| 315 | + Fetch the IAM policy for a subscription | ||
| 316 | + | ||
| 317 | + .. doctest:: | ||
| 318 | + | ||
| 319 | + >>> from gcloud import pubsub | ||
| 320 | + >>> client = pubsub.Client() | ||
| 321 | + >>> topic = client.topic('topic_name') | ||
| 322 | + >>> subscription = topic.subscription('subscription_name') | ||
| 323 | + >>> policy = subscription.get_iam_policy() # API request | ||
| 324 | + >>> policy.etag | ||
| 325 | + 'DEADBEEF' | ||
| 326 | + >>> policy.owners | ||
| 327 | + ['user:phred@example.com'] | ||
| 328 | + >>> policy.writers | ||
| 329 | + ['systemAccount:abc-1234@systemaccounts.example.com'] | ||
| 330 | + >>> policy.readers | ||
| 331 | + ['domain:example.com'] | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | 17 | from gcloud.exceptions import NotFound | |
| 18 | 18 | from gcloud.pubsub._helpers import topic_name_from_path | |
| 19 | + from gcloud.pubsub.iam import Policy | ||
| 19 | 20 | from gcloud.pubsub.message import Message | |
| 20 | 21 | ||
| 21 | 22 | ||
@@ -263,3 +264,22 @@ def delete(self, client=None): | |||
| 263 | 264 | """ | |
| 264 | 265 | client = self._require_client(client) | |
| 265 | 266 | client.connection.api_request(method='DELETE', path=self.path) | |
| 267 | + | ||
| 268 | + def get_iam_policy(self, client=None): | ||
| 269 | + """Fetch the IAM policy for the subscription. | ||
| 270 | + | ||
| 271 | + See: | ||
| 272 | + https://cloud.google.com/pubsub/reference/rest/v1/projects.subscriptions/getIamPolicy | ||
| 273 | + | ||
| 274 | + :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType`` | ||
| 275 | + :param client: the client to use. If not passed, falls back to the | ||
| 276 | + ``client`` stored on the current subscription's topic. | ||
| 277 | + | ||
| 278 | + :rtype: :class:`gcloud.pubsub.iam.Policy` | ||
| 279 | + :returns: policy created from the resource returned by the | ||
| 280 | + ``getIamPolicy`` API request. | ||
| 281 | + """ | ||
| 282 | + client = self._require_client(client) | ||
| 283 | + path = '%s:getIamPolicy' % (self.path,) | ||
| 284 | + resp = client.connection.api_request(method='GET', path=path) | ||
| 285 | + return Policy.from_api_repr(resp) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -484,6 +484,77 @@ def test_delete_w_alternate_client(self): | |||
| 484 | 484 | self.assertEqual(req['method'], 'DELETE') | |
| 485 | 485 | self.assertEqual(req['path'], '/%s' % SUB_PATH) | |
| 486 | 486 | ||
| 487 | + def test_get_iam_policy_w_bound_client(self): | ||
| 488 | + OWNER1 = 'user:phred@example.com' | ||
| 489 | + OWNER2 = 'group:cloud-logs@google.com' | ||
| 490 | + WRITER1 = 'domain:google.com' | ||
| 491 | + WRITER2 = 'user:phred@example.com' | ||
| 492 | + READER1 = 'serviceAccount:1234-abcdef@service.example.com' | ||
| 493 | + READER2 = 'user:phred@example.com' | ||
| 494 | + POLICY = { | ||
| 495 | + 'etag': 'DEADBEEF', | ||
| 496 | + 'version': 17, | ||
| 497 | + 'bindings': [ | ||
| 498 | + {'role': 'roles/owner', 'members': [OWNER1, OWNER2]}, | ||
| 499 | + {'role': 'roles/writer', 'members': [WRITER1, WRITER2]}, | ||
| 500 | + {'role': 'roles/reader', 'members': [READER1, READER2]}, | ||
| 501 | + ], | ||
| 502 | + } | ||
| 503 | + PROJECT = 'PROJECT' | ||
| 504 | + TOPIC_NAME = 'topic_name' | ||
| 505 | + SUB_NAME = 'sub_name' | ||
| 506 | + PATH = 'projects/%s/subscriptions/%s:getIamPolicy' % ( | ||
| 507 | + PROJECT, SUB_NAME) | ||
| 508 | + | ||
| 509 | + conn = _Connection(POLICY) | ||
| 510 | + CLIENT = _Client(project=PROJECT, connection=conn) | ||
| 511 | + topic = _Topic(TOPIC_NAME, client=CLIENT) | ||
| 512 | + subscription = self._makeOne(SUB_NAME, topic) | ||
| 513 | + | ||
| 514 | + policy = subscription.get_iam_policy() | ||
| 515 | + | ||
| 516 | + self.assertEqual(policy.etag, 'DEADBEEF') | ||
| 517 | + self.assertEqual(policy.version, 17) | ||
| 518 | + self.assertEqual(sorted(policy.owners), [OWNER2, OWNER1]) | ||
| 519 | + self.assertEqual(sorted(policy.writers), [WRITER1, WRITER2]) | ||
| 520 | + self.assertEqual(sorted(policy.readers), [READER1, READER2]) | ||
| 521 | + | ||
| 522 | + self.assertEqual(len(conn._requested), 1) | ||
| 523 | + req = conn._requested[0] | ||
| 524 | + self.assertEqual(req['method'], 'GET') | ||
| 525 | + self.assertEqual(req['path'], '/%s' % PATH) | ||
| 526 | + | ||
| 527 | + def test_get_iam_policy_w_alternate_client(self): | ||
| 528 | + POLICY = { | ||
| 529 | + 'etag': 'ACAB', | ||
| 530 | + } | ||
| 531 | + PROJECT = 'PROJECT' | ||
| 532 | + TOPIC_NAME = 'topic_name' | ||
| 533 | + SUB_NAME = 'sub_name' | ||
| 534 | + PATH = 'projects/%s/subscriptions/%s:getIamPolicy' % ( | ||
| 535 | + PROJECT, SUB_NAME) | ||
| 536 | + | ||
| 537 | + conn1 = _Connection() | ||
| 538 | + conn2 = _Connection(POLICY) | ||
| 539 | + CLIENT1 = _Client(project=PROJECT, connection=conn1) | ||
| 540 | + CLIENT2 = _Client(project=PROJECT, connection=conn2) | ||
| 541 | + topic = _Topic(TOPIC_NAME, client=CLIENT1) | ||
| 542 | + subscription = self._makeOne(SUB_NAME, topic) | ||
| 543 | + | ||
| 544 | + policy = subscription.get_iam_policy(client=CLIENT2) | ||
| 545 | + | ||
| 546 | + self.assertEqual(policy.etag, 'ACAB') | ||
| 547 | + self.assertEqual(policy.version, None) | ||
| 548 | + self.assertEqual(sorted(policy.owners), []) | ||
| 549 | + self.assertEqual(sorted(policy.writers), []) | ||
| 550 | + self.assertEqual(sorted(policy.readers), []) | ||
| 551 | + | ||
| 552 | + self.assertEqual(len(conn1._requested), 0) | ||
| 553 | + self.assertEqual(len(conn2._requested), 1) | ||
| 554 | + req = conn2._requested[0] | ||
| 555 | + self.assertEqual(req['method'], 'GET') | ||
| 556 | + self.assertEqual(req['path'], '/%s' % PATH) | ||
| 557 | + | ||
| 487 | 558 | ||
| 488 | 559 | class _Connection(object): | |
| 489 | 560 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments