def test_reload_routing_any(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable.enums import RoutingPolicyType api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = _Instance(self.INSTANCE_ID, client) routing = RoutingPolicyType.ANY description = 'routing policy any' app_profile = self._make_one(self.APP_PROFILE_ID, instance, routing_policy_type=routing, description=description) # Create response_pb description_from_server = 'routing policy switched to single' cluster_id_from_server = self.CLUSTER_ID allow_transactional_writes = True single_cluster_routing = (data_v2_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id_from_server, allow_transactional_writes=allow_transactional_writes)) response_pb = data_v2_pb2.AppProfile( name=app_profile.name, single_cluster_routing=single_cluster_routing, description=description_from_server) # Patch the stub used by the API method. client._instance_admin_client = api instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) instance_stub.GetCluster.side_effect = [response_pb] # Create expected_result. expected_result = None # reload() has no return value. # Check app_profile config values before. self.assertEqual(app_profile.routing_policy_type, routing) self.assertEqual(app_profile.description, description) self.assertIsNone(app_profile.cluster_id) self.assertIsNone(app_profile.allow_transactional_writes) # Perform the method and check the result. result = app_profile.reload() self.assertEqual(result, expected_result) self.assertEqual(app_profile.routing_policy_type, RoutingPolicyType.SINGLE) self.assertEqual(app_profile.description, description_from_server) self.assertEqual(app_profile.cluster_id, cluster_id_from_server) self.assertEqual(app_profile.allow_transactional_writes, allow_transactional_writes)
def test_from_pb_success_routing_single(self): from google.cloud.bigtable_admin_v2.types import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable.enums import RoutingPolicyType client = _Client(self.PROJECT) instance = _Instance(self.INSTANCE_ID, client) desctiption = 'routing single' allow_transactional_writes = True routing = RoutingPolicyType.SINGLE single_cluster_routing = (data_v2_pb2.AppProfile.SingleClusterRouting( cluster_id=self.CLUSTER_ID, allow_transactional_writes=allow_transactional_writes)) app_profile_pb = data_v2_pb2.AppProfile( name=self.APP_PROFILE_NAME, description=desctiption, single_cluster_routing=single_cluster_routing) klass = self._get_target_class() app_profile = klass.from_pb(app_profile_pb, instance) self.assertIsInstance(app_profile, klass) self.assertIs(app_profile._instance, instance) self.assertEqual(app_profile.app_profile_id, self.APP_PROFILE_ID) self.assertEqual(app_profile.description, desctiption) self.assertEqual(app_profile.routing_policy_type, routing) self.assertEqual(app_profile.cluster_id, self.CLUSTER_ID) self.assertEqual(app_profile.allow_transactional_writes, allow_transactional_writes)
def _to_pb(self): """Create an AppProfile proto buff message for API calls :rtype: :class:`.instance_pb2.AppProfile` :returns: The converted current object. :raises: :class:`ValueError <exceptions.ValueError>` if the AppProfile routing_policy_type is not set """ if not self.routing_policy_type: raise ValueError("AppProfile required routing policy.") single_cluster_routing = None multi_cluster_routing_use_any = None if self.routing_policy_type == RoutingPolicyType.ANY: multi_cluster_routing_use_any = ( instance_pb2.AppProfile.MultiClusterRoutingUseAny()) else: single_cluster_routing = instance_pb2.AppProfile.SingleClusterRouting( cluster_id=self.cluster_id, allow_transactional_writes=self.allow_transactional_writes, ) app_profile_pb = instance_pb2.AppProfile( name=self.name, description=self.description, multi_cluster_routing_use_any=multi_cluster_routing_use_any, single_cluster_routing=single_cluster_routing, ) return app_profile_pb
def test_from_pb_success_routing_any(self): from google.cloud.bigtable_admin_v2.types import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable.enums import RoutingPolicyType client = _Client(self.PROJECT) instance = _Instance(self.INSTANCE_ID, client) desctiption = 'routing any' routing = RoutingPolicyType.ANY multi_cluster_routing_use_any = ( data_v2_pb2.AppProfile.MultiClusterRoutingUseAny()) app_profile_pb = data_v2_pb2.AppProfile( name=self.APP_PROFILE_NAME, description=desctiption, multi_cluster_routing_use_any=multi_cluster_routing_use_any) klass = self._get_target_class() app_profile = klass.from_pb(app_profile_pb, instance) self.assertIsInstance(app_profile, klass) self.assertIs(app_profile._instance, instance) self.assertEqual(app_profile.app_profile_id, self.APP_PROFILE_ID) self.assertEqual(app_profile.description, desctiption) self.assertEqual(app_profile.routing_policy_type, routing) self.assertIsNone(app_profile.cluster_id) self.assertEqual(app_profile.allow_transactional_writes, False)
def test_exists(self): from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 from google.api_core import exceptions instance_api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = client.instance(self.INSTANCE_ID) # Create response_pb response_pb = data_v2_pb2.AppProfile(name=self.APP_PROFILE_NAME) client._instance_admin_client = instance_api # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_stub = client._instance_admin_client.transport instance_stub.get_app_profile.side_effect = [ response_pb, exceptions.NotFound("testing"), exceptions.BadRequest("testing"), ] # Perform the method and check the result. non_existing_app_profile_id = "other-app-profile-id" app_profile = self._make_one(self.APP_PROFILE_ID, instance) alt_app_profile = self._make_one(non_existing_app_profile_id, instance) self.assertTrue(app_profile.exists()) self.assertFalse(alt_app_profile.exists()) with self.assertRaises(exceptions.BadRequest): alt_app_profile.exists()
def test_from_pb_bad_app_profile_name(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 bad_app_profile_name = "BAD_NAME" app_profile_pb = data_v2_pb2.AppProfile(name=bad_app_profile_name) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(app_profile_pb, None)
def test_create_app_profile_with_single_routing_policy(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable.enums import RoutingPolicyType credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) description = 'description-1724546052' app_profile_id = 'appProfileId1262094415' cluster_id = 'cluster-id' expected_response = { 'name': self.APP_PROFILE_PATH + app_profile_id, 'description': description, 'single_cluster_routing': instance_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id, allow_transactional_writes=False) } expected_request = { 'app_profile_id': app_profile_id, 'routing_policy_type': RoutingPolicyType.SINGLE, 'description': description, 'cluster_id': cluster_id } expected_response = instance_pb2.AppProfile(**expected_response) channel = ChannelStub(responses=[expected_response]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Patch the stub used by the API method. client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create_app_profile(**expected_request) parent = client._instance_admin_client.instance_path( self.PROJECT, self.INSTANCE_ID) expected_request = _CreateAppProfileRequestPB( parent=parent, app_profile_id=app_profile_id, app_profile=expected_response, ) actual_request = channel.requests[0][1] self.assertEqual(expected_request, actual_request) self.assertEqual(result, expected_response)
def test_from_pb_project_mistmatch(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 ALT_PROJECT = "ALT_PROJECT" client = _Client(project=ALT_PROJECT) instance = _Instance(self.INSTANCE_ID, client) self.assertEqual(client.project, ALT_PROJECT) app_profile_pb = data_v2_pb2.AppProfile(name=self.APP_PROFILE_NAME) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(app_profile_pb, instance)
def test_from_pb_instance_id_mistmatch(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) ALT_INSTANCE_ID = 'ALT_INSTANCE_ID' client = _Client(self.PROJECT) instance = _Instance(ALT_INSTANCE_ID, client) self.assertEqual(instance.instance_id, ALT_INSTANCE_ID) app_profile_pb = data_v2_pb2.AppProfile(name=self.APP_PROFILE_NAME) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(app_profile_pb, instance)
def test_create_app_profile_with_multi_routing_policy(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) description = 'description-1724546052' app_profile_id = 'appProfileId1262094415' expected_response = { 'name': self.APP_PROFILE_PATH + app_profile_id, 'description': description, 'multi_cluster_routing_use_any': instance_pb2.AppProfile.MultiClusterRoutingUseAny() } expected_request = { 'app_profile_id': app_profile_id, 'routing_policy_type': 1, 'description': description } expected_response = instance_pb2.AppProfile(**expected_response) channel = ChannelStub(responses=[expected_response]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Patch the stub used by the API method. client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create_app_profile(**expected_request) parent = client._instance_admin_client.instance_path( self.PROJECT, self.INSTANCE_ID) expected_request = _CreateAppProfileRequestPB( parent=parent, app_profile_id=app_profile_id, app_profile=expected_response, ) actual_request = channel.requests[0][1] assert expected_request == actual_request self.assertEqual(result, expected_response)
def update_app_profile(self, app_profile_id, update_mask, routing_policy_type, description='', ignore_warnings=None, cluster_id=None, allow_transactional_writes=False): """Updates an app profile within an instance. :type: app_profile_id: str :param app_profile_id: The unique name for the new app profile. :type: update_mask: list :param: update_mask: Name of the parameters of AppProfiles that needed to update. :type: routing_policy_type: int :param: routing_policy_type: There are two routing policies ROUTING_POLICY_TYPE_ANY = 1 and ROUTING_POLICY_TYPE_SINGLE = 2. If ROUTING_POLICY_TYPE_ANY which will create a MultiClusterRoutingUseAny policy and if ROUTING_POLICY_TYPE_ANY is specified, a SingleClusterRouting policy will be created using the cluster_id and allow_transactional_writes parameters. :type: description: str :param: description: (Optional) Optional long form description of the use case for this AppProfile. :type: ignore_warnings: bool :param: ignore_warnings: (Optional) If true, ignore safety checks when creating the app profile. :type: cluster_id: str :param: cluster_id: (Optional) Unique cluster_id which is only required when routing_policy_type is ROUTING_POLICY_TYPE_SINGLE. :type: allow_transactional_writes: bool :param: allow_transactional_writes: (Optional) If true, allow transactional writes for ROUTING_POLICY_TYPE_SINGLE. :rtype: :class:`~google.cloud.bigtable_admin_v2.types.AppProfile` :return: The AppProfile instance. :raises: :class:`ValueError <exceptions.ValueError>` If routing policy is not set. """ if not routing_policy_type: raise ValueError('AppProfile required routing policy.') single_cluster_routing = None multi_cluster_routing_use_any = None instance_admin_client = self._client._instance_admin_client name = instance_admin_client.app_profile_path(self._client.project, self.instance_id, app_profile_id) if routing_policy_type == ROUTING_POLICY_TYPE_ANY: multi_cluster_routing_use_any = ( instance_pb2.AppProfile.MultiClusterRoutingUseAny()) if routing_policy_type == ROUTING_POLICY_TYPE_SINGLE: single_cluster_routing = ( instance_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id, allow_transactional_writes=allow_transactional_writes)) update_app_profile = instance_pb2.AppProfile( name=name, description=description, multi_cluster_routing_use_any=multi_cluster_routing_use_any, single_cluster_routing=single_cluster_routing) update_mask = field_mask_pb2.FieldMask(paths=update_mask) return self._client._instance_admin_client.update_app_profile( app_profile=update_app_profile, update_mask=update_mask, ignore_warnings=ignore_warnings)
def update_app_profile(self, app_profile_id, routing_policy_type, description=None, ignore_warnings=None, cluster_id=None, allow_transactional_writes=False): """Updates an app profile within an instance. :type: app_profile_id: str :param app_profile_id: The unique name for the new app profile. :type: update_mask: list :param: update_mask: Name of the parameters of AppProfiles that needed to update. :type: routing_policy_type: int :param: routing_policy_type: The type of the routing policy. Possible values are represented by the following constants: :data:`google.cloud.bigtable.enums.RoutingPolicyType.ANY` :data:`google.cloud.bigtable.enums.RoutingPolicyType.SINGLE` :type: description: str :param: description: (Optional) Optional long form description of the use case for this AppProfile. :type: ignore_warnings: bool :param: ignore_warnings: (Optional) If true, ignore safety checks when creating the app profile. :type: cluster_id: str :param: cluster_id: (Optional) Unique cluster_id which is only required when routing_policy_type is ROUTING_POLICY_TYPE_SINGLE. :type: allow_transactional_writes: bool :param: allow_transactional_writes: (Optional) If true, allow transactional writes for ROUTING_POLICY_TYPE_SINGLE. :rtype: :class:`~google.cloud.bigtable_admin_v2.types.AppProfile` :return: The AppProfile instance. :raises: :class:`ValueError <exceptions.ValueError>` If routing policy is not set. """ if not routing_policy_type: raise ValueError('AppProfile required routing policy.') update_mask_pb = field_mask_pb2.FieldMask() single_cluster_routing = None multi_cluster_routing_use_any = None instance_admin_client = self._client._instance_admin_client name = instance_admin_client.app_profile_path(self._client.project, self.instance_id, app_profile_id) if description is not None: update_mask_pb.paths.append('description') if routing_policy_type == RoutingPolicyType.ANY: multi_cluster_routing_use_any = ( instance_pb2.AppProfile.MultiClusterRoutingUseAny()) update_mask_pb.paths.append('multi_cluster_routing_use_any') if routing_policy_type == RoutingPolicyType.SINGLE: single_cluster_routing = ( instance_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id, allow_transactional_writes=allow_transactional_writes)) update_mask_pb.paths.append('single_cluster_routing') update_app_profile_pb = instance_pb2.AppProfile( name=name, description=description, multi_cluster_routing_use_any=multi_cluster_routing_use_any, single_cluster_routing=single_cluster_routing) return self._client._instance_admin_client.update_app_profile( app_profile=update_app_profile_pb, update_mask=update_mask_pb, ignore_warnings=ignore_warnings)
def test_update_app_profile_multi_cluster_routing_policy(self): 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.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.types import instance_pb2 from google.protobuf import field_mask_pb2 from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as instance_v2_pb2) from google.cloud.bigtable.enums import RoutingPolicyType 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 metadata = messages_v2_pb2.UpdateAppProfileMetadata() type_url = 'type.googleapis.com/{}'.format( messages_v2_pb2.UpdateAppProfileMetadata.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)) # Mock api calls client._instance_admin_client = instance_api # Perform the method and check the result. description = 'description-1724546052' app_profile_id = 'appProfileId1262094415' ignore_warnings = True multi_cluster_routing_use_any = ( instance_pb2.AppProfile.MultiClusterRoutingUseAny()) expected_request_app_profile = instance_pb2.AppProfile( name=self.APP_PROFILE_PATH + app_profile_id, description=description, multi_cluster_routing_use_any=multi_cluster_routing_use_any) expected_request_update_mask = field_mask_pb2.FieldMask( paths=['description', 'multi_cluster_routing_use_any']) expected_request = instance_v2_pb2.UpdateAppProfileRequest( app_profile=expected_request_app_profile, update_mask=expected_request_update_mask, ignore_warnings=ignore_warnings) result = instance.update_app_profile(app_profile_id, RoutingPolicyType.ANY, description=description, ignore_warnings=ignore_warnings) actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.UpdateAppProfileMetadata)