def test_update(self): from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create request_pb request_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.DISPLAY_NAME, ) # Create response_pb response_pb = data_v2_pb2.Instance() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # Perform the method and check the result. result = instance.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateInstance', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_with_create_instance_metadata(self): from google.protobuf import any_pb2 from google.protobuf.timestamp_pb2 import Timestamp from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from gcloud.bigtable._generated import (bigtable_instance_admin_pb2 as messages_v2_pb) TYPE_URL = ('type.googleapis.com/' + messages_v2_pb._CREATEINSTANCEMETADATA.full_name) metadata = messages_v2_pb.CreateInstanceMetadata( request_time=Timestamp(seconds=1, nanos=1234), finish_time=Timestamp(seconds=10, nanos=891011), original_request=messages_v2_pb.CreateInstanceRequest( parent='foo', instance_id='bar', instance=data_v2_pb2.Instance(display_name='quux', ), ), ) any_val = any_pb2.Any( type_url=TYPE_URL, value=metadata.SerializeToString(), ) result = self._callFUT(any_val) self.assertEqual(result, metadata)
def test__update_from_pb_no_display_name(self): from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) instance_pb = data_v2_pb2.Instance() instance = self._makeOne(None, None, None, None) self.assertEqual(instance.display_name, None) with self.assertRaises(ValueError): instance._update_from_pb(instance_pb) self.assertEqual(instance.display_name, None)
def test_from_pb_bad_instance_name(self): from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) instance_name = 'INCORRECT_FORMAT' instance_pb = data_v2_pb2.Instance(name=instance_name) klass = self._getTargetClass() with self.assertRaises(ValueError): klass.from_pb(instance_pb, None)
def test__update_from_pb_success(self): from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) display_name = 'display_name' instance_pb = data_v2_pb2.Instance(display_name=display_name, ) instance = self._makeOne(None, None, None, None) self.assertEqual(instance.display_name, None) instance._update_from_pb(instance_pb) self.assertEqual(instance.display_name, display_name)
def test_from_pb_project_mistmatch(self): from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) ALT_PROJECT = 'ALT_PROJECT' client = _Client(project=ALT_PROJECT) self.assertNotEqual(self.PROJECT, ALT_PROJECT) instance_pb = data_v2_pb2.Instance(name=self.INSTANCE_NAME) klass = self._getTargetClass() with self.assertRaises(ValueError): klass.from_pb(instance_pb, client)
def test_from_pb_success(self): from gcloud.bigtable.instance import _EXISTING_INSTANCE_LOCATION_ID from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) client = _Client(project=self.PROJECT) instance_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.INSTANCE_ID, ) klass = self._getTargetClass() instance = klass.from_pb(instance_pb, client) self.assertTrue(isinstance(instance, klass)) self.assertEqual(instance._client, client) self.assertEqual(instance.instance_id, self.INSTANCE_ID) self.assertEqual(instance._cluster_location_id, _EXISTING_INSTANCE_LOCATION_ID)
def update(self): """Update this instance. .. note:: Updates the ``display_name``. To change that value before updating, reset its values via .. code:: python instance.display_name = 'New display name' before calling :meth:`update`. """ request_pb = data_v2_pb2.Instance( name=self.name, display_name=self.display_name, ) # Ignore the expected `data_v2_pb2.Instance`. self._client._instance_stub.UpdateInstance(request_pb)
def _prepare_create_request(instance): """Creates a protobuf request for a CreateInstance request. :type instance: :class:`Instance` :param instance: The instance to be created. :rtype: :class:`.messages_v2_pb2.CreateInstanceRequest` :returns: The CreateInstance request object containing the instance info. """ parent_name = ('projects/' + instance._client.project) message = messages_v2_pb2.CreateInstanceRequest( parent=parent_name, instance_id=instance.instance_id, instance=data_v2_pb2.Instance(display_name=instance.display_name, ), ) cluster = message.clusters[instance.instance_id] cluster.name = instance.name + '/clusters/' + instance.instance_id cluster.location = (parent_name + '/locations/' + instance._cluster_location_id) cluster.serve_nodes = instance._cluster_serve_nodes return message
def test_reload(self): from gcloud.bigtable._generated import ( instance_pb2 as data_v2_pb2) from gcloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb) from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID) # Create request_pb request_pb = messages_v2_pb.GetInstanceRequest( name=self.INSTANCE_NAME) # Create response_pb DISPLAY_NAME = u'hey-hi-hello' response_pb = data_v2_pb2.Instance( display_name=DISPLAY_NAME, ) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # reload() has no return value. # Check Instance optional config values before. self.assertEqual(instance.display_name, self.INSTANCE_ID) # Perform the method and check the result. result = instance.reload() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetInstance', (request_pb,), {}, )]) # Check Instance optional config values before. self.assertEqual(instance.display_name, DISPLAY_NAME)
def test_list_instances(self): from gcloud.bigtable._generated import ( instance_pb2 as data_v2_pb2) from gcloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from gcloud.bigtable._testing import _FakeStub LOCATION = 'projects/' + self.PROJECT + '/locations/locname' FAILED_LOCATION = 'FAILED' INSTANCE_ID1 = 'instance-id1' INSTANCE_ID2 = 'instance-id2' INSTANCE_NAME1 = ( 'projects/' + self.PROJECT + '/instances/' + INSTANCE_ID1) INSTANCE_NAME2 = ( 'projects/' + self.PROJECT + '/instances/' + INSTANCE_ID2) credentials = _Credentials() client = self._makeOne( project=self.PROJECT, credentials=credentials, admin=True, timeout_seconds=self.TIMEOUT_SECONDS, ) # Create request_pb request_pb = messages_v2_pb2.ListInstancesRequest( parent='projects/' + self.PROJECT, ) # Create response_pb response_pb = messages_v2_pb2.ListInstancesResponse( failed_locations=[ FAILED_LOCATION, ], instances=[ data_v2_pb2.Instance( name=INSTANCE_NAME1, display_name=INSTANCE_NAME1, ), data_v2_pb2.Instance( name=INSTANCE_NAME2, display_name=INSTANCE_NAME2, ), ], ) # Patch the stub used by the API method. client._instance_stub_internal = stub = _FakeStub(response_pb) # Create expected_result. failed_locations = [FAILED_LOCATION] instances = [ client.instance(INSTANCE_ID1, LOCATION), client.instance(INSTANCE_ID2, LOCATION), ] expected_result = (instances, failed_locations) # Perform the method and check the result. result = client.list_instances() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ListInstances', (request_pb, self.TIMEOUT_SECONDS), {}, )])