def _create_cluster_pb(self, instance_api, cluster_id, location_id, serve_nodes, storage_type): from google.cloud.bigtable_admin_v2.types import instance_pb2 location = instance_api.location_path(self.PROJECT, location_id) return instance_pb2.Cluster(location=location, serve_nodes=serve_nodes, default_storage_type=storage_type)
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_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2, ) from google.cloud.bigtable_admin_v2.types import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client from google.cloud.bigtable.enums import StorageType 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 ) STORAGE_TYPE_SSD = StorageType.SSD instance = _Instance(self.INSTANCE_ID, client) cluster = self._make_one( self.CLUSTER_ID, instance, location_id=self.LOCATION_ID, serve_nodes=self.SERVE_NODES, default_storage_type=STORAGE_TYPE_SSD, ) # Create expected_request expected_request = instance_pb2.Cluster( name=cluster.name, serve_nodes=self.SERVE_NODES ) metadata = messages_v2_pb2.UpdateClusterMetadata(request_time=NOW_PB) type_url = "type.googleapis.com/{}".format( messages_v2_pb2.UpdateClusterMetadata.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]) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel ) client._instance_admin_client = api # Perform the method and check the result. result = cluster.update() 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.UpdateClusterMetadata)
def _to_pb(self): """ Create cluster proto buff message for API calls """ client = self._instance._client location = client.instance_admin_client.location_path( client.project, self.location_id) cluster_pb = instance_pb2.Cluster( location=location, serve_nodes=self.serve_nodes, default_storage_type=self.default_storage_type) return cluster_pb
def _create_pb_request(self): """ Create cluster proto buff message for API calls """ client = self._instance._client cluster_name = client.instance_admin_client.cluster_path( client.project, self._instance.instance_id, self.cluster_id) location = client.instance_admin_client.location_path( client.project, self.location) cluster_message = instance_pb2.Cluster( name=cluster_name, location=location, serve_nodes=self.serve_nodes, default_storage_type=self.default_storage_type) return cluster_message
def test_create(self): from google.cloud.bigtable import enums from google.cloud.bigtable_admin_v2.types import instance_pb2 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() 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 = instance_pb2.Cluster( location=instance_api.location_path(self.PROJECT, self.LOCATION_ID), serve_nodes=serve_nodes, default_storage_type=enums.StorageType.UNSPECIFIED, ) instance_pb = instance_pb2.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( 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 create(self): """Create this instance. .. 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: The long-running operation corresponding to the create operation. """ clusters = {} cluster_id = '{}-cluster'.format(self.instance_id) cluster_name = self._client._instance_admin_client.cluster_path( self._client.project, self.instance_id, cluster_id) location = self._client._instance_admin_client.location_path( self._client.project, self._cluster_location_id) cluster = instance_pb2.Cluster( name=cluster_name, location=location, serve_nodes=self._cluster_serve_nodes, default_storage_type=self._default_storage_type) instance = instance_pb2.Instance(display_name=self.display_name) clusters[cluster_id] = cluster parent = self._client.project_path return self._client._instance_admin_client.create_instance( parent=parent, instance_id=self.instance_id, instance=instance, clusters=clusters)
def test_bigtable_cluster_from_pb(): # [START bigtable_cluster_from_pb] from google.cloud.bigtable import Client from google.cloud.bigtable_admin_v2.types import instance_pb2 client = Client(admin=True) instance = client.instance(INSTANCE_ID) cluster = instance.cluster(CLUSTER_ID) name = cluster.name cluster_state = cluster.state cluster_pb = instance_pb2.Cluster( name=name, location=LOCATION_ID, state=cluster_state, serve_nodes=SERVER_NODES, default_storage_type=STORAGE_TYPE, ) cluster2 = cluster.from_pb(cluster_pb, instance) # [END bigtable_cluster_from_pb] assert cluster2.name == cluster.name
def test_create_w_clusters(self): from google.cloud.bigtable import enums from google.cloud.bigtable_admin_v2.types import instance_pb2 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() 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) cluster_pb_1 = instance_pb2.Cluster( location=instance_api.location_path(self.PROJECT, location_id_1), serve_nodes=serve_nodes_1, default_storage_type=enums.StorageType.UNSPECIFIED, ) cluster_pb_2 = instance_pb2.Cluster( location=instance_api.location_path(self.PROJECT, location_id_2), serve_nodes=serve_nodes_2, default_storage_type=enums.StorageType.UNSPECIFIED, ) instance_pb = instance_pb2.Instance( display_name=self.DISPLAY_NAME, type=enums.Instance.Type.PRODUCTION, labels=self.LABELS, ) instance_api.create_instance.assert_called_once_with( parent=instance_api.project_path(self.PROJECT), instance_id=self.INSTANCE_ID, instance=instance_pb, clusters={ cluster_id_1: cluster_pb_1, cluster_id_2: cluster_pb_2 }, ) self.assertIs(result, response)
def create(self, location_id=_EXISTING_INSTANCE_LOCATION_ID, serve_nodes=DEFAULT_SERVE_NODES, default_storage_type=_STORAGE_TYPE_UNSPECIFIED): """Create this instance. .. 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`. :type location_id: str :param location_id: ID of the location in which the instance will be created. Required for instances which do not yet exist. :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. :type default_storage_type: int :param default_storage_type: (Optional) The default values are STORAGE_TYPE_UNSPECIFIED = 0: The user did not specify a storage type. SSD = 1: Flash (SSD) storage should be used. HDD = 2: Magnetic drive (HDD) storage should be used. :rtype: :class:`~google.api_core.operation.Operation` :returns: The long-running operation corresponding to the create operation. """ clusters = {} cluster_id = '{}-cluster'.format(self.instance_id) cluster_name = self._client.instance_admin_client.cluster_path( self._client.project, self.instance_id, cluster_id) location = self._client.instance_admin_client.location_path( self._client.project, location_id) cluster = instance_pb2.Cluster( name=cluster_name, location=location, serve_nodes=serve_nodes, default_storage_type=default_storage_type) instance = instance_pb2.Instance(display_name=self.display_name) clusters[cluster_id] = cluster parent = self._client.project_path return self._client.instance_admin_client.create_instance( parent=parent, instance_id=self.instance_id, instance=instance, clusters=clusters)
def create(self, location_id=_EXISTING_INSTANCE_LOCATION_ID, serve_nodes=DEFAULT_SERVE_NODES, default_storage_type=None): """Create this instance. .. 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`. :type location_id: str :param location_id: ID of the location in which the instance will be created. Required for instances which do not yet exist. :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. :type default_storage_type: int :param default_storage_type: (Optional) The storage media type for persisting Bigtable data. Possible values are represented by the following constants: :data:`google.cloud.bigtable.enums.StorageType.SSD`. :data:`google.cloud.bigtable.enums.StorageType.SHD`, Defaults to :data:`google.cloud.bigtable.enums.StorageType.UNSPECIFIED`. :rtype: :class:`~google.api_core.operation.Operation` :returns: The long-running operation corresponding to the create operation. """ clusters = {} cluster_id = '{}-cluster'.format(self.instance_id) cluster_name = self._client.instance_admin_client.cluster_path( self._client.project, self.instance_id, cluster_id) location = self._client.instance_admin_client.location_path( self._client.project, location_id) cluster = instance_pb2.Cluster( name=cluster_name, location=location, serve_nodes=serve_nodes, default_storage_type=default_storage_type) instance = instance_pb2.Instance(display_name=self.display_name, type=self.type_, labels=self.labels) clusters[cluster_id] = cluster parent = self._client.project_path return self._client.instance_admin_client.create_instance( parent=parent, instance_id=self.instance_id, instance=instance, clusters=clusters)