def get_iam_policy(self, resource, options=None): """ Gets the access control policy for an instance resource. Returns an empty policy if an instance exists but does not have a policy set. Authorization requires ``spanner.instances.getIamPolicy`` on ``resource``. Example: >>> from google.cloud.gapic.spanner_admin_instance.v1 import instance_admin_client >>> api = instance_admin_client.InstanceAdminClient() >>> resource = api.instance_path('[PROJECT]', '[INSTANCE]') >>> response = api.get_iam_policy(resource) Args: resource (string): REQUIRED: The resource for which the policy is being requested. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. options (:class:`google.gax.CallOptions`): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`google.iam.v1.policy_pb2.Policy` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ # Create the request object. request = iam_policy_pb2.GetIamPolicyRequest(resource=resource) return self._get_iam_policy(request, options)
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"etag3123477" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = bigtable_admin_v2.BigtableInstanceAdminClient() # Setup Request resource = client.instance_path("[PROJECT]", "[INSTANCE]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_admin_database_v1.DatabaseAdminClient() # Setup Request resource = "resource-341064690" response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = publisher_client.PublisherClient() # Setup Request resource = client.topic_path("[PROJECT]", "[TOPIC]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def get_iam_policy(self, resource, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Gets the access control policy for a database resource. Returns an empty policy if a database exists but does not have a policy set. Authorization requires ``spanner.databases.getIamPolicy`` permission on ``resource``. Example: >>> from google.cloud import spanner_admin_database_v1 >>> >>> client = spanner_admin_database_v1.DatabaseAdminClient() >>> >>> resource = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') >>> >>> response = client.get_iam_policy(resource) Args: resource (str): REQUIRED: The resource for which the policy is being requested. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.spanner_admin_database_v1.types.Policy` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if 'get_iam_policy' not in self._inner_api_calls: self._inner_api_calls[ 'get_iam_policy'] = google.api_core.gapic_v1.method.wrap_method( self.transport.get_iam_policy, default_retry=self._method_configs['GetIamPolicy'].retry, default_timeout=self._method_configs['GetIamPolicy']. timeout, client_info=self._client_info, ) request = iam_policy_pb2.GetIamPolicyRequest(resource=resource, ) return self._inner_api_calls['get_iam_policy'](request, retry=retry, timeout=timeout, metadata=metadata)
def get_iam_policy(self, resource, options=None): """ Gets the access control policy for a resource. Is empty if the policy or the resource does not exist. Example: >>> from google.cloud.gapic.pubsub.v1 import subscriber_api >>> api = subscriber_api.SubscriberApi() >>> resource = api.subscription_path('[PROJECT]', '[SUBSCRIPTION]') >>> response = api.get_iam_policy(resource) Args: resource (string): REQUIRED: The resource for which policy is being requested. Resource is usually specified as a path, such as, projects/{project}. options (:class:`google.gax.CallOptions`): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`google.iam.v1.policy_pb2.Policy` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ request = iam_policy_pb2.GetIamPolicyRequest(resource=resource) return self._get_iam_policy(request, options)
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = iot_v1.DeviceManagerClient() # Setup Request resource = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def get_iam_policy(self, resource, options=None): """ Gets the access control policy for a database resource. Returns an empty policy if a database exists but does not have a policy set. Authorization requires ``spanner.databases.getIamPolicy`` permission on ``resource``. Example: >>> from google.cloud import spanner_admin_database_v1 >>> >>> client = spanner_admin_database_v1.DatabaseAdminClient() >>> >>> resource = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') >>> >>> response = client.get_iam_policy(resource) Args: resource (str): REQUIRED: The resource for which the policy is being requested. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. options (~google.gax.CallOptions): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`~google.cloud.spanner_admin_database_v1.types.Policy` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ request = iam_policy_pb2.GetIamPolicyRequest(resource=resource) return self._get_iam_policy(request, options)
def get_iam_policy(self, resource, options=None): """ Gets the access control policy for a resource. Returns an empty policy if the resource exists and does not have a policy set. Example: >>> from google.cloud.gapic.pubsub.v1 import subscriber_client >>> client = subscriber_client.SubscriberClient() >>> resource = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]') >>> response = client.get_iam_policy(resource) Args: resource (string): REQUIRED: The resource for which the policy is being requested. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. options (:class:`google.gax.CallOptions`): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`google.iam.v1.policy_pb2.Policy` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ # Create the request object. request = iam_policy_pb2.GetIamPolicyRequest(resource=resource) return self._get_iam_policy(request, options)
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request resource = client.source_path("[ORGANIZATION]", "[SOURCE]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = spanner_admin_instance_v1.InstanceAdminClient() # Setup Request resource = client.instance_path('[PROJECT]', '[INSTANCE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = kms_v1.KeyManagementServiceClient() # Setup Request resource = client.key_ring_path("[PROJECT]", "[LOCATION]", "[KEY_RING]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = database_admin_client.DatabaseAdminClient() # Mock request resource = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') # Mock response version = 351608024 etag = b'21' expected_response = policy_pb2.Policy(version=version, etag=etag) grpc_stub.GetIamPolicy.return_value = expected_response response = client.get_iam_policy(resource) self.assertEqual(expected_response, response) grpc_stub.GetIamPolicy.assert_called_once() args, kwargs = grpc_stub.GetIamPolicy.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) self.assertEqual(expected_request, actual_request)
def get_iam_policy(self, resource, options=None): """ Returns the IAM access control policy for a ``ServiceAccount``. Example: >>> from google.cloud.gapic.iam_admin.v1 import iam_api >>> api = iam_api.IAMApi() >>> resource = api.service_account_path('[PROJECT]', '[SERVICE_ACCOUNT]') >>> response = api.get_iam_policy(resource) Args: resource (string): REQUIRED: The resource for which the policy is being requested. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. options (:class:`google.gax.CallOptions`): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`google.iam.v1.policy_pb2.Policy` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ request = iam_policy_pb2.GetIamPolicyRequest(resource=resource) return self._get_iam_policy(request, options)
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = datacatalog_v1beta1.DataCatalogClient() # Setup Request resource = client.tag_template_path("[PROJECT]", "[LOCATION]", "[TAG_TEMPLATE]") response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(transport: str = "grpc"): client = CloudBillingClient( credentials=credentials.AnonymousCredentials(), transport=transport ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = iam_policy.GetIamPolicyRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.get_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = policy.Policy(version=774, etag=b"etag_blob") response = client.get_iam_policy(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the response is the type that we expect. assert isinstance(response, policy.Policy) assert response.version == 774 assert response.etag == b"etag_blob"
def test_get_iam_policy_flattened_error(): client = CloudBillingClient(credentials=credentials.AnonymousCredentials()) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): client.get_iam_policy(iam_policy.GetIamPolicyRequest(), resource="resource_value")
async def sample_get_iam_policy(): # Create a client client = secretmanager_v1.SecretManagerServiceAsyncClient() # Initialize request argument(s) request = iam_policy_pb2.GetIamPolicyRequest(resource="resource_value", ) # Make the request response = await client.get_iam_policy(request=request) # Handle the response print(response)
def sample_get_iam_policy(): # Create a client client = bigquery_connection_v1.ConnectionServiceClient() # Initialize request argument(s) request = iam_policy_pb2.GetIamPolicyRequest(resource="resource_value", ) # Make the request response = client.get_iam_policy(request=request) # Handle the response print(response)
async def sample_get_iam_policy(): # Create a client client = spanner_admin_instance_v1.InstanceAdminAsyncClient() # Initialize request argument(s) request = iam_policy_pb2.GetIamPolicyRequest(resource="resource_value", ) # Make the request response = await client.get_iam_policy(request=request) # Handle the response print(response)
def sample_get_iam_policy(): # Create a client client = spanner_admin_database_v1.DatabaseAdminClient() # Initialize request argument(s) request = iam_policy_pb2.GetIamPolicyRequest(resource="resource_value", ) # Make the request response = client.get_iam_policy(request=request) # Handle the response print(response)
def get_iam_policy(self, resource, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Gets the access control policy for an instance resource. Returns an empty policy if an instance exists but does not have a policy set. Authorization requires ``spanner.instances.getIamPolicy`` on ``resource``. Example: >>> from google.cloud import spanner_admin_instance_v1 >>> >>> client = spanner_admin_instance_v1.InstanceAdminClient() >>> >>> resource = client.instance_path('[PROJECT]', '[INSTANCE]') >>> >>> response = client.get_iam_policy(resource) Args: resource (str): REQUIRED: The resource for which the policy is being requested. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. Returns: A :class:`~google.cloud.spanner_admin_instance_v1.types.Policy` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ request = iam_policy_pb2.GetIamPolicyRequest(resource=resource, ) return self._get_iam_policy(request, retry=retry, timeout=timeout, metadata=metadata)
def test_get_iam_policy(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.iam.v1 import iam_policy_pb2 from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import Policy from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) version = 1 etag = b'etag_v1' bindings = [{'role': BIGTABLE_ADMIN_ROLE, 'members': ['serviceAccount:[email protected]', 'user:[email protected]']}] expected_request_policy = policy_pb2.Policy(version=version, etag=etag, bindings=bindings) expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=instance.name ) # Patch the stub used by the API method. channel = ChannelStub(responses=[expected_request_policy]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. policy_request = Policy(etag=etag, version=version) policy_request[BIGTABLE_ADMIN_ROLE] = [Policy.user("*****@*****.**"), Policy.service_account( "*****@*****.**")] result = instance.get_iam_policy() actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertEqual(result.bigtable_admins, policy_request.bigtable_admins)
def test_get_iam_policy_field_headers(): client = CloudBillingClient(credentials=credentials.AnonymousCredentials()) # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. request = iam_policy.GetIamPolicyRequest(resource="resource/value") # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.get_iam_policy), "__call__") as call: call.return_value = policy.Policy() response = client.get_iam_policy(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the field header was sent. _, _, kw = call.mock_calls[0] assert ("x-goog-request-params", "resource=resource/value") in kw["metadata"]
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = datacatalog_v1beta1.PolicyTagManagerClient() response = client.get_iam_policy() assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'etag3123477' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request resource = client.instance_path('[PROJECT]', '[INSTANCE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request resource = client.source_path('[ORGANIZATION]', '[SOURCE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = tasks_v2beta3.CloudTasksClient(channel=channel) # Setup Request resource = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = subscriber_client.SubscriberClient(channel=channel) # Setup Request resource = client.subscription_path('[PROJECT]', '[SUBSCRIPTION]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = kms_v1.KeyManagementServiceClient(channel=channel) # Setup Request resource = client.key_ring_path('[PROJECT]', '[LOCATION]', '[KEY_RING]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request