def test_list_jobs(self): # Setup Expected Response next_page_token = '' jobs_element = {} jobs = [jobs_element] expected_response = {'next_page_token': next_page_token, 'jobs': jobs} expected_response = jobs_pb2.ListJobsResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dataproc_v1beta2.JobControllerClient(channel=channel) # Setup Request project_id = 'projectId-1969970175' region = 'region-934795532' paged_list_response = client.list_jobs(project_id, region) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.jobs[0] == resources[0] assert len(channel.requests) == 1 expected_request = jobs_pb2.ListJobsRequest(project_id=project_id, region=region) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_jobs(self): # Setup Expected Response next_page_token = "" jobs_element = {} jobs = [jobs_element] expected_response = {"next_page_token": next_page_token, "jobs": jobs} expected_response = jobs_pb2.ListJobsResponse(**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.JobControllerClient() # Setup Request project_id = "projectId-1969970175" region = "region-934795532" paged_list_response = client.list_jobs(project_id, region) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.jobs[0] == resources[0] assert len(channel.requests) == 1 expected_request = jobs_pb2.ListJobsRequest( project_id=project_id, region=region ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def list_jobs( self, project_id, region, page_size=None, cluster_name=None, job_state_matcher=None, filter_=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Lists regions/{region}/jobs in a project. Example: >>> from google.cloud import dataproc_v1beta2 >>> >>> client = dataproc_v1beta2.JobControllerClient() >>> >>> # TODO: Initialize `project_id`: >>> project_id = '' >>> >>> # TODO: Initialize `region`: >>> region = '' >>> >>> # Iterate over all results >>> for element in client.list_jobs(project_id, region): ... # process element ... pass >>> >>> >>> # Alternatively: >>> >>> # Iterate over results one page at a time >>> for page in client.list_jobs(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 job belongs to. region (str): Required. The Cloud Dataproc region in which to handle the request. 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. cluster_name (str): Optional. If set, the returned jobs list includes only jobs that were submitted to the named cluster. job_state_matcher (~google.cloud.dataproc_v1beta2.types.JobStateMatcher): Optional. Specifies enumerated categories of jobs to list. (default = match ALL jobs). If ``filter`` is provided, ``jobStateMatcher`` will be ignored. filter_ (str): Optional. A filter constraining the jobs to list. Filters are case-sensitive and have the following syntax: [field = value] AND [field [= value]] ... where **field** is ``status.state`` or ``labels.[KEY]``, and ``[KEY]`` is a label key. **value** can be ``*`` to match all values. ``status.state`` can be either ``ACTIVE`` or ``NON_ACTIVE``. Only the logical ``AND`` operator is supported; space-separated items are treated as having an implicit ``AND`` operator. Example filter: status.state = ACTIVE AND labels.env = staging AND labels.starred = \* retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. 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.gax.PageIterator` instance. By default, this is an iterable of :class:`~google.cloud.dataproc_v1beta2.types.Job` instances. This object can also be configured to iterate over the pages of the response through the `options` parameter. 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_jobs" not in self._inner_api_calls: self._inner_api_calls[ "list_jobs"] = google.api_core.gapic_v1.method.wrap_method( self.transport.list_jobs, default_retry=self._method_configs["ListJobs"].retry, default_timeout=self._method_configs["ListJobs"].timeout, client_info=self._client_info, ) request = jobs_pb2.ListJobsRequest( project_id=project_id, region=region, page_size=page_size, cluster_name=cluster_name, job_state_matcher=job_state_matcher, filter=filter_, ) iterator = google.api_core.page_iterator.GRPCIterator( client=None, method=functools.partial( self._inner_api_calls["list_jobs"], retry=retry, timeout=timeout, metadata=metadata, ), request=request, items_field="jobs", request_token_field="page_token", response_token_field="next_page_token", ) return iterator