def set_iam_policy(self, policy, client=None): """Update the IAM policy for the bucket. See https://cloud.google.com/storage/docs/json_api/v1/buckets/setIamPolicy :type policy: :class:`google.cloud.iam.Policy` :param policy: policy instance used to update bucket's IAM policy. :type client: :class:`~google.cloud.storage.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the current bucket. :rtype: :class:`google.cloud.iam.Policy` :returns: the policy instance, based on the resource returned from the ``setIamPolicy`` API request. """ client = self._require_client(client) query_params = {} if self.user_project is not None: query_params['userProject'] = self.user_project resource = policy.to_api_repr() resource['resourceId'] = self.path info = client._connection.api_request(method='PUT', path='%s/iam' % (self.path, ), query_params=query_params, data=resource, _target_object=None) return Policy.from_api_repr(info)
def get_iam_policy(self, client=None): """Retrieve the IAM policy for the bucket. See https://cloud.google.com/storage/docs/json_api/v1/buckets/getIamPolicy If :attr:`user_project` is set, bills the API request to that project. :type client: :class:`~google.cloud.storage.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the current bucket. :rtype: :class:`google.cloud.iam.Policy` :returns: the policy instance, based on the resource returned from the ``getIamPolicy`` API request. """ client = self._require_client(client) query_params = {} if self.user_project is not None: query_params['userProject'] = self.user_project info = client._connection.api_request(method='GET', path='%s/iam' % (self.path, ), query_params=query_params, _target_object=None) return Policy.from_api_repr(info)
def set_iam_policy(self, policy, client=None): """Update the IAM policy for the bucket. See https://cloud.google.com/storage/docs/json_api/v1/buckets/setIamPolicy If :attr:`user_project` is set, bills the API request to that project. :type policy: :class:`google.cloud.iam.Policy` :param policy: policy instance used to update bucket's IAM policy. :type client: :class:`~google.cloud.storage.client.Client` or ``NoneType`` :param client: Optional. The client to use. If not passed, falls back to the ``client`` stored on the current bucket. :rtype: :class:`google.cloud.iam.Policy` :returns: the policy instance, based on the resource returned from the ``setIamPolicy`` API request. """ client = self._require_client(client) query_params = {} if self.user_project is not None: query_params['userProject'] = self.user_project resource = policy.to_api_repr() resource['resourceId'] = self.path info = client._connection.api_request( method='PUT', path='%s/iam' % (self.path,), query_params=query_params, data=resource, _target_object=None) return Policy.from_api_repr(info)
def test_set_iam_policy(self): import operator from google.cloud.storage.iam import STORAGE_OWNER_ROLE from google.cloud.storage.iam import STORAGE_EDITOR_ROLE from google.cloud.storage.iam import STORAGE_VIEWER_ROLE from google.cloud.iam import Policy NAME = 'name' PATH = '/b/%s' % (NAME,) ETAG = 'DEADBEEF' VERSION = 17 OWNER1 = 'user:[email protected]' OWNER2 = 'group:[email protected]' EDITOR1 = 'domain:google.com' EDITOR2 = 'user:[email protected]' VIEWER1 = 'serviceAccount:[email protected]' VIEWER2 = 'user:[email protected]' BINDINGS = [ {'role': STORAGE_OWNER_ROLE, 'members': [OWNER1, OWNER2]}, {'role': STORAGE_EDITOR_ROLE, 'members': [EDITOR1, EDITOR2]}, {'role': STORAGE_VIEWER_ROLE, 'members': [VIEWER1, VIEWER2]}, ] RETURNED = { 'etag': ETAG, 'version': VERSION, 'bindings': BINDINGS, } policy = Policy() for binding in BINDINGS: policy[binding['role']] = binding['members'] connection = _Connection(RETURNED) client = _Client(connection, None) bucket = self._make_one(client=client, name=NAME) returned = bucket.set_iam_policy(policy) self.assertEqual(returned.etag, ETAG) self.assertEqual(returned.version, VERSION) self.assertEqual(dict(returned), dict(policy)) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PUT') self.assertEqual(kw[0]['path'], '%s/iam' % (PATH,)) sent = kw[0]['data'] self.assertEqual(sent['resourceId'], PATH) self.assertEqual(len(sent['bindings']), len(BINDINGS)) key = operator.itemgetter('role') for found, expected in zip( sorted(sent['bindings'], key=key), sorted(BINDINGS, key=key)): self.assertEqual(found['role'], expected['role']) self.assertEqual( sorted(found['members']), sorted(expected['members']))
def __init__(self, etag=None, version=None): BasePolicy.__init__(self, etag=etag if etag is None else _to_bytes(etag), version=version)