= Azure Functions Component
:doctitle: Azure Functions
:shortname: azure-functions
:artifactid: camel-azure-functions
:description: Invoke and manage Azure Functions.
:since: 4.19
:supportlevel: Preview
:tabs-sync-option:
:component-header: Only producer is supported
//Manually maintained attributes
:group: Azure
:camel-spring-boot-name: azure-functions

*Since Camel {since}*

*{component-header}*

The azure-functions component enables integration with https://docs.microsoft.com/en-us/azure/azure-functions/[Azure Functions].
Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure.

This component supports:

- Invoking HTTP-triggered Azure Functions
- Managing Azure Function Apps (list, create, delete, start, stop, restart)
- Managing function app configuration and tags

Prerequisites

You must have a valid Azure subscription. More information is available at
https://docs.microsoft.com/azure/[Azure Documentation Portal].

[source,xml]
------------------------------------------------------------
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-azure-functions</artifactId>
    <version>${camel-version}</version>
</dependency>
------------------------------------------------------------

where `$\{camel-version}` must be replaced by the actual version of Camel.


// component-configure options: START
// component-configure options: END

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

== URI Format

[source]
----
azure-functions:functionApp[/functionName]
----

Where:

- `functionApp` is the name of your Azure Function App
- `functionName` is the name of the function within the app (required for `invokeFunction` operation)

== Usage

=== Authentication Information

There are three different Credential Types: `AZURE_IDENTITY`, `FUNCTION_KEY`, and `TOKEN_CREDENTIAL`.

*AZURE_IDENTITY* (default):

This will use `com.azure.identity.DefaultAzureCredentialBuilder().build()` instance.
This follows the Default Azure Credential Chain which tries multiple authentication methods:

- Environment variables
- Managed Identity
- Azure CLI
- Visual Studio Code
- IntelliJ IDEA

See the documentation https://docs.microsoft.com/en-us/azure/active-directory/authentication/overview-authentication[here about Azure AD authentication].

*FUNCTION_KEY*:

For invoking HTTP-triggered functions, you can use function keys or host keys:

[source,java]
----
from("direct:invoke")
    .to("azure-functions:myFunctionApp/myFunction?credentialType=FUNCTION_KEY&functionKey=your-function-key");
----

*TOKEN_CREDENTIAL*:

Provide an implementation of `com.azure.core.credential.TokenCredential` into the Camel Registry:

[source,java]
----
from("direct:invoke")
    .to("azure-functions:myFunctionApp/myFunction?credentialType=TOKEN_CREDENTIAL&tokenCredential=#myCredential");
----

=== Service Principal Authentication

For service principal authentication, provide the client credentials:

[source,java]
----
from("direct:manage")
    .to("azure-functions:myFunctionApp?operation=listFunctionApps"
        + "&subscriptionId=your-subscription-id"
        + "&resourceGroup=your-resource-group"
        + "&clientId=your-client-id"
        + "&clientSecret=your-client-secret"
        + "&tenantId=your-tenant-id");
----

== Operations

The component supports the following operations:

=== Function Invocation

[width="100%",cols="2,5",options="header"]
|===
| Operation | Description
| `invokeFunction` | Invokes an HTTP-triggered Azure Function (default operation)
|===

=== Function App Management

[width="100%",cols="2,5",options="header"]
|===
| Operation | Description
| `listFunctionApps` | List all function apps in the subscription or resource group
| `getFunctionApp` | Get details of a specific function app
| `createFunctionApp` | Create a new function app
| `deleteFunctionApp` | Delete a function app
| `startFunctionApp` | Start a stopped function app
| `stopFunctionApp` | Stop a running function app
| `restartFunctionApp` | Restart a function app
|===

=== Function Operations

[width="100%",cols="2,5",options="header"]
|===
| Operation | Description
| `listFunctions` | List all functions within a function app
| `getFunction` | Get details of a specific function
| `getFunctionKeys` | Get the master key for the function app
|===

=== Configuration Operations

[width="100%",cols="2,5",options="header"]
|===
| Operation | Description
| `getFunctionAppConfiguration` | Get app settings/configuration
| `updateFunctionAppConfiguration` | Update app settings
|===

=== Tag Operations

[width="100%",cols="2,5",options="header"]
|===
| Operation | Description
| `listTags` | List tags on the function app resource
| `tagResource` | Add tags to the function app
| `untagResource` | Remove tags from the function app
|===

== Examples

=== Invoke a Function

[source,java]
----
from("direct:invoke")
    .setBody(constant("{\"name\": \"World\"}"))
    .to("azure-functions:myFunctionApp/HelloFunction?functionKey=your-key")
    .log("Response: ${body}");
----

=== List Function Apps

[source,java]
----
from("direct:list")
    .to("azure-functions:myFunctionApp?operation=listFunctionApps"
        + "&subscriptionId=xxx&resourceGroup=myResourceGroup")
    .log("Function Apps: ${body}");
----

=== Stop a Function App

[source,java]
----
from("direct:stop")
    .to("azure-functions:myFunctionApp?operation=stopFunctionApp"
        + "&subscriptionId=xxx&resourceGroup=myResourceGroup")
    .log("Function App stopped");
----

=== Update Configuration

[source,java]
----
from("direct:updateConfig")
    .setHeader("CamelAzureFunctionsAppSettings", constant(Map.of("MY_SETTING", "value")))
    .to("azure-functions:myFunctionApp?operation=updateFunctionAppConfiguration"
        + "&subscriptionId=xxx&resourceGroup=myResourceGroup")
    .log("Configuration updated: ${body}");
----

=== Create a Function App

[source,java]
----
from("direct:create")
    .to("azure-functions:newFunctionApp?operation=createFunctionApp"
        + "&subscriptionId=xxx"
        + "&resourceGroup=myResourceGroup"
        + "&location=eastus"
        + "&runtime=java"
        + "&storageAccountConnectionString=DefaultEndpointsProtocol=https;...")
    .log("Created function app: ${header.CamelAzureFunctionsResourceId}");
----

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