Пример #1
0
    def list_instances(self):
        """List instances owned by the project.

        :rtype: tuple
        :returns: A pair of results, the first is a list of
                  :class:`~google.cloud.bigtable.instance.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
Пример #2
0
    def test_list_instances(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_pb2)
        from unit_tests._testing import _FakeStub

        LOCATION = 'projects/' + self.PROJECT + '/locations/locname'
        FAILED_LOCATION = 'FAILED'
        INSTANCE_ID1 = 'instance-id1'
        INSTANCE_ID2 = 'instance-id2'
        INSTANCE_NAME1 = ('projects/' + self.PROJECT + '/instances/' +
                          INSTANCE_ID1)
        INSTANCE_NAME2 = ('projects/' + self.PROJECT + '/instances/' +
                          INSTANCE_ID2)

        credentials = _Credentials()
        client = self._make_oneWithMocks(
            project=self.PROJECT,
            credentials=credentials,
            admin=True,
        )

        # Create request_pb
        request_pb = messages_v2_pb2.ListInstancesRequest(parent='projects/' +
                                                          self.PROJECT, )

        # Create response_pb
        response_pb = messages_v2_pb2.ListInstancesResponse(
            failed_locations=[
                FAILED_LOCATION,
            ],
            instances=[
                data_v2_pb2.Instance(
                    name=INSTANCE_NAME1,
                    display_name=INSTANCE_NAME1,
                ),
                data_v2_pb2.Instance(
                    name=INSTANCE_NAME2,
                    display_name=INSTANCE_NAME2,
                ),
            ],
        )

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

        # Create expected_result.
        failed_locations = [FAILED_LOCATION]
        instances = [
            client.instance(INSTANCE_ID1, LOCATION),
            client.instance(INSTANCE_ID2, LOCATION),
        ]
        expected_result = (instances, failed_locations)

        # Perform the method and check the result.
        result = client.list_instances()
        self.assertEqual(result, expected_result)
        self.assertEqual(stub.method_calls, [(
            'ListInstances',
            (request_pb, ),
            {},
        )])