def _instance_api_response_for_create(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2, ) from google.cloud.bigtable_admin_v2.types import instance_pb2 NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = "type.googleapis.com/{}".format( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any(type_url=type_url, value=metadata.SerializeToString()), ) response = operation.from_gapic( response_pb, mock.Mock(), instance_pb2.Instance, metadata_type=messages_v2_pb2.CreateInstanceMetadata, ) project_path_template = "projects/{}" location_path_template = "projects/{}/locations/{}" instance_api = mock.create_autospec( bigtable_instance_admin_client.BigtableInstanceAdminClient) instance_api.create_instance.return_value = response instance_api.project_path = project_path_template.format instance_api.location_path = location_path_template.format return instance_api, response
def test_create(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable_admin_v2 import enums from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, display_name=self.DISPLAY_NAME) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create(location_id=self.LOCATION_ID) actual_request = channel.requests[0][1] cluster_id = '{}-cluster'.format(self.INSTANCE_ID) cluster = self._create_cluster( instance_api, cluster_id, self.LOCATION_ID, DEFAULT_SERVE_NODES, enums.StorageType.STORAGE_TYPE_UNSPECIFIED) expected_request = self._create_instance_request( self.DISPLAY_NAME, {cluster_id: cluster}) self.assertEqual(expected_request, actual_request) self.assertIsInstance(result, operation.Operation) # self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.CreateInstanceMetadata)
def test_update_app_profile_with_single_routing_policy(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from tests.unit._testing import _FakeStub from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Create response_pb NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. client._instance_admin_client = instance_api stub = _FakeStub(response_pb) client._instance_admin_client.bigtable_instance_admin_stub = stub update_mask = [] # Perform the method and check the result. app_profile_id = 'appProfileId1262094415' cluster_id = 'cluster-id' result = instance.update_app_profile(app_profile_id, update_mask=update_mask, routing_policy_type=2, cluster_id=cluster_id) self.assertIsInstance(result, operation.Operation)
def test_create(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from tests.unit._testing import _FakeStub from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. stub = _FakeStub(response_pb) client._instance_admin_client = api client._instance_admin_client.bigtable_instance_admin_stub = stub # Perform the method and check the result. result = instance.create() self.assertIsInstance(result, operation.Operation) # self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.CreateInstanceMetadata)
def test_create_w_clusters(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable import enums from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.DISPLAY_NAME, enums.Instance.Type.PRODUCTION, self.LABELS) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/{}'.format( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), ) ) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. cluster_id_1 = 'cluster-1' cluster_id_2 = 'cluster-2' location_id_1 = 'location-id-1' location_id_2 = 'location-id-2' serve_nodes_1 = 3 serve_nodes_2 = 5 clusters = [ Cluster(cluster_id_1, instance, location_id=location_id_1, serve_nodes=serve_nodes_1), Cluster(cluster_id_2, instance, location_id=location_id_2, serve_nodes=serve_nodes_2)] result = instance.create(clusters=clusters) actual_request = channel.requests[0][1] cluster_1_pb = self._create_cluster_pb( instance_api, cluster_id_1, location_id_1, serve_nodes_1, enums.StorageType.UNSPECIFIED) cluster_2_pb = self._create_cluster_pb( instance_api, cluster_id_2, location_id_2, serve_nodes_2, enums.StorageType.UNSPECIFIED) expected_request = self._create_instance_request( {cluster_id_1: cluster_1_pb, cluster_id_2: cluster_2_pb} ) self.assertEqual(expected_request, actual_request) self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.CreateInstanceMetadata)
def test_create(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable import enums from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.DISPLAY_NAME, enums.Instance.Type.PRODUCTION, self.LABELS) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/{}'.format( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), ) ) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. serve_nodes = 3 cluster_id = '{}-cluster'.format(self.INSTANCE_ID) # cluster = instance.cluster(cluster_id, location_id=self.LOCATION_ID, # serve_nodes=serve_nodes) # result = instance.create(clusters=[cluster]) # TODO: replace this example with above once the otpion is removed # from instance.create() method result = instance.create(location_id=self.LOCATION_ID, serve_nodes=serve_nodes) actual_request = channel.requests[0][1] cluster = self._create_cluster_pb( instance_api, cluster_id, self.LOCATION_ID, serve_nodes, enums.StorageType.UNSPECIFIED) expected_request = self._create_instance_request({cluster_id: cluster}) self.assertEqual(expected_request, actual_request) self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.CreateInstanceMetadata)