[[google-functions-component]]
= Google Cloud Functions Component
:docTitle: Google Cloud Functions
:shortname: google-functions
:artifactId: camel-google-functions
:description: Manage and invoke Google Cloud Functions
:since: 3.9
:supportLevel: Stable
:component-header: Only producer is supported
//Manually maintained attributes
:group: Google
:camel-spring-boot-name: google-functions

*Since Camel {since}*

*{component-header}*

The Google Functions component provides access to https://cloud.google.com/functions/[Google Cloud Functions] via
the https://github.com/googleapis/java-functions[Google Cloud Functions Client for Java].

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

[source,xml]
------------------------------------------------------
<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>

------------------------------------------------------

[[GoogleFunctions-AuthenticationConfiguration]]

== Authentication Configuration

Google Functions component authentication is targeted for use with the GCP Service Accounts.
For more information please refer to https://github.com/googleapis/google-cloud-java#authentication[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:
[source,text]
--------------------------------------------------------
String endpoint = "google-functions://myCamelFunction?serviceAccountKey=/home/user/Downloads/my-key.json";
--------------------------------------------------------

Or by setting the environment variable `GOOGLE_APPLICATION_CREDENTIALS` :

[source,text]
--------------------------------------------------------
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
--------------------------------------------------------


== URI Format

[source,text]
--------------------------------------------------------
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:

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


== URI Options

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



[width="100%",cols="2,5,^1,2",options="header"]
|===
| 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
|===
// component options: END

// endpoint options: START
The Google Cloud Functions endpoint is configured using URI syntax:

----
google-functions:functionName
----

with the following path and query parameters:

=== Path Parameters (1 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *functionName* | *Required* The user-defined name of the function |  | String
|===


=== Query Parameters (7 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| 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
|===
// endpoint options: END


== Usage

=== Message headers evaluated by the Google Functions Producer

[width="100%",cols="10%,10%,80%",options="header",]
|=======================================================================
|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

[width="100%",cols="10%,10%,80%",options="header",]
|=======================================================================
|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:

[source,java]
--------------------------------------------------------------------------------
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

[source,java]
--------------------------------------------------------------------------------

//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

[source,java]
--------------------------------------------------------------------------------
//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

[source,java]
--------------------------------------------------------------------------------
//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.

[source,java]
--------------------------------------------------------------------------------
//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.

[source,java]
--------------------------------------------------------------------------------
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.

[source,java]
--------------------------------------------------------------------------------

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.

[source,java]
--------------------------------------------------------------------------------
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.

[source,java]
--------------------------------------------------------------------------------
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");
--------------------------------------------------------------------------------


include::spring-boot:partial$starter.adoc[]
