예제 #1
0
    def update_cluster(instance, cluster_id, nodes):
        """
        Updates number of nodes in the specified Cloud Bigtable cluster.
        Raises google.api_core.exceptions.NotFound if the cluster does not exist.

        :type instance: Instance
        :param instance: The Cloud Bigtable instance that owns the cluster.
        :type cluster_id: str
        :param cluster_id: The ID of the cluster.
        :type nodes: int
        :param nodes: The desired number of nodes.
        """
        cluster = Cluster(cluster_id, instance)
        cluster.serve_nodes = nodes
        cluster.update()
예제 #2
0
    def list_clusters(self):
        """List the clusters in the project.

        For example:

        .. literalinclude:: snippets.py
            :start-after: [START bigtable_list_clusters_in_project]
            :end-before: [END bigtable_list_clusters_in_project]

        :rtype: tuple
        :returns:
            (clusters, failed_locations), where 'clusters' is list of
            :class:`google.cloud.bigtable.instance.Cluster`, and
            'failed_locations' is a list of strings representing
            locations which could not be resolved.
        """
        resp = self.instance_admin_client.list_clusters(
            self.instance_admin_client.instance_path(self.project, "-")
        )
        clusters = []
        instances = {}
        for cluster in resp.clusters:
            match_cluster_name = _CLUSTER_NAME_RE.match(cluster.name)
            instance_id = match_cluster_name.group("instance")
            if instance_id not in instances:
                instances[instance_id] = self.instance(instance_id)
            clusters.append(Cluster.from_pb(cluster, instances[instance_id]))
        return clusters, resp.failed_locations
예제 #3
0
    def test_it(self):
        from google.cloud.bigtable.cluster import Cluster

        PROJECT = 'PROJECT'
        INSTANCE_ID = 'instance-id'
        CLUSTER_ID = 'cluster-id'
        SERVE_NODES = 8

        client = _Client(PROJECT)
        instance = _Instance(INSTANCE_ID, client)
        cluster = Cluster(CLUSTER_ID, instance,
                          serve_nodes=SERVE_NODES)
        cluster.location = u'projects/prahj-ekt/locations/zona-tres'

        request_pb = self._call_fut(cluster)

        self.assertEqual(request_pb.cluster_id, CLUSTER_ID)
        self.assertEqual(request_pb.parent, instance.name)
        self.assertEqual(request_pb.cluster.serve_nodes, SERVE_NODES)
        self.assertEqual(request_pb.cluster.location, cluster.location)
예제 #4
0
    def cluster(self, cluster_id, serve_nodes=3):
        """Factory to create a cluster associated with this client.

        :type cluster_id: str
        :param cluster_id: The ID of the cluster.

        :type serve_nodes: int
        :param serve_nodes: (Optional) The number of nodes in the cluster.
                            Defaults to 3.

        :rtype: :class:`.Cluster`
        :returns: The cluster owned by this client.
        """
        return Cluster(cluster_id, self, serve_nodes=serve_nodes)
예제 #5
0
    def list_clusters(self):
        """List the clusters in this instance.

        :rtype: tuple
        :returns:
            (clusters, failed_locations), where 'clusters' is list of
            :class:`google.cloud.bigtable.instance.Cluster`, and
            'failed_locations' is a list of locations which could not
            be resolved.
        """
        resp = self._client.instance_admin_client.list_clusters(self.name)
        clusters = [
            Cluster.from_pb(cluster, self) for cluster in resp.clusters]
        return clusters, resp.failed_locations
예제 #6
0
def test_instance_create():
    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 = _make_client(project=PROJECT, credentials=credentials, admin=True)
    instance = _make_instance(
        INSTANCE_ID,
        client,
        DISPLAY_NAME,
        enums.Instance.Type.PRODUCTION,
        LABELS,
    )
    api, response = _instance_api_response_for_create()
    client._instance_admin_client = api
    api.common_project_path.return_value = "projects/project"
    serve_nodes = 3

    with warnings.catch_warnings(record=True) as warned:
        result = instance.create(location_id=LOCATION_ID,
                                 serve_nodes=serve_nodes)

    assert result is response

    cluster_pb = Cluster(
        location=api.location_path(PROJECT, LOCATION_ID),
        serve_nodes=serve_nodes,
        default_storage_type=enums.StorageType.UNSPECIFIED,
    )
    instance_pb = Instance(
        display_name=DISPLAY_NAME,
        type_=enums.Instance.Type.PRODUCTION,
        labels=LABELS,
    )
    cluster_id = "{}-cluster".format(INSTANCE_ID)
    api.create_instance.assert_called_once_with(
        request={
            "parent": api.project_path(PROJECT),
            "instance_id": INSTANCE_ID,
            "instance": instance_pb,
            "clusters": {
                cluster_id: cluster_pb
            },
        })

    assert len(warned) == 1
    assert warned[0].category is DeprecationWarning
예제 #7
0
    def cluster(self,
                cluster_id,
                location_id=None,
                serve_nodes=None,
                default_storage_type=None):
        """Factory to create a cluster associated with this instance.

        For example:

        .. literalinclude:: snippets.py
            :start-after: [START bigtable_create_cluster]
            :end-before: [END bigtable_create_cluster]

        :type cluster_id: str
        :param cluster_id: The ID of the cluster.

        :type instance: :class:`~google.cloud.bigtable.instance.Instance`
        :param instance: The instance where the cluster resides.

        :type location_id: str
        :param location_id: (Creation Only) The location where this cluster's
                            nodes and storage reside. For best performance,
                            clients should be located as close as possible to
                            this cluster.
                            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 cluster.

        :type default_storage_type: int
        :param default_storage_type: (Optional) The type of storage
                                     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.cloud.bigtable.instance.Cluster`
        :returns: a cluster owned by this instance.
        """
        return Cluster(
            cluster_id,
            self,
            location_id=location_id,
            serve_nodes=serve_nodes,
            default_storage_type=default_storage_type,
        )
    def test_it(self):
        from google.cloud.bigtable.cluster import Cluster

        PROJECT = 'PROJECT'
        INSTANCE_ID = 'instance-id'
        CLUSTER_ID = 'cluster-id'
        SERVE_NODES = 8

        client = _Client(PROJECT)
        instance = _Instance(INSTANCE_ID, client)
        cluster = Cluster(CLUSTER_ID, instance, serve_nodes=SERVE_NODES)

        request_pb = self._callFUT(cluster)

        self.assertEqual(request_pb.cluster_id, CLUSTER_ID)
        self.assertEqual(request_pb.parent, instance.name)
        self.assertEqual(request_pb.cluster.serve_nodes, SERVE_NODES)
예제 #9
0
    def list_clusters(self):
        """Lists clusters in this instance.

        :rtype: tuple
        :returns: A pair of results, the first is a list of :class:`.Cluster` s
                  returned and the second is a list of strings (the failed
                  locations in the request).
        """
        request_pb = messages_v2_pb2.ListClustersRequest(parent=self.name)
        # We expect a `.cluster_messages_v1_pb2.ListClustersResponse`
        list_clusters_response = self._client._instance_stub.ListClusters(
            request_pb)

        failed_locations = [
            location for location in list_clusters_response.failed_locations]
        clusters = [Cluster.from_pb(cluster_pb, self)
                    for cluster_pb in list_clusters_response.clusters]
        return clusters, failed_locations
예제 #10
0
    def list_clusters(self):
        """List the clusters in this instance.

        For example:

        .. literalinclude:: snippets.py
            :start-after: [START bigtable_list_clusters_on_instance]
            :end-before: [END bigtable_list_clusters_on_instance]

        :rtype: tuple
        :returns:
            (clusters, failed_locations), where 'clusters' is list of
            :class:`google.cloud.bigtable.instance.Cluster`, and
            'failed_locations' is a list of locations which could not
            be resolved.
        """
        resp = self._client.instance_admin_client.list_clusters(self.name)
        clusters = [Cluster.from_pb(cluster, self) for cluster in resp.clusters]
        return clusters, resp.failed_locations
예제 #11
0
    def list_clusters(self):
        """List the clusters in this instance.

        For example:

        .. literalinclude:: snippets.py
            :start-after: [START bigtable_list_clusters_on_instance]
            :end-before: [END bigtable_list_clusters_on_instance]

        :rtype: tuple
        :returns:
            (clusters, failed_locations), where 'clusters' is list of
            :class:`google.cloud.bigtable.instance.Cluster`, and
            'failed_locations' is a list of locations which could not
            be resolved.
        """
        resp = self._client.instance_admin_client.list_clusters(self.name)
        clusters = [Cluster.from_pb(cluster, self) for cluster in resp.clusters]
        return clusters, resp.failed_locations
예제 #12
0
    def list_clusters(self):
        """List the clusters in the project.

        :rtype: tuple
        :returns:
            (clusters, failed_locations), where 'clusters' is list of
            :class:`google.cloud.bigtable.instance.Cluster`, and
            'failed_locations' is a list of strings representing
            locations which could not be resolved.
        """
        resp = (self.instance_admin_client.list_clusters(
            self.instance_admin_client.instance_path(self.project, '-')))
        clusters = []
        instances = {}
        for cluster in resp.clusters:
            match_cluster_name = _CLUSTER_NAME_RE.match(cluster.name)
            instance_id = match_cluster_name.group('instance')
            if instance_id not in instances:
                instances[instance_id] = self.instance(instance_id)
            clusters.append(Cluster.from_pb(cluster, instances[instance_id]))
        return clusters, resp.failed_locations
예제 #13
0
    def list_clusters(self):
        """List the clusters in the project.

        :rtype: tuple
        :returns:
            (clusters, failed_locations), where 'clusters' is list of
            :class:`google.cloud.bigtable.instance.Cluster`, and
            'failed_locations' is a list of strings representing
            locations which could not be resolved.
        """
        resp = (self.instance_admin_client.list_clusters(
            self.instance_admin_client.instance_path(self.project, '-')))
        clusters = []
        instances = {}
        for cluster in resp.clusters:
            match_cluster_name = _CLUSTER_NAME_RE.match(cluster.name)
            instance_id = match_cluster_name.group('instance')
            if instance_id not in instances:
                instances[instance_id] = self.instance(instance_id)
            clusters.append(Cluster.from_pb(cluster, instances[instance_id]))
        return clusters, resp.failed_locations
예제 #14
0
    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)
예제 #15
0
    def cluster(
        self,
        cluster_id,
        location_id=None,
        serve_nodes=None,
        default_storage_type=None,
        kms_key_name=None,
    ):
        """Factory to create a cluster associated with this instance.

        For example:

        .. literalinclude:: snippets.py
            :start-after: [START bigtable_api_create_cluster]
            :end-before: [END bigtable_api_create_cluster]
            :dedent: 4

        :type cluster_id: str
        :param cluster_id: The ID of the cluster.

        :type location_id: str
        :param location_id: (Creation Only) The location where this cluster's
                            nodes and storage reside. For best performance,
                            clients should be located as close as possible to
                            this cluster.
                            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 cluster.

        :type default_storage_type: int
        :param default_storage_type: (Optional) The type of storage
                                     Possible values are represented by the
                                     following constants:
                                     :data:`google.cloud.bigtable.enums.StorageType.SSD`.
                                     :data:`google.cloud.bigtable.enums.StorageType.HDD`,
                                     Defaults to
                                     :data:`google.cloud.bigtable.enums.StorageType.UNSPECIFIED`.

        :rtype: :class:`~google.cloud.bigtable.instance.Cluster`
        :returns: a cluster owned by this instance.

        :type kms_key_name: str
        :param kms_key_name: (Optional, Creation Only) The name of the KMS customer
                             managed encryption key (CMEK) to use for at-rest encryption
                             of data in this cluster.  If omitted, Google's default
                             encryption will be used. If specified, the requirements for
                             this key are:

                             1) The Cloud Bigtable service account associated with the
                                project that contains the cluster must be granted the
                                ``cloudkms.cryptoKeyEncrypterDecrypter`` role on the
                                CMEK.
                             2) Only regional keys can be used and the region of the
                                CMEK key must match the region of the cluster.
                             3) All clusters within an instance must use the same CMEK
                                key.
        """
        return Cluster(
            cluster_id,
            self,
            location_id=location_id,
            serve_nodes=serve_nodes,
            default_storage_type=default_storage_type,
            kms_key_name=kms_key_name,
        )
예제 #16
0
def test_instance_create_w_clusters():
    from google.cloud.bigtable import enums
    from google.cloud.bigtable.cluster import Cluster
    from google.cloud.bigtable_admin_v2.types import Cluster as cluster_pb
    from google.cloud.bigtable_admin_v2.types import Instance as instance_pb

    credentials = _make_credentials()
    client = _make_client(project=PROJECT, credentials=credentials, admin=True)
    instance = _make_instance(
        INSTANCE_ID,
        client,
        DISPLAY_NAME,
        enums.Instance.Type.PRODUCTION,
        LABELS,
    )
    api, response = _instance_api_response_for_create()
    client._instance_admin_client = api
    api.common_project_path.return_value = "projects/project"
    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)

    assert result is response

    cluster_pb_1 = cluster_pb(
        location=api.location_path(PROJECT, location_id_1),
        serve_nodes=serve_nodes_1,
        default_storage_type=enums.StorageType.UNSPECIFIED,
    )
    cluster_pb_2 = cluster_pb(
        location=api.location_path(PROJECT, location_id_2),
        serve_nodes=serve_nodes_2,
        default_storage_type=enums.StorageType.UNSPECIFIED,
    )
    instance_pb = instance_pb(
        display_name=DISPLAY_NAME,
        type_=enums.Instance.Type.PRODUCTION,
        labels=LABELS,
    )
    api.create_instance.assert_called_once_with(
        request={
            "parent": api.project_path(PROJECT),
            "instance_id": INSTANCE_ID,
            "instance": instance_pb,
            "clusters": {
                cluster_id_1: cluster_pb_1,
                cluster_id_2: cluster_pb_2
            },
        })
예제 #17
0
def _make_cluster(*args, **kwargs):
    from google.cloud.bigtable.cluster import Cluster

    return Cluster(*args, **kwargs)
예제 #18
0
    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)
예제 #19
0
    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})