示例#1
0
    def test_list_clusters(self):
        # Setup Expected Response
        next_page_token = ""
        clusters_element = {}
        clusters = [clusters_element]
        expected_response = {"next_page_token": next_page_token, "clusters": clusters}
        expected_response = clusters_pb2.ListClustersResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dataproc_v1beta2.ClusterControllerClient()

        # Setup Request
        project_id = "projectId-1969970175"
        region = "region-934795532"

        paged_list_response = client.list_clusters(project_id, region)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.clusters[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = clusters_pb2.ListClustersRequest(
            project_id=project_id, region=region
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_clusters(self):
        # Setup Expected Response
        next_page_token = ''
        clusters_element = {}
        clusters = [clusters_element]
        expected_response = {
            'next_page_token': next_page_token,
            'clusters': clusters
        }
        expected_response = clusters_pb2.ListClustersResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dataproc_v1beta2.ClusterControllerClient(channel=channel)

        # Setup Request
        project_id = 'projectId-1969970175'
        region = 'region-934795532'

        paged_list_response = client.list_clusters(project_id, region)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.clusters[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = clusters_pb2.ListClustersRequest(
            project_id=project_id, region=region)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def list_clusters(
        self,
        project_id,
        region,
        filter_=None,
        page_size=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Lists all regions/{region}/clusters in a project.

        Example:
            >>> from google.cloud import dataproc_v1beta2
            >>>
            >>> client = dataproc_v1beta2.ClusterControllerClient()
            >>>
            >>> # TODO: Initialize `project_id`:
            >>> project_id = ''
            >>>
            >>> # TODO: Initialize `region`:
            >>> region = ''
            >>>
            >>> # Iterate over all results
            >>> for element in client.list_clusters(project_id, region):
            ...     # process element
            ...     pass
            >>>
            >>>
            >>> # Alternatively:
            >>>
            >>> # Iterate over results one page at a time
            >>> for page in client.list_clusters(project_id, region).pages:
            ...     for element in page:
            ...         # process element
            ...         pass

        Args:
            project_id (str): Required. The ID of the Google Cloud Platform project that the cluster
                belongs to.
            region (str): Required. The Cloud Dataproc region in which to handle the request.
            filter_ (str): Optional. A filter constraining the clusters to list. Filters are
                case-sensitive and have the following syntax:

                field = value [AND [field = value]] ...

                where **field** is one of ``status.state``, ``clusterName``, or
                ``labels.[KEY]``, and ``[KEY]`` is a label key. **value** can be ``*``
                to match all values. ``status.state`` can be one of the following:
                ``ACTIVE``, ``INACTIVE``, ``CREATING``, ``RUNNING``, ``ERROR``,
                ``DELETING``, or ``UPDATING``. ``ACTIVE`` contains the ``CREATING``,
                ``UPDATING``, and ``RUNNING`` states. ``INACTIVE`` contains the
                ``DELETING`` and ``ERROR`` states. ``clusterName`` is the name of the
                cluster provided at creation time. Only the logical ``AND`` operator is
                supported; space-separated items are treated as having an implicit
                ``AND`` operator.

                Example filter:

                status.state = ACTIVE AND clusterName = mycluster AND labels.env =
                staging AND labels.starred = \*
            page_size (int): The maximum number of resources contained in the
                underlying API response. If page streaming is performed per-
                resource, this parameter does not affect the return value. If page
                streaming is performed per-page, this determines the maximum number
                of resources in a page.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will
                be retried using a default configuration.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.api_core.page_iterator.PageIterator` instance.
            An iterable of :class:`~google.cloud.dataproc_v1beta2.types.Cluster` instances.
            You can also iterate over the pages of the response
            using its `pages` property.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "list_clusters" not in self._inner_api_calls:
            self._inner_api_calls[
                "list_clusters"
            ] = google.api_core.gapic_v1.method.wrap_method(
                self.transport.list_clusters,
                default_retry=self._method_configs["ListClusters"].retry,
                default_timeout=self._method_configs["ListClusters"].timeout,
                client_info=self._client_info,
            )

        request = clusters_pb2.ListClustersRequest(
            project_id=project_id, region=region, filter=filter_, page_size=page_size
        )
        iterator = google.api_core.page_iterator.GRPCIterator(
            client=None,
            method=functools.partial(
                self._inner_api_calls["list_clusters"],
                retry=retry,
                timeout=timeout,
                metadata=metadata,
            ),
            request=request,
            items_field="clusters",
            request_token_field="page_token",
            response_token_field="next_page_token",
        )
        return iterator