def test_list_clusters(self): 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.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.instance import Instance from google.cloud.bigtable.instance import Cluster instance_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) failed_location = 'FAILED' cluster_id1 = 'cluster-id1' cluster_id2 = 'cluster-id2' cluster_name1 = (client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, cluster_id1)) cluster_name2 = (client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, cluster_id2)) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[ failed_location ], clusters=[ data_v2_pb2.Cluster( name=cluster_name1, ), data_v2_pb2.Cluster( name=cluster_name2, ), ], ) # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.ListClusters.side_effect = [response_pb] # Perform the method and check the result. clusters, failed_locations = instance.list_clusters() cluster_1, cluster_2 = clusters self.assertIsInstance(cluster_1, Cluster) self.assertEqual(cluster_1.name, cluster_name1) self.assertIsInstance(cluster_2, Cluster) self.assertEqual(cluster_2.name, cluster_name2) self.assertEqual(failed_locations, [failed_location])
def test_list_clusters(self): 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.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.instance import Instance from google.cloud.bigtable.instance import Cluster instance_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) failed_location = 'FAILED' cluster_id1 = 'cluster-id1' cluster_id2 = 'cluster-id2' cluster_name1 = (client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, cluster_id1)) cluster_name2 = (client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, cluster_id2)) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[ failed_location ], clusters=[ data_v2_pb2.Cluster( name=cluster_name1, ), data_v2_pb2.Cluster( name=cluster_name2, ), ], ) # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.transport instance_stub.list_clusters.side_effect = [response_pb] # Perform the method and check the result. clusters, failed_locations = instance.list_clusters() cluster_1, cluster_2 = clusters self.assertIsInstance(cluster_1, Cluster) self.assertEqual(cluster_1.name, cluster_name1) self.assertIsInstance(cluster_2, Cluster) self.assertEqual(cluster_2.name, cluster_name2) self.assertEqual(failed_locations, [failed_location])
def test_instance_from_pb_bad_instance_name(): from google.cloud.bigtable_admin_v2.types import instance as data_v2_pb2 from google.cloud.bigtable.instance import Instance instance_name = "INCORRECT_FORMAT" instance_pb = data_v2_pb2.Instance(name=instance_name) with pytest.raises(ValueError): Instance.from_pb(instance_pb, None)
def test_list_clusters(self): 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.proto import instance_pb2 as data_v2_pb2 from google.cloud.bigtable.instance import Instance from google.cloud.bigtable.instance import Cluster credentials = _make_credentials() client = self._make_client( project=self.PROJECT, credentials=credentials, admin=True ) instance = Instance(self.INSTANCE_ID, client) failed_location = "FAILED" cluster_id1 = "cluster-id1" cluster_id2 = "cluster-id2" cluster_path_template = "projects/{}/instances/{}/clusters/{}" cluster_name1 = cluster_path_template.format( self.PROJECT, self.INSTANCE_ID, cluster_id1 ) cluster_name2 = cluster_path_template.format( self.PROJECT, self.INSTANCE_ID, cluster_id2 ) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[failed_location], clusters=[ data_v2_pb2.Cluster(name=cluster_name1), data_v2_pb2.Cluster(name=cluster_name2), ], ) # Patch the stub used by the API method. instance_api = mock.create_autospec( bigtable_instance_admin_client.BigtableInstanceAdminClient ) instance_api.list_clusters.side_effect = [response_pb] instance_api.cluster_path = cluster_path_template.format client._instance_admin_client = instance_api # Perform the method and check the result. clusters, failed_locations = instance.list_clusters() cluster_1, cluster_2 = clusters self.assertIsInstance(cluster_1, Cluster) self.assertEqual(cluster_1.name, cluster_name1) self.assertIsInstance(cluster_2, Cluster) self.assertEqual(cluster_2.name, cluster_name2) self.assertEqual(failed_locations, [failed_location])
def test_name_property(self): from google.cloud.bigtable.instance import Instance channel = self._make_channel() client = self._make_client(project=self.PROJECT, channel=channel, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance) instance = Instance(self.INSTANCE_ID, client) self.assertEqual(cluster.name, self.CLUSTER_NAME)
def delete_instance(self, project_id, instance_id): """ Deletes the specified Cloud Bigtable instance. Raises google.api_core.exceptions.NotFound if the Cloud Bigtable instance does not exist. :param project_id: The ID of the GCP project. :type project_id: str :param instance_id: The ID of the Cloud Bigtable instance. :type instance_id: str """ instance = Instance(instance_id, self.get_client(project_id)) instance.delete()
def test_list_clusters(self): 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.proto import instance_pb2 as data_v2_pb2 from google.cloud.bigtable.instance import Instance from google.cloud.bigtable.instance import Cluster credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = Instance(self.INSTANCE_ID, client) failed_location = "FAILED" cluster_id1 = "cluster-id1" cluster_id2 = "cluster-id2" cluster_path_template = "projects/{}/instances/{}/clusters/{}" cluster_name1 = cluster_path_template.format(self.PROJECT, self.INSTANCE_ID, cluster_id1) cluster_name2 = cluster_path_template.format(self.PROJECT, self.INSTANCE_ID, cluster_id2) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[failed_location], clusters=[ data_v2_pb2.Cluster(name=cluster_name1), data_v2_pb2.Cluster(name=cluster_name2), ], ) # Patch the stub used by the API method. instance_api = mock.create_autospec( bigtable_instance_admin_client.BigtableInstanceAdminClient) instance_api.list_clusters.side_effect = [response_pb] instance_api.cluster_path = cluster_path_template.format client._instance_admin_client = instance_api # Perform the method and check the result. clusters, failed_locations = instance.list_clusters() cluster_1, cluster_2 = clusters self.assertIsInstance(cluster_1, Cluster) self.assertEqual(cluster_1.name, cluster_name1) self.assertIsInstance(cluster_2, Cluster) self.assertEqual(cluster_2.name, cluster_name2) self.assertEqual(failed_locations, [failed_location])
def test_instance_from_pb_project_mistmatch(): from google.cloud.bigtable_admin_v2.types import instance as data_v2_pb2 from google.cloud.bigtable.instance import Instance ALT_PROJECT = "ALT_PROJECT" credentials = _make_credentials() client = _make_client(project=ALT_PROJECT, credentials=credentials, admin=True) instance_pb = data_v2_pb2.Instance(name=INSTANCE_NAME) with pytest.raises(ValueError): Instance.from_pb(instance_pb, client)
def test_instance_update(): from google.cloud.bigtable import enums from google.protobuf import field_mask_pb2 from google.cloud.bigtable_admin_v2.types import Instance credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) instance = _make_instance( INSTANCE_ID, client, display_name=DISPLAY_NAME, instance_type=enums.Instance.Type.DEVELOPMENT, labels=LABELS, ) api, response = _instance_api_response_for_update() client._instance_admin_client = api result = instance.update() assert result is response instance_pb = Instance( name=instance.name, display_name=instance.display_name, type_=instance.type_, labels=instance.labels, ) update_mask_pb = field_mask_pb2.FieldMask( paths=["display_name", "type", "labels"]) api.partial_update_instance.assert_called_once_with( request={ "instance": instance_pb, "update_mask": update_mask_pb })
def test_w_defaults(self): from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb) from google.cloud.bigtable.instance import Instance client = _Client(self.PROJECT) instance = Instance(self.INSTANCE_ID, client, self.LOCATION_ID) request_pb = self._call_fut(instance) self.assertIsInstance(request_pb, messages_v2_pb.CreateInstanceRequest) self.assertEqual(request_pb.instance_id, self.INSTANCE_ID) self.assertEqual(request_pb.parent, self.PARENT) self.assertIsInstance(request_pb.instance, data_v2_pb2.Instance) self.assertEqual(request_pb.instance.name, u'') self.assertEqual(request_pb.instance.display_name, self.INSTANCE_ID) # An instance must also define a same-named cluster cluster = request_pb.clusters[self.INSTANCE_ID] self.assertIsInstance(cluster, data_v2_pb2.Cluster) self.assertEqual(cluster.name, self.CLUSTER_NAME) self.assertEqual(cluster.location, self.LOCATION_NAME) self.assertEqual(cluster.serve_nodes, DEFAULT_SERVE_NODES)
def test_w_explicit_serve_nodes(self): from google.cloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb) from google.cloud.bigtable.instance import Instance DISPLAY_NAME = u'DISPLAY_NAME' SERVE_NODES = 5 client = _Client(self.PROJECT) instance = Instance(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=DISPLAY_NAME, serve_nodes=SERVE_NODES) request_pb = self._call_fut(instance) self.assertIsInstance(request_pb, messages_v2_pb.CreateInstanceRequest) self.assertEqual(request_pb.instance_id, self.INSTANCE_ID) self.assertEqual(request_pb.parent, 'projects/' + self.PROJECT) self.assertIsInstance(request_pb.instance, data_v2_pb2.Instance) self.assertEqual(request_pb.instance.display_name, DISPLAY_NAME) # An instance must also define a same-named cluster cluster = request_pb.clusters[self.INSTANCE_ID] self.assertIsInstance(cluster, data_v2_pb2.Cluster) self.assertEqual(cluster.location, self.LOCATION_NAME) self.assertEqual(cluster.serve_nodes, SERVE_NODES)
def test_create_instance_with_multiple_replica_clusters( self, get_client, instance_create, cluster, mock_project_id ): operation = mock.Mock() operation.result_return_value = Instance(instance_id=CBT_INSTANCE, client=get_client) instance_create.return_value = operation res = self.bigtable_hook_default_project_id.create_instance( instance_id=CBT_INSTANCE, main_cluster_id=CBT_CLUSTER, main_cluster_zone=CBT_ZONE, replica_clusters=CBT_REPLICATE_CLUSTERS, cluster_nodes=1, cluster_storage_type=enums.StorageType.SSD, project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST, ) cluster.assert_has_calls( [ unittest.mock.call(CBT_CLUSTER, CBT_ZONE, 1, enums.StorageType.SSD), unittest.mock.call('replica-1', 'us-west1-a', 1, enums.StorageType.SSD), unittest.mock.call('replica-2', 'us-central1-f', 1, enums.StorageType.SSD), unittest.mock.call('replica-3', 'us-east1-d', 1, enums.StorageType.SSD), ], any_order=True, ) get_client.assert_called_once_with(project_id='example-project') instance_create.assert_called_once_with(clusters=mock.ANY) self.assertEqual(res.instance_id, 'instance')
def test_create_instance_with_one_replica_cluster_development( self, get_client, instance_create, cluster, mock_project_id ): operation = mock.Mock() operation.result_return_value = Instance(instance_id=CBT_INSTANCE, client=get_client) instance_create.return_value = operation res = self.bigtable_hook_default_project_id.create_instance( instance_id=CBT_INSTANCE, main_cluster_id=CBT_CLUSTER, main_cluster_zone=CBT_ZONE, replica_cluster_id=CBT_REPLICA_CLUSTER_ID, replica_cluster_zone=CBT_REPLICA_CLUSTER_ZONE, cluster_nodes=1, cluster_storage_type=enums.StorageType.SSD, project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST, instance_type=enums.Instance.Type.DEVELOPMENT, ) cluster.assert_has_calls( [ unittest.mock.call( cluster_id=CBT_CLUSTER, location_id=CBT_ZONE, default_storage_type=enums.StorageType.SSD ), unittest.mock.call( CBT_REPLICA_CLUSTER_ID, CBT_REPLICA_CLUSTER_ZONE, 1, enums.StorageType.SSD ), ], any_order=True, ) get_client.assert_called_once_with(project_id='example-project') instance_create.assert_called_once_with(clusters=mock.ANY) assert res.instance_id == 'instance'
def instance(self, instance_id, location=_EXISTING_INSTANCE_LOCATION_ID, display_name=None, serve_nodes=DEFAULT_SERVE_NODES): """Factory to create a instance associated with this client. :type instance_id: str :param instance_id: The ID of the instance. :type location: str :param location: location name, in form ``projects/<project>/locations/<location>``; used to set up the instance's cluster. :type display_name: str :param display_name: (Optional) The display name for the instance in the Cloud Console UI. (Must be between 4 and 30 characters.) If this value is not set in the constructor, will fall back to the instance ID. :type serve_nodes: int :param serve_nodes: (Optional) The number of nodes in the instance's cluster; used to set up the instance's cluster. :rtype: :class:`~google.cloud.bigtable.instance.Instance` :returns: an instance owned by this client. """ return Instance(instance_id, self, location, display_name=display_name, serve_nodes=serve_nodes)
def test_instance_update_empty(): from google.protobuf import field_mask_pb2 from google.cloud.bigtable_admin_v2.types import Instance credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) instance = _make_instance(None, client) api, response = _instance_api_response_for_update() client._instance_admin_client = api result = instance.update() assert result is response instance_pb = Instance( name=instance.name, display_name=instance.display_name, type_=instance.type_, labels=instance.labels, ) update_mask_pb = field_mask_pb2.FieldMask() api.partial_update_instance.assert_called_once_with( request={ "instance": instance_pb, "update_mask": update_mask_pb })
def test_create(self): from google.api_core import operation from google.longrunning import operations_pb2 from google.cloud.bigtable.instance import Instance from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) 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) cluster = self._make_one(self.CLUSTER_ID, instance, self.LOCATION_ID) # Create response_pb OP_ID = 5678 OP_NAME = ( 'operations/projects/{}/instances/{}/clusters/{}/operations/{}' .format(self.PROJECT, self.INSTANCE_ID, self.CLUSTER_ID, OP_ID)) response_pb = operations_pb2.Operation(name=OP_NAME) # Patch the stub used by the API method. client._instance_admin_client = api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.CreateCluster.side_effect = [response_pb] # Perform the method and check the result. result = cluster.create() self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, OP_NAME) self.assertIsNone(result.metadata)
def test_delete(self): from google.protobuf import empty_pb2 from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES from google.cloud.bigtable.instance import Instance channel = self._make_channel() client = self._make_client(project=self.PROJECT, channel=channel, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance, serve_nodes=DEFAULT_SERVE_NODES) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.DeleteCluster.side_effect = [response_pb] # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = cluster.delete() self.assertEqual(result, expected_result)
def test_reload(self): from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES from google.cloud.bigtable.instance import Instance LOCATION = 'LOCATION' channel = self._make_channel() client = self._make_client(project=self.PROJECT, channel=channel, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance) # Create response_pb response_pb = _ClusterPB( serve_nodes=DEFAULT_SERVE_NODES, location=LOCATION, ) # Patch the stub used by the API method. instance_admin_client = client._instance_admin_client instance_stub = 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 Cluster optional config values before. self.assertEqual(cluster.serve_nodes, DEFAULT_SERVE_NODES) # Perform the method and check the result. result = cluster.reload() self.assertEqual(result, expected_result)
def test_create(self): from google.api_core import operation from google.longrunning import operations_pb2 from google.cloud.bigtable.instance import Instance channel = self._make_channel() client = self._make_client(project=self.PROJECT, channel=channel, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance) # Create response_pb OP_ID = 5678 OP_NAME = ( 'operations/projects/%s/instances/%s/clusters/%s/operations/%d' % (self.PROJECT, self.INSTANCE_ID, self.CLUSTER_ID, OP_ID)) response_pb = operations_pb2.Operation(name=OP_NAME) # Patch the stub used by the API method. instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.CreateCluster.side_effect = [response_pb] # Perform the method and check the result. result = cluster.create() self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, OP_NAME) self.assertIsNone(result.metadata)
def test_instance_from_pb_success(): from google.cloud.bigtable_admin_v2.types import instance as data_v2_pb2 from google.cloud.bigtable import enums from google.cloud.bigtable.instance import Instance credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) instance_type = enums.Instance.Type.PRODUCTION state = enums.Instance.State.READY instance_pb = data_v2_pb2.Instance( name=INSTANCE_NAME, display_name=INSTANCE_ID, type_=instance_type, labels=LABELS, state=state, ) instance = Instance.from_pb(instance_pb, client) assert isinstance(instance, Instance) assert instance._client == client assert instance.instance_id == INSTANCE_ID assert instance.display_name == INSTANCE_ID assert instance.type_ == instance_type assert instance.labels == LABELS assert instance._state == state
def test_update(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.instance import Instance from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) SERVE_NODES = 81 channel = self._make_channel() client = self._make_client(project=self.PROJECT, channel=channel, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance, serve_nodes=SERVE_NODES) # Create request_pb request_pb = _ClusterPB( name=self.CLUSTER_NAME, serve_nodes=SERVE_NODES, ) # Create response_pb OP_ID = 5678 OP_NAME = ( 'operations/projects/%s/instances/%s/clusters/%s/operations/%d' % (self.PROJECT, self.INSTANCE_ID, self.CLUSTER_ID, OP_ID)) metadata = messages_v2_pb2.UpdateClusterMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.UpdateClusterMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=OP_NAME, metadata=Any(type_url=type_url, value=metadata.SerializeToString())) # Patch the stub used by the API method. instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.UpdateCluster.side_effect = [response_pb] result = cluster.update() self.assertIsInstance(result, operation.Operation) self.assertIsInstance(result.metadata, messages_v2_pb2.UpdateClusterMetadata) self.assertIsInstance(request_pb, data_v2_pb2.Cluster) self.assertEqual(request_pb.name, self.CLUSTER_NAME) self.assertEqual(request_pb.serve_nodes, SERVE_NODES)
def get_instance(self, project_id, instance_id): """ Retrieves and returns the specified Cloud Bigtable instance if it exists. Otherwise, returns None. :param project_id: The ID of the GCP project. :type project_id: str :param instance_id: The ID of the Cloud Bigtable instance. :type instance_id: str """ client = self.get_client(project_id) instance = Instance(instance_id, client) if not instance.exists(): return None return instance
def update_instance( self, instance_id: str, project_id: str, instance_display_name: Optional[str] = None, instance_type: Optional[Union[enums.Instance.Type, enum.IntEnum]] = None, instance_labels: Optional[Dict] = None, timeout: Optional[float] = None, ) -> Instance: """ Update an existing instance. :type instance_id: str :param instance_id: The ID for the existing instance. :type project_id: str :param project_id: Optional, Google Cloud project ID where the BigTable exists. If set to None or missing, the default project_id from the Google Cloud connection is used. :type instance_display_name: str :param instance_display_name: (optional) Human-readable name of the instance. :type instance_type: enums.Instance.Type or enum.IntEnum :param instance_type: (optional) The type of the instance. :type instance_labels: dict :param instance_labels: (optional) Dictionary of labels to associate with the instance. :type timeout: int :param timeout: (optional) timeout (in seconds) for instance update. If None is not specified, Operator will wait indefinitely. """ instance_type = enums.Instance.Type(instance_type) instance = Instance( instance_id=instance_id, client=self._get_client(project_id=project_id), display_name=instance_display_name, instance_type=instance_type, labels=instance_labels, ) operation = instance.update() operation.result(timeout) return instance
def test_cluster_create_w_cmek(): import datetime from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.types import ( bigtable_instance_admin as messages_v2_pb2, ) from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable.instance import Instance from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2 from google.cloud.bigtable.enums import StorageType NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) STORAGE_TYPE_SSD = StorageType.SSD LOCATION = LOCATION_PATH + LOCATION_ID instance = Instance(INSTANCE_ID, client) cluster = _make_cluster( CLUSTER_ID, instance, location_id=LOCATION_ID, serve_nodes=SERVE_NODES, default_storage_type=STORAGE_TYPE_SSD, kms_key_name=KMS_KEY_NAME, ) name = instance.name metadata = messages_v2_pb2.CreateClusterMetadata(request_time=NOW_PB) type_url = "type.googleapis.com/{}".format( messages_v2_pb2.CreateClusterMetadata._meta._pb.DESCRIPTOR.full_name) response_pb = operations_pb2.Operation( name=OP_NAME, metadata=Any(type_url=type_url, value=metadata._pb.SerializeToString()), ) api = client._instance_admin_client = _make_instance_admin_client() api.common_location_path.return_value = LOCATION api.instance_path.return_value = name api.create_cluster.return_value = response_pb cluster.create() expected_request_cluster = instance_v2_pb2.Cluster( location=LOCATION, serve_nodes=cluster.serve_nodes, default_storage_type=cluster.default_storage_type, encryption_config=instance_v2_pb2.Cluster.EncryptionConfig( kms_key_name=KMS_KEY_NAME, ), ) expected_request = { "parent": instance.name, "cluster_id": CLUSTER_ID, "cluster": expected_request_cluster, } api.create_cluster.assert_called_once_with(request=expected_request)
def test_name_property(self): from google.cloud.bigtable.instance import Instance credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance, self.LOCATION_ID) self.assertEqual(cluster.name, self.CLUSTER_NAME)
def test_create_instance(self, get_client, instance_create): operation = mock.Mock() operation.result_return_value = Instance(instance_id=CBT_INSTANCE, client=get_client) instance_create.return_value = operation res = self.bigtable_hook_default_project_id.create_instance( instance_id=CBT_INSTANCE, main_cluster_id=CBT_CLUSTER, main_cluster_zone=CBT_ZONE) get_client.assert_called_once_with(project_id='example-project') instance_create.assert_called_once_with(clusters=mock.ANY) self.assertEqual(res.instance_id, 'instance')
def instance(self, instance_id, display_name=None, instance_type=None, labels=None): """Factory to create a instance associated with this client. For example: .. literalinclude:: snippets.py :start-after: [START bigtable_api_create_prod_instance] :end-before: [END bigtable_api_create_prod_instance] :dedent: 4 :type instance_id: str :param instance_id: The ID of the instance. :type display_name: str :param display_name: (Optional) The display name for the instance in the Cloud Console UI. (Must be between 4 and 30 characters.) If this value is not set in the constructor, will fall back to the instance ID. :type instance_type: int :param instance_type: (Optional) The type of the instance. Possible values are represented by the following constants: :data:`google.cloud.bigtable.instance.InstanceType.PRODUCTION`. :data:`google.cloud.bigtable.instance.InstanceType.DEVELOPMENT`, Defaults to :data:`google.cloud.bigtable.instance.InstanceType.UNSPECIFIED`. :type labels: dict :param labels: (Optional) Labels are a flexible and lightweight mechanism for organizing cloud resources into groups that reflect a customer's organizational needs and deployment strategies. They can be used to filter resources and aggregate metrics. Label keys must be between 1 and 63 characters long. Maximum 64 labels can be associated with a given resource. Label values must be between 0 and 63 characters long. Keys and values must both be under 128 bytes. :rtype: :class:`~google.cloud.bigtable.instance.Instance` :returns: an instance owned by this client. """ return Instance( instance_id, self, display_name=display_name, instance_type=instance_type, labels=labels, )
def test_create(self): from google.cloud.bigtable import enums from google.cloud.bigtable_admin_v2.types import Instance from google.cloud.bigtable_admin_v2.types import Cluster import warnings 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, ) instance_api, response = self._instance_api_response_for_create() instance_api.common_project_path.return_value = "projects/project" client._instance_admin_client = instance_api serve_nodes = 3 with warnings.catch_warnings(record=True) as warned: result = instance.create(location_id=self.LOCATION_ID, serve_nodes=serve_nodes) cluster_pb = Cluster( location=instance_api.location_path(self.PROJECT, self.LOCATION_ID), serve_nodes=serve_nodes, default_storage_type=enums.StorageType.UNSPECIFIED, ) instance_pb = Instance( display_name=self.DISPLAY_NAME, type_=enums.Instance.Type.PRODUCTION, labels=self.LABELS, ) cluster_id = "{}-cluster".format(self.INSTANCE_ID) instance_api.create_instance.assert_called_once_with( request={ "parent": instance_api.project_path(self.PROJECT), "instance_id": self.INSTANCE_ID, "instance": instance_pb, "clusters": { cluster_id: cluster_pb }, }) self.assertEqual(len(warned), 1) self.assertIs(warned[0].category, DeprecationWarning) self.assertIs(result, response)
def test_create_instance(self, get_client, instance_create, mock_project_id): operation = mock.Mock() operation.result_return_value = Instance(instance_id=CBT_INSTANCE, client=get_client) instance_create.return_value = operation res = self.bigtable_hook_default_project_id.create_instance( instance_id=CBT_INSTANCE, main_cluster_id=CBT_CLUSTER, main_cluster_zone=CBT_ZONE, project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST, ) get_client.assert_called_once_with(project_id='example-project') instance_create.assert_called_once_with(clusters=mock.ANY) assert res.instance_id == 'instance'
def test_create_instance_overridden_project_id(self, get_client, instance_create): operation = mock.Mock() operation.result_return_value = Instance(instance_id=CBT_INSTANCE, client=get_client) instance_create.return_value = operation res = self.bigtable_hook_default_project_id.create_instance( project_id='new-project', instance_id=CBT_INSTANCE, main_cluster_id=CBT_CLUSTER, main_cluster_zone=CBT_ZONE, ) get_client.assert_called_once_with(project_id='new-project') instance_create.assert_called_once_with(clusters=mock.ANY) assert res.instance_id == 'instance'
def list_instances(self): """List instances owned by the project. :rtype: tuple :returns: (instances, failed_locations), where 'instances' is list of :class:`google.cloud.bigtable.instance.Instance`, and 'failed_locations' is a list of locations which could not be resolved. """ resp = self.instance_admin_client.list_instances(self.project_path) instances = [ Instance.from_pb(instance, self) for instance in resp.instances] return instances, resp.failed_locations
def test_update_instance(self, get_client, instance_update, mock_project_id): operation = mock.Mock() operation.result_return_value = Instance(instance_id=CBT_INSTANCE, client=get_client) instance_update.return_value = operation res = self.bigtable_hook_default_project_id.update_instance( instance_id=CBT_INSTANCE, instance_display_name=CBT_INSTANCE_DISPLAY_NAME, instance_type=CBT_INSTANCE_TYPE, instance_labels=CBT_INSTANCE_LABELS, project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST, ) get_client.assert_called_once_with(project_id='example-project') instance_update.assert_called_once_with() self.assertEqual(res.instance_id, 'instance')
def list_instances(self): """List instances owned by the project. :rtype: tuple :returns: (instances, failed_locations), where 'instances' is list of :class:`google.cloud.bigtable.instance.Instance`, and 'failed_locations' is a list of locations which could not be resolved. """ resp = self.instance_admin_client.list_instances(self.project_path) instances = [ Instance.from_pb(instance, self) for instance in resp.instances ] return instances, resp.failed_locations
def list_instances(self): """List instances owned by the project. :rtype: tuple :returns: A pair of results, the first is a list of :class:`.Instance` objects returned and the second is a list of strings (the failed locations in the request). """ request_pb = bigtable_instance_admin_pb2.ListInstancesRequest( parent=self.project_name) response = self._instance_stub.ListInstances(request_pb) instances = [Instance.from_pb(instance_pb, self) for instance_pb in response.instances] return instances, response.failed_locations
def list_instances(self): """List instances owned by the project. For example: .. literalinclude:: snippets.py :start-after: [START bigtable_list_instances] :end-before: [END bigtable_list_instances] :rtype: tuple :returns: (instances, failed_locations), where 'instances' is list of :class:`google.cloud.bigtable.instance.Instance`, and 'failed_locations' is a list of locations which could not be resolved. """ resp = self.instance_admin_client.list_instances(self.project_path) instances = [Instance.from_pb(instance, self) for instance in resp.instances] return instances, resp.failed_locations
def create_instance(self, instance_id, main_cluster_id, main_cluster_zone, project_id=None, replica_cluster_id=None, replica_cluster_zone=None, instance_display_name=None, instance_type=enums.Instance.Type.TYPE_UNSPECIFIED, instance_labels=None, cluster_nodes=None, cluster_storage_type=enums.StorageType.STORAGE_TYPE_UNSPECIFIED, timeout=None): """ Creates new instance. :type instance_id: str :param instance_id: The ID for the new instance. :type main_cluster_id: str :param main_cluster_id: The ID for main cluster for the new instance. :type main_cluster_zone: str :param main_cluster_zone: The zone for main cluster. See https://cloud.google.com/bigtable/docs/locations for more details. :type project_id: str :param project_id: Optional, Google Cloud Platform project ID where the BigTable exists. If set to None or missing, the default project_id from the GCP connection is used. :type replica_cluster_id: str :param replica_cluster_id: (optional) The ID for replica cluster for the new instance. :type replica_cluster_zone: str :param replica_cluster_zone: (optional) The zone for replica cluster. :type instance_type: enums.Instance.Type :param instance_type: (optional) The type of the instance. :type instance_display_name: str :param instance_display_name: (optional) Human-readable name of the instance. Defaults to ``instance_id``. :type instance_labels: dict :param instance_labels: (optional) Dictionary of labels to associate with the instance. :type cluster_nodes: int :param cluster_nodes: (optional) Number of nodes for cluster. :type cluster_storage_type: enums.StorageType :param cluster_storage_type: (optional) The type of storage. :type timeout: int :param timeout: (optional) timeout (in seconds) for instance creation. If None is not specified, Operator will wait indefinitely. """ cluster_storage_type = enums.StorageType(cluster_storage_type) instance_type = enums.Instance.Type(instance_type) instance = Instance( instance_id, self._get_client(project_id=project_id), instance_display_name, instance_type, instance_labels, ) clusters = [ instance.cluster( main_cluster_id, main_cluster_zone, cluster_nodes, cluster_storage_type ) ] if replica_cluster_id and replica_cluster_zone: clusters.append(instance.cluster( replica_cluster_id, replica_cluster_zone, cluster_nodes, cluster_storage_type )) operation = instance.create( clusters=clusters ) operation.result(timeout) return instance