def create(self): """Create this instance. See https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.CreateInstance .. note:: Uses the ``project`` and ``instance_id`` on the current :class:`Instance` in addition to the ``display_name``. To change them before creating, reset the values via .. code:: python instance.display_name = 'New display name' instance.instance_id = 'i-changed-my-mind' before calling :meth:`create`. :rtype: :class:`google.api_core.operation.Operation` :returns: an operation instance :raises Conflict: if the instance already exists """ api = self._client.instance_admin_api instance_pb = admin_v1_pb2.Instance( name=self.name, config=self.configuration_name, display_name=self.display_name, node_count=self.node_count, ) metadata = _metadata_with_prefix(self.name) future = api.create_instance( parent=self._client.project_name, instance_id=self.instance_id, instance=instance_pb, metadata=metadata, ) return future
def test_create_instance(self): # Setup Expected Response name = "name3373707" config = "config-1354792126" display_name = "displayName1615086568" node_count = 1539922066 expected_response = { "name": name, "config": config, "display_name": display_name, "node_count": node_count, } expected_response = spanner_instance_admin_pb2.Instance(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_instance", done=True ) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) 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 parent = client.project_path("[PROJECT]") instance_id = "instanceId-2101995259" instance = {} response = client.create_instance(parent, instance_id, instance) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = spanner_instance_admin_pb2.CreateInstanceRequest( parent=parent, instance_id=instance_id, instance=instance ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_instances(self): from google.cloud.spanner_admin_instance_v1.gapic import instance_admin_client from google.cloud.spanner_admin_instance_v1.proto import ( spanner_instance_admin_pb2, ) from google.cloud.spanner_v1.client import Instance api = instance_admin_client.InstanceAdminClient(mock.Mock()) credentials = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=credentials) client._instance_admin_api = api instance_pbs = spanner_instance_admin_pb2.ListInstancesResponse( instances=[ spanner_instance_admin_pb2.Instance( name=self.INSTANCE_NAME, config=self.CONFIGURATION_NAME, display_name=self.DISPLAY_NAME, node_count=self.NODE_COUNT, ) ]) li_api = api._inner_api_calls["list_instances"] = mock.Mock( return_value=instance_pbs) response = client.list_instances() instances = list(response) instance = instances[0] self.assertIsInstance(instance, Instance) self.assertEqual(instance.name, self.INSTANCE_NAME) self.assertEqual(instance.configuration_name, self.CONFIGURATION_NAME) self.assertEqual(instance.display_name, self.DISPLAY_NAME) self.assertEqual(instance.node_count, self.NODE_COUNT) li_api.assert_called_once_with( spanner_instance_admin_pb2.ListInstancesRequest(parent=self.PATH), metadata=[("google-cloud-resource-prefix", client.project_name)], retry=mock.ANY, timeout=mock.ANY, )
def update(self): """Update this instance. See https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.UpdateInstance .. note:: Updates the ``display_name`` and ``node_count``. To change those values before updating, set them via .. code:: python instance.display_name = 'New display name' instance.node_count = 5 before calling :meth:`update`. :rtype: :class:`google.api_core.operation.Operation` :returns: an operation instance :raises NotFound: if the instance does not exist """ api = self._client.instance_admin_api instance_pb = admin_v1_pb2.Instance( name=self.name, config=self.configuration_name, display_name=self.display_name, node_count=self.node_count, ) field_mask = FieldMask(paths=['config', 'display_name', 'node_count']) metadata = _metadata_with_prefix(self.name) future = api.update_instance( instance=instance_pb, field_mask=field_mask, metadata=metadata, ) return future
def test_exists_success(self): from google.cloud.spanner_admin_instance_v1.proto import ( spanner_instance_admin_pb2 as admin_v1_pb2, ) client = _Client(self.PROJECT) instance_pb = admin_v1_pb2.Instance( name=self.INSTANCE_NAME, config=self.CONFIG_NAME, display_name=self.DISPLAY_NAME, node_count=self.NODE_COUNT, ) api = client.instance_admin_api = _FauxInstanceAdminAPI( _get_instance_response=instance_pb ) instance = self._make_one(self.INSTANCE_ID, client) self.assertTrue(instance.exists()) name, metadata = api._got_instance self.assertEqual(name, self.INSTANCE_NAME) self.assertEqual(metadata, [("google-cloud-resource-prefix", instance.name)])
def test_get_instance(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_admin_instance_v1.InstanceAdminClient() # Mock request name = client.instance_path('[PROJECT]', '[INSTANCE]') # Mock response name_2 = 'name2-1052831874' config = 'config-1354792126' display_name = 'displayName1615086568' node_count = 1539922066 expected_response = { 'name': name_2, 'config': config, 'display_name': display_name, 'node_count': node_count } expected_response = spanner_instance_admin_pb2.Instance( **expected_response) grpc_stub.GetInstance.return_value = expected_response response = client.get_instance(name) self.assertEqual(expected_response, response) grpc_stub.GetInstance.assert_called_once() args, kwargs = grpc_stub.GetInstance.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_instance_admin_pb2.GetInstanceRequest( name=name) self.assertEqual(expected_request, actual_request)
def test_update_instance(self): # Setup Expected Response name = 'name3373707' config = 'config-1354792126' display_name = 'displayName1615086568' node_count = 1539922066 expected_response = { 'name': name, 'config': config, 'display_name': display_name, 'node_count': node_count } expected_response = spanner_instance_admin_pb2.Instance( **expected_response) operation = operations_pb2.Operation( name='operations/test_update_instance', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) 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 instance = {} field_mask = {} response = client.update_instance(instance, field_mask) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = spanner_instance_admin_pb2.UpdateInstanceRequest( instance=instance, field_mask=field_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_instance(self): # Setup Expected Response name = 'name3373707' config = 'config-1354792126' display_name = 'displayName1615086568' node_count = 1539922066 expected_response = { 'name': name, 'config': config, 'display_name': display_name, 'node_count': node_count } expected_response = spanner_instance_admin_pb2.Instance( **expected_response) operation = operations_pb2.Operation( name='operations/test_create_instance', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = spanner_admin_instance_v1.InstanceAdminClient(channel=channel) # Setup Request parent = client.project_path('[PROJECT]') instance_id = 'instanceId-2101995259' instance = {} response = client.create_instance(parent, instance_id, instance) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = spanner_instance_admin_pb2.CreateInstanceRequest( parent=parent, instance_id=instance_id, instance=instance) actual_request = channel.requests[0][1] assert expected_request == actual_request