Search code examples
google-cloud-platformgoogle-cloud-scheduler

How to list Google Scheduler Jobs from all locations within a project with Python


I want to list all the Google Cloud Schedule jobs within a project, but to use the ListJobsRequest() class the parent parameter is required: projects/PROJECT_ID/locations/LOCATION_ID. Since I have jobs in different locations I would like to list all jobs, is it possible to do?

I already tried projects/PROJECT_ID/locations/* and projects/PROJECT_ID/locations.

In both cases I got the following error:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\main.py", line 97, in <module>
    list_scheduler_jobs()
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\main.py", line 51, in list_scheduler_jobs
    page_result = client.list_jobs(request=request)
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\venv\lib\site-packages\google\cloud\scheduler_v1\services\cloud_scheduler\client.py", line 548, in list_jobs
    response = rpc(
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\venv\lib\site-packages\google\api_core\gapic_v1\method.py", line 154, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\venv\lib\site-packages\google\api_core\retry.py", line 283, in retry_wrapped_func
    return retry_target(
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\venv\lib\site-packages\google\api_core\retry.py", line 190, in retry_target
    return target()
  File "C:\Users\user\Desktop\TI\GCP\bq-to-scheduler\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 52, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.PermissionDenied: 403 The principal (user or service account) lacks IAM permission "cloudscheduler.jobs.list" for the resource "projects/projeto1-358102/locations/*" (or the resource may not exist).

Help?


Solution

  • You can list all your available locations in your project using the code below. I also included calling list_jobs() to send a request to list the available jobs on the location.

    I got the code on listing location in this document but I edited the authentication to use google.auth library instead of oauth2client.client since this is already deprecated.

    from googleapiclient import discovery
    import google.auth
    from google.cloud import scheduler_v1
    
    def get_project_locations():
        credentials, project = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
    
        service = discovery.build('cloudscheduler', 'v1', credentials=credentials)
    
        name = f'projects/{project}'
        loc_arr = []
        request = service.projects().locations().list(name=name)
        while True:
            response = request.execute()
    
            for location in response.get('locations', []):
                loc_arr.append(location['labels']['cloud.googleapis.com/region'])
    
            request = service.projects().locations().list_next(previous_request=request, previous_response=response)
            if request is None:
                break
    
        return project,loc_arr
    
    def list_jobs(project_id,location_list):
        client = scheduler_v1.CloudSchedulerClient()
    
        for location in location_list:
            request = scheduler_v1.ListJobsRequest(parent = f"projects/{project_id}/locations/{location}")
    
            page_result = client.list_jobs(request=request)
            for response in page_result:
                print(response.name)
                print(response.http_target)
                print(response.schedule)
    
    project_id,location_list = get_project_locations()
    list_jobs(project_id,location_list)
    

    Output:

    enter image description here

    From GCP scheduler:

    enter image description here