Skip to content

Commit

Permalink
integrate official Azure Function templates with Functions Plugin (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
xscript authored Nov 1, 2017
1 parent c245582 commit d221bd9
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 61 deletions.
16 changes: 10 additions & 6 deletions azure-functions-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ Maven | 3.0 and above

## Goals

#### `azure-functions:add`
- Create new Java function and add to current project.
- You will be prompted to choose template and enter parameters.

#### `azure-functions:package`
- Scan the output directory (default is `${project.basedir}/target/classes`) and generating `function.json` for each function (method annotated with `FunctionName`) in the staging directory.
- Copy JAR files from the build directory (default is `${project.basedir}/target/`) to the staging directory.

>NOTE:
>Default staging directory is `${project.basedir}/target/azure-functions/${function-app-name}/`
#### `azure-functions:add`
- Create new Java function and add to current project.
- You will be prompted to choose template and enter parameters. Templates for below triggers are supported as of now:
- HTTP Trigger
- Azure Storage Blob Trigger
- Azure Storage Queue Trigger
- Timer Trigger

#### `azure-functions:run`
- Invoke Azure Functions Local Emulator to run all functions. Default working directory is the staging directory.
- Use property `-Dfunctions.target=myFunction` to run a single function named `myFunction`
Expand Down Expand Up @@ -150,8 +154,8 @@ You don't have to provide all properties on command line. Missing properties wil
### Generate `function.json` from current project

Follow below instructions, you don't need to handwrite `function.json` any more.
1. Use annotations from package `com.microsoft.azure.serverless:azure-functions-java-core` to decorate your functions.
2. Run `mvn package azure-functions:package`; then `function.json` files will be automatically generated for all functions in your project.
1. Use annotations from package `com.microsoft.azure:azure-functions-java-core` to decorate your functions.
2. Run `mvn clean package azure-functions:package`; then `function.json` files will be automatically generated for all functions in your project.

### Run Azure Functions locally

Expand Down
10 changes: 9 additions & 1 deletion azure-functions-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.build.timestamp.format>yyyyMMddHHmmssSSS</maven.build.timestamp.format>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.version>3.3.3</maven.version>
Expand Down Expand Up @@ -108,8 +109,15 @@
<invokerPropertiesFile>invoker.properties</invokerPropertiesFile>
<goals>
<goal>clean</goal>
<goal>function:deploy</goal>
<goal>package</goal>
<goal>azure-functions:deploy</goal>
</goals>
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
</properties>
<scriptVariables>
<timestamp>${maven.build.timestamp}</timestamp>
</scriptVariables>
</configuration>
<executions>
<execution>
Expand Down
30 changes: 30 additions & 0 deletions azure-functions-maven-plugin/src/it/http-trigger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Build output
target/
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IDE
.idea/
*.iml

# macOS
.DS_Store
27 changes: 27 additions & 0 deletions azure-functions-maven-plugin/src/it/http-trigger/cleanup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

// Verify Azure Functions
def url = "https://maven-functions-it-${timestamp}.azurewebsites.net/api/hello?name=Azure".toURL()
try {
url.getText() // warm up
} catch (Exception e) {
// ignore warm-up exception
}
def response = url.getText()
assert response == "Hello, Azure"

// Clean up resources created in test
def clientId = System.getenv("CLIENT_ID")
def tenantId = System.getenv("TENANT_ID")
def key = System.getenv("KEY")
def command = """
az login --service-principal -u ${clientId} -p ${key} --tenant ${tenantId}
az group delete -y -n maven-functions-it-rg-1 --no-wait
az logout
"""
def process = ["bash", "-c", command].execute()
println process.text
2 changes: 2 additions & 0 deletions azure-functions-maven-plugin/src/it/http-trigger/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": ""
}
}
110 changes: 110 additions & 0 deletions azure-functions-maven-plugin/src/it/http-trigger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.microsoft.azure</groupId>
<artifactId>azure-java-functions</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Azure Java Functions</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<functionAppName>maven-functions-it-${timestamp}</functionAppName>
<functionAppRegion>westus</functionAppRegion>
</properties>

<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-java-core</artifactId>
<version>1.0.0-beta-1</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<resourceGroup>maven-functions-it-rg-1</resourceGroup>
<appName>${functionAppName}</appName>
<region>${functionAppRegion}</region>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>beta</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>host.json</include>
<include>local.settings.json</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>

</project>
17 changes: 17 additions & 0 deletions azure-functions-maven-plugin/src/it/http-trigger/setup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

// Delete resources before test
def clientId = System.getenv("CLIENT_ID")
def tenantId = System.getenv("TENANT_ID")
def key = System.getenv("KEY")
def command = """
az login --service-principal -u ${clientId} -p ${key} --tenant ${tenantId}
az group delete -y -n maven-functions-it-rg-1
az logout
"""
def process = ["bash", "-c", command].execute()
println process.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure;

import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;

/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
@FunctionName("hello")
public HttpResponseMessage httpHandler(
@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage request,
final ExecutionContext context
) {
context.getLogger().info("Java HTTP trigger processed a HTTP request.");

// Parse query parameter
String name = request.getQueryParameters().get("name");

if (name == null) {
// Get request body
Object body = request.getBody();
if (body != null) {
name = body.toString();
}
}

if (name == null) {
return request.createResponse(400, "Please pass a name on the query string or in the request body");
} else {
return request.createResponse(200, "Hello, " + name);
}
}
}
45 changes: 45 additions & 0 deletions azure-functions-maven-plugin/src/it/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<settings>
<servers>
<server>
<id>azure-auth</id>
<configuration>
<client>${env.CLIENT_ID}</client>
<tenant>${env.TENANT_ID}</tenant>
<key>${env.KEY}</key>
<environment>AZURE</environment>
</configuration>
</server>
</servers>
<profiles>
<profile>
<id>it-repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
Loading

0 comments on commit d221bd9

Please sign in to comment.