= 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,java]
--------------------------------------------------------
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:

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



// component-configure options: START

// component-configure options: END

// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
// component options: END

// endpoint options: START

// endpoint options: END


== Usage

// component headers: START
include::partial$component-endpoint-headers.adoc[]
// component headers: END

=== 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("direct:start")
    .to("google-functions://myCamelFunction?client=#myClient");
--------------------------------------------------------------------------------

=== 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[]
