def test_update(self): from google.cloud.bigtable import enums from google.protobuf import field_mask_pb2 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, display_name=self.DISPLAY_NAME, instance_type=enums.Instance.Type.DEVELOPMENT, labels=self.LABELS, ) instance_api, response = self._instance_api_response_for_update() client._instance_admin_client = instance_api result = instance.update() instance_pb = instance_pb2.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"]) instance_api.partial_update_instance.assert_called_once_with( instance=instance_pb, update_mask=update_mask_pb) self.assertIs(result, response)
def test_update_empty(self): from google.protobuf import field_mask_pb2 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(None, client) instance_api, response = self._instance_api_response_for_update() client._instance_admin_client = instance_api result = instance.update() instance_pb = instance_pb2.Instance( name=instance.name, display_name=instance.display_name, type=instance.type_, labels=instance.labels, ) update_mask_pb = field_mask_pb2.FieldMask() instance_api.partial_update_instance.assert_called_once_with( instance=instance_pb, update_mask=update_mask_pb) self.assertIs(result, response)
def _create_instance_request(self, display_name, clusters): 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 instance = instance_pb2.Instance(display_name=display_name) return messages_v2_pb2.CreateInstanceRequest( parent='projects/%s' % (self.PROJECT), instance_id=self.INSTANCE_ID, instance=instance, clusters=clusters)
def _create_instance_request(self, clusters): 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 import enums instance = instance_pb2.Instance(display_name=self.DISPLAY_NAME, type=enums.InstanceType.PRODUCTION, labels=self.LABELS) return messages_v2_pb2.CreateInstanceRequest( parent='projects/%s' % (self.PROJECT), instance_id=self.INSTANCE_ID, instance=instance, clusters=clusters)
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 test_bigtable_instance_from_pb(): # [START bigtable_instance_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) name = instance.name instance_pb = instance_pb2.Instance(name=name, display_name=INSTANCE_ID, type=PRODUCTION, labels=LABELS) instance2 = instance.from_pb(instance_pb, client) # [END bigtable_instance_from_pb] assert instance2.name == instance.name
def update(self): """Updates an instance within a project. For example: .. literalinclude:: snippets.py :start-after: [START bigtable_update_instance] :end-before: [END bigtable_update_instance] :dedent: 4 .. note:: Updates any or all of the following values: ``display_name`` ``type`` ``labels`` To change a value before updating, assign that values via .. code:: python instance.display_name = 'New display name' before calling :meth:`update`. :rtype: :class:`~google.api_core.operation.Operation` :returns: The long-running operation corresponding to the update operation. """ update_mask_pb = field_mask_pb2.FieldMask() if self.display_name is not None: update_mask_pb.paths.append("display_name") if self.type_ is not None: update_mask_pb.paths.append("type") if self.labels is not None: update_mask_pb.paths.append("labels") instance_pb = instance_pb2.Instance( name=self.name, display_name=self.display_name, type=self.type_, labels=self.labels, ) return self._client.instance_admin_client.partial_update_instance( instance=instance_pb, update_mask=update_mask_pb)
def test_update_empty(self): from google.api_core import operation from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.longrunning import operations_pb2 from google.protobuf import field_mask_pb2 from google.cloud.bigtable_admin_v2.types import instance_pb2 from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as instance_v2_pb2) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(None, client) expected_request_instance = instance_pb2.Instance( name=instance.name, display_name=instance.display_name, type=instance.type_, labels=instance.labels) expected_request_update_mask = field_mask_pb2.FieldMask() expected_request = instance_v2_pb2.PartialUpdateInstanceRequest( instance=expected_request_instance, update_mask=expected_request_update_mask) response_pb = operations_pb2.Operation(name=self.OP_NAME) 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. result = instance.update() actual_request = channel.requests[0][1] self.assertIsInstance(result, operation.Operation) self.assertEqual(actual_request, expected_request)
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 update(self): """Updates an instance within a project. .. note:: Updates any or all of the following values: ``display_name`` ``type`` ``labels`` To change a value before updating, assign that values via .. code:: python instance.display_name = 'New display name' before calling :meth:`update`. :rtype: :class:`~google.api_core.operation.Operation` :returns: The long-running operation corresponding to the update operation. """ update_mask_pb = field_mask_pb2.FieldMask() if self.display_name is not None: update_mask_pb.paths.append('display_name') if self.type_ is not None: update_mask_pb.paths.append('type') if self.labels is not None: update_mask_pb.paths.append('labels') instance_pb = instance_pb2.Instance(name=self.name, display_name=self.display_name, type=self.type_, labels=self.labels) return self._client.instance_admin_client.partial_update_instance( instance=instance_pb, update_mask=update_mask_pb)
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=None, serve_nodes=None, default_storage_type=None, clusters=None, ): """Create this instance. For example: .. literalinclude:: snippets.py :start-after: [START bigtable_create_prod_instance] :end-before: [END bigtable_create_prod_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: (Creation Only) The location where nodes and storage of the cluster owned by this instance reside. For best performance, clients should be located as close as possible to cluster's location. For list of supported locations refer to https://cloud.google.com/bigtable/docs/locations :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`. :type clusters: class:`~[~google.cloud.bigtable.cluster.Cluster]` :param clusters: List of clusters to be created. :rtype: :class:`~google.api_core.operation.Operation` :returns: The long-running operation corresponding to the create operation. :raises: :class:`ValueError <exceptions.ValueError>` if both ``clusters`` and one of ``location_id``, ``serve_nodes`` and ``default_storage_type`` are set. """ if clusters is None: cluster_id = "{}-cluster".format(self.instance_id) clusters = [ self.cluster( cluster_id, location_id=location_id, serve_nodes=serve_nodes, default_storage_type=default_storage_type, ) ] elif (location_id is not None or serve_nodes is not None or default_storage_type is not None): raise ValueError("clusters and one of location_id, serve_nodes, \ default_storage_type can not be set \ simultaneously.") instance_pb = instance_pb2.Instance(display_name=self.display_name, type=self.type_, labels=self.labels) parent = self._client.project_path return self._client.instance_admin_client.create_instance( parent=parent, instance_id=self.instance_id, instance=instance_pb, clusters={c.cluster_id: c._to_pb() for c in clusters}, )
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)
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.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) from google.protobuf import field_mask_pb2 from google.cloud.bigtable_admin_v2.types import instance_pb2 from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as instance_v2_pb2) 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, instance_type=enums.Instance.Type.DEVELOPMENT, labels=self.LABELS) expected_request_instance = instance_pb2.Instance( name=instance.name, display_name=instance.display_name, type=instance.type_, labels=instance.labels) expected_request_update_mask = field_mask_pb2.FieldMask( paths=['display_name', 'type', 'labels']) expected_request = instance_v2_pb2.PartialUpdateInstanceRequest( instance=expected_request_instance, update_mask=expected_request_update_mask) metadata = messages_v2_pb2.UpdateInstanceMetadata( request_time=NOW_PB) type_url = 'type.googleapis.com/{}'.format( messages_v2_pb2.UpdateInstanceMetadata.DESCRIPTOR.full_name) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), ) ) 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. result = instance.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.UpdateInstanceMetadata)
def create(self, location_id=_EXISTING_INSTANCE_LOCATION_ID, serve_nodes=DEFAULT_SERVE_NODES, default_storage_type=None, clusters=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`. :type clusters: class:`~[~google.cloud.bigtable.cluster.Cluster]` :param clusters: List of clusters to be created. :rtype: :class:`~google.api_core.operation.Operation` :returns: The long-running operation corresponding to the create operation. :raises: :class:`ValueError <exceptions.ValueError>` if both ``clusters`` and one of ``location_id``, ``serve_nodes`` and ``default_storage_type`` are set. """ if clusters is None: cluster_id = '{}-cluster'.format(self.instance_id) clusters = [ Cluster(cluster_id, self, location_id, serve_nodes, default_storage_type) ] elif (location_id is not None or serve_nodes is not None or default_storage_type is not None): raise ValueError("clusters and one of location_id, serve_nodes, \ default_storage_type can not be set \ simultaneously.") instance = instance_pb2.Instance(display_name=self.display_name, type=self.type_, labels=self.labels) parent = self._client.project_path return self._client.instance_admin_client.create_instance( parent=parent, instance_id=self.instance_id, instance=instance, clusters={c.cluster_id: c._create_pb_request() for c in clusters})