适用于 Cloud Functions 的连接器

定义用于在工作流内访问 Cloud Functions 的内置函数的 Workflows 连接器。

包含此代码示例的文档页面

如需查看上下文中使用的代码示例,请参阅以下文档:

代码示例

YAML

# This workflow demonstrates how to use the Cloud Functions connector.
# This workflow creates a function from a zip archive containing the source code stored in a Cloud Storage bucket:
# gs://your-bucket/cloud-function-source.zip.
# Make sure cloud-function-source.zip contains properly structured Cloud Functions source code and the function returns
# "success" when triggered.
# Expected successful output: "SUCCESS"

main:
  params: []
  steps:
  - init:
      assign:
      - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
      - location: "us-central1"
      - name: "dummy-function"
      - sourceArchiveUrl: "gs://your-bucket/cloud-function-source.zip"
      - service_account: "[Replace with App Engine service account]"  # Use App Engine default SA.
  - create_function:
      call: googleapis.cloudfunctions.v1.projects.locations.functions.create
      args:
        location: ${"projects/" + project + "/locations/" + location}
        body:
          name: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
          description: "cloud function tested for connector e2e testing, creation/deletion"
          entryPoint: "helloWorld"
          runtime: "nodejs10"
          serviceAccountEmail: ${service_account}
          sourceArchiveUrl: ${sourceArchiveUrl}
          httpsTrigger:
            securityLevel: "SECURE_OPTIONAL"
  - 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
  - verify_response:
      call: assert_response
      args:
        expected_response: "success"
        got_response: ${resp.body}
  - delete_function:
      call: googleapis.cloudfunctions.v1.projects.locations.functions.delete
      args:
        name: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
  - the_end:
      return: "SUCCESS"

assert_response:
  params: [expected_response, got_response]
  steps:
  - compare:
      switch:
      - condition: ${expected_response == got_response}
        next: end
  - fail:
      raise: ${"Expected response is " + expected_response + ". Got " + got_response + " instead."}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器