示例#1
0
    def test_list_clusters(self):
        from gcloud.bigtable._generated import (
            instance_pb2 as instance_v2_pb2)
        from gcloud.bigtable._generated import (
            bigtable_instance_admin_pb2 as messages_v2_pb2)
        from gcloud.bigtable._testing import _FakeStub

        FAILED_LOCATION = 'FAILED'
        FAILED_LOCATIONS = [FAILED_LOCATION]
        CLUSTER_ID1 = 'cluster-id1'
        CLUSTER_ID2 = 'cluster-id2'
        SERVE_NODES = 4

        client = _Client(self.PROJECT)
        instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID)

        CLUSTER_NAME1 = (instance.name + '/clusters/' + CLUSTER_ID1)
        CLUSTER_NAME2 = (instance.name + '/clusters/' + CLUSTER_ID2)
        # Create request_pb
        request_pb = messages_v2_pb2.ListClustersRequest(
            parent=instance.name,
        )

        # Create response_pb
        response_pb = messages_v2_pb2.ListClustersResponse(
            failed_locations=[FAILED_LOCATION],
            clusters=[
                instance_v2_pb2.Cluster(
                    name=CLUSTER_NAME1,
                    serve_nodes=SERVE_NODES,
                ),
                instance_v2_pb2.Cluster(
                    name=CLUSTER_NAME2,
                    serve_nodes=SERVE_NODES,
                ),
            ],
        )

        # Patch the stub used by the API method.
        client._instance_stub = stub = _FakeStub(response_pb)

        # Create expected_result.
        clusters = [
            instance.cluster(CLUSTER_ID1),
            instance.cluster(CLUSTER_ID2),
        ]
        expected_result = (clusters, FAILED_LOCATIONS)

        # Perform the method and check the result.
        result = instance.list_clusters()
        self.assertEqual(result, expected_result)
        self.assertEqual(stub.method_calls, [(
            'ListClusters',
            (request_pb,),
            {},
        )])
示例#2
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, self._client.timeout_seconds)

        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