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");
URI 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 |
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 |
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. There are 8 enums and the value can be one of: 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 |
---|---|---|
|
|
The operation to perform. Permitted values are listFunctions, getFunction, callFunction, generateDownloadUrl, generateUploadUrl, createFunction, updateFunction, deleteFunction |
|
|
The name of the function (as defined in source code) that will be executed. Used for createFunction operation. |
|
|
The runtime in which to run the function. Possible values are |
|
|
The Google Cloud Storage URL, starting with |
Message headers set by the Google Functions Producer
Header | Type | Description |
---|---|---|
|
|
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 |
---|---|---|---|
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 |
|
Whether to enable auto configuration of the google-functions component. This is enabled by default. |
Boolean |
||
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 |