Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Google Cloud function with arguments

I have a function which fetch sql file from Cloud storage. This function accept project_id,bucket_id & sql_file

from google.cloud import storage    
def read_sql(request):
    request_json = request.get_json(silent=True)
    project_id=request_json['project_id']
    bucket_id=request_json['bucket_id']
    sql_file=request_json['sql_file']    
    gcs_client = storage.Client(project_id)
    bucket = gcs_client.get_bucket(bucket_id)
    blob = bucket.get_blob(sql_file)
    contents = blob.download_as_string()
    return contents.decode('utf-8')

It works fine when I test it with these parameters

{"project_id":"my_project","bucket_id":"my_bucket","sql_file":"daily_load.sql"}

I am trying to call this function in Google WorkFlow but don't know how to setup arguments in call http.get

main:
  params: []
  steps:
  - init:
      assign:
      - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
      - location: "us-central1"
      - name: "workflow_test_return_sql1"
      - service_account: "[email protected]"  # Use App Engine default SA.
    - get_function:
      call: googleapis.cloudfunctions.v1.projects.locations.functions.get
      args:
        name: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
      result: function
  - grant_permission_to_all:
      call: googleapis.cloudfunctions.v1.projects.locations.functions.setIamPolicy
      args:
        resource: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
        body:
          policy:
            bindings:
            - members: ["allUsers"]
              role: "roles/cloudfunctions.invoker"
  - call_function:
      call: http.get
      args:
        url: ${function.httpsTrigger.url}
      result: resp

Code written in Workflow is yaml

Any idea how to build function call URL with arguments ?

I have tried below two approached but they didn't work

- call_function:
      call: http.post
      args: 
        url: ${function.httpsTrigger.url}
        body:
            input: {"project_id":"my_project","bucket_id":"my_bucket","sql_file":"daily_load.sql"}
      result: resp

and

- call_function:
      call: http.get
      args: 
        url: ${function.httpsTrigger.url}?project_id=my_project?bucket_id=my_bucket?sql_file=daily_load.sql
like image 771
Dinesh Avatar asked Mar 20 '26 13:03

Dinesh


1 Answers

You are close!! Try that

- call_function:
      call: http.get
      args: 
        url: ${function.httpsTrigger.url + "?project_id=" + my_project + "&bucket_id=" + my_bucket + "&sql_file=" + daily_load.sql}

EDIT 1

Here the post option with JSON body (note that YAML and JSON are similar. here how to write your JSON with yaml)

- call_function:
      call: http.post
      args: 
        url: ${function.httpsTrigger.url}
        body:
          project_id: "my_project"
          bucket_id: "my_bucket"
          sql_file: "daily_load.sql"
      result: resp
like image 153
guillaume blaquiere Avatar answered Mar 23 '26 11:03

guillaume blaquiere