Google Cloud Functions

Since Camel 3.9

Only producer is supported

The Google Functions component provides access to Google Cloud Functions via the Google Cloud Functions Client for Java.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-google-functions</artifactId>
    <!-- use the same version as your Camel core version -->
    <version>x.x.x</version>
</dependency>

Authentication Configuration

Google Functions component authentication is targeted for use with the GCP Service Accounts. For more information please refer to Google Cloud Authentication.

When you have the service account key you can provide authentication credentials to your application code. Google security credentials can be set through the component endpoint:

String endpoint = "google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json";

Or by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS :

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"

URI Format

google-functions://functionName[?options]

You can append query options to the URI in the following format, ?options=value&option2=value&…​

For example in order to call the function myCamelFunction from the project myProject and location us-central1, use the following snippet:

from("google-functions://myCamelFunction?project=myProject&location=us-central1&operation=callFunction&serviceAccountKey=/home/user/Downloads/my-key.json")
  .to("direct:test");

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.

Configuring Endpoint Options

Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL as a type safe way of configuring endpoints.

A good practice when configuring options is to use Property Placeholders, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.

The following two sections lists all the options, firstly for the component followed by the endpoint.

Component Options

The Google Cloud Functions component supports 2 options, which are listed below.

Name Description Default Type

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The Google Cloud Functions endpoint is configured using URI syntax:

google-functions:functionName

with the following path and query parameters:

Path Parameters (1 parameters)

Name Description Default Type

functionName (common)

Required The user-defined name of the function.

String

Query Parameters (7 parameters)

Name Description Default Type

serviceAccountKey (common)

Service account key to authenticate an application as a service account.

String

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

location (producer)

The Google Cloud Location (Region) where the Function is located.

String

operation (producer)

The operation to perform on the producer.

Enum values:

  • listFunctions

  • getFunction

  • callFunction

  • generateDownloadUrl

  • generateUploadUrl

  • createFunction

  • updateFunction

  • deleteFunction

GoogleCloudFunctionsOperations

pojoRequest (producer)

Specifies if the request is a pojo request.

false

boolean

project (producer)

The Google Cloud Project name where the Function is located.

String

client (advanced)

Autowired The client to use during service invocation.

CloudFunctionsServiceClient

Usage

Message headers evaluated by the Google Functions Producer

Header Type Description

GoogleCloudFunctionsOperation

String

The operation to perform. Permitted values are listFunctions, getFunction, callFunction, generateDownloadUrl, generateUploadUrl, createFunction, updateFunction, deleteFunction

GoogleCloudFunctionsEntryPoint

String

The name of the function (as defined in source code) that will be executed. Used for createFunction operation.

GoogleCloudFunctionsRuntime

String

The runtime in which to run the function. Possible values are nodejs10, nodejs12, nodejs14, python37, python38, python39, go111, go113, java11, dotnet3, ruby26, nodejs6, nodejs8. Used for createFunction operation.

GoogleCloudFunctionsSourceArchiveUrl

String

The Google Cloud Storage URL, starting with gs://, pointing to the zip archive which contains the function. Used for createFunction operation.

Message headers set by the Google Functions Producer

Header Type Description

GoogleCloudFunctionsResponseObject

Object

The response object resulting from the Google Functions Client invocation

Google Functions Producer operations

Google Functions component provides the following operation on the producer side:

  • listFunctions

  • getFunction

  • callFunction

  • generateDownloadUrl

  • generateUploadUrl

  • createFunction

  • updateFunction

  • deleteFunction

If you don’t specify an operation by default the producer will use the callFunction operation.

Advanced component configuration

If you need to have more control over the client instance configuration, you can create your own instance and refer to it in your Camel google-functions component configuration:

from("google-functions://myCamelFunction?client=#myClient")
    .to("mock:result");

Google Functions Producer Operation examples

  • ListFunctions: This operation invoke the Google Functions client and get the list of cloud Functions

//list functions
from("direct:start")
    .to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=listFunctions")
    .log("body:${body}")

This operation will get the list of cloud functions for the project myProject and location us-central1.

  • GetFunction: this operation get the Cloud Functions object

//get function
from("direct:start")
    .to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=getFunction")
    .log("body:${body}")
    .to("mock:result");

This operation will get the CloudFunction object for the project myProject, location us-central1 and functionName myCamelFunction.

  • CallFunction: this operation call the function using an HTTP request

//call function
from("direct:start")
    .process(exchange -> {
      exchange.getIn().setBody("just a message");
    })
    .to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=callFunction")
    .log("body:${body}")
    .to("mock:result");
  • GenerateDownloadUrl: this operation generate the signed URL for downloading deployed function source code.

//generate download url
from("direct:start").to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=generateDownloadUrl")
.log("body:${body}")
.to("mock:result");
  • GenerateUploadUrl: this operation generate a signed URL for uploading a function source code.

from("direct:start").to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=generateUploadUrl")
    .log("body:${body}")
    .to("mock:result");
  • createFunction: this operation creates a new function.

from("direct:start")
    .process(exchange -> {
      exchange.getIn().setHeader(GoogleCloudFunctionsConstants.ENTRY_POINT, "com.example.Example");
      exchange.getIn().setHeader(GoogleCloudFunctionsConstants.RUNTIME, "java11");
      exchange.getIn().setHeader(GoogleCloudFunctionsConstants.SOURCE_ARCHIVE_URL, "gs://myBucket/source.zip");
    }).to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=createFunction")
    .log("body:${body}")
    .to("mock:result");
  • updateFunction: this operation updates existing function.

from("direct:start")
.process(exchange -> {
  UpdateFunctionRequest request = UpdateFunctionRequest.newBuilder()
    .setFunction(CloudFunction.newBuilder().build())
    .setUpdateMask(FieldMask.newBuilder().build()).build();
  exchange.getIn().setBody(request);
}).to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=updateFunction&pojoRequest=true")
.log("body:${body}")
.to("mock:result");
  • deleteFunction: this operation Deletes a function with the given name from the specified project.

from("direct:start")
.to("google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json&project=myProject&location=us-central1&operation=deleteFunction")
.log("body:${body}")
.to("mock:result");

Spring Boot Auto-Configuration

When using google-functions with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-google-functions-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

The component supports 3 options, which are listed below.

Name Description Default Type

camel.component.google-functions.autowired-enabled

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

Boolean

camel.component.google-functions.enabled

Whether to enable auto configuration of the google-functions component. This is enabled by default.

Boolean

camel.component.google-functions.lazy-start-producer

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

Boolean