Cleanup
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
////
|
|
||||||
DO NOT EDIT THIS FILE. IT WAS GENERATED.
|
|
||||||
Manual changes to this file will be lost when it is generated again.
|
|
||||||
Edit the files in the src/main/asciidoc/ directory instead.
|
|
||||||
////
|
|
||||||
|
|
||||||
== Quick Start
|
|
||||||
|
|
||||||
Unresolved directive in openwhisk-readme.adoc - include::./adapters/openwhisk-quick-start.adoc[]
|
|
||||||
|
|
||||||
== Examples
|
|
||||||
|
|
||||||
The following examples are built based on the details and explanations above, on how to deploy Spring Cloud Functions on to https://openwhisk.apache.org/[OpenWhisk]
|
|
||||||
|
|
||||||
* https://github.com/redhat-developer-demos/ow-scf-fruiteason[Spring Cloud Function PoF Example]. This example shows how to use Spring Cloud Functions by defining simple Plain Old Function (POF)
|
|
||||||
|
|
||||||
* https://github.com/redhat-developer-demos/ow-scf-greeter[Spring Cloud Function Application Example]. This example shows how to use Spring Cloud Functions with a complete Spring Boot Application that has functions defined by extending `java.util.function.Function` interfaces.
|
|
||||||
|
|
||||||
The base docker images used for above examples is available https://github.com/redhat-developer-demos/openwhisk-scf-docker[here].
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<artifactId>spring-cloud-function-adapter-openwhisk</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>spring-cloud-function-adapter-openwhisk</name>
|
|
||||||
<description>Apache OpenWhisk Adapter for Spring Cloud Function</description>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-function-adapter-parent</artifactId>
|
|
||||||
<version>4.0.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-function-context</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>io.projectreactor</groupId>
|
|
||||||
<artifactId>reactor-core</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot.experimental</groupId>
|
|
||||||
<artifactId>spring-boot-thin-layout</artifactId>
|
|
||||||
<version>${wrapper.version}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<name>Spring Snapshots</name>
|
|
||||||
<url>https://repo.spring.io/snapshot</url>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>true</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>spring-milestones</id>
|
|
||||||
<name>Spring Milestones</name>
|
|
||||||
<url>https://repo.spring.io/milestone</url>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>false</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
<pluginRepositories>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<name>Spring Snapshots</name>
|
|
||||||
<url>https://repo.spring.io/snapshot</url>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>true</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</pluginRepository>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>spring-milestones</id>
|
|
||||||
<name>Spring Milestones</name>
|
|
||||||
<url>https://repo.spring.io/milestone</url>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>false</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</pluginRepository>
|
|
||||||
</pluginRepositories>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
@ConfigurationProperties(prefix = "function")
|
|
||||||
public class FunctionProperties {
|
|
||||||
|
|
||||||
private String name = "function";
|
|
||||||
|
|
||||||
private String type = "function";
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getType() {
|
|
||||||
return this.type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
// @checkstyle:off
|
|
||||||
@SpringBootApplication
|
|
||||||
@EnableConfigurationProperties(FunctionProperties.class)
|
|
||||||
public class OpenWhiskActionApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(OpenWhiskActionApplication.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
// @checkstyle:on
|
|
||||||
@@ -1,122 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
* @author Kamesh Sampath
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
public class OpenWhiskActionHandler extends OpenWhiskFunctionInitializer {
|
|
||||||
|
|
||||||
private static final String NO_INPUT_PROVIDED = "No input provided";
|
|
||||||
|
|
||||||
private static Log logger = LogFactory.getLog(OpenWhiskFunctionInitializer.class);
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ObjectMapper mapper;
|
|
||||||
|
|
||||||
public OpenWhiskActionHandler() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/init")
|
|
||||||
public void init(@RequestBody OpenWhiskInitRequest request) {
|
|
||||||
initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(value = "/run", consumes = "application/json", produces = "application/json")
|
|
||||||
public Object run(@RequestBody OpenWhiskActionRequest request) {
|
|
||||||
Object input = convertEvent(request.getValue());
|
|
||||||
Object result = NO_INPUT_PROVIDED;
|
|
||||||
if (input != null) {
|
|
||||||
Publisher<?> output = apply(extract(input));
|
|
||||||
result = result(input, output);
|
|
||||||
}
|
|
||||||
return serializeBody(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object result(Object input, Publisher<?> output) {
|
|
||||||
List<Object> result = new ArrayList<>();
|
|
||||||
for (Object value : Flux.from(output).toIterable()) {
|
|
||||||
result.add(value);
|
|
||||||
}
|
|
||||||
if (isSingleValue(input) && result.size() == 1) {
|
|
||||||
return result.get(0);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isSingleValue(Object input) {
|
|
||||||
return !(input instanceof Collection);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Flux<?> extract(Object input) {
|
|
||||||
if (input instanceof Collection) {
|
|
||||||
return Flux.fromIterable((Iterable<?>) input);
|
|
||||||
}
|
|
||||||
return Flux.just(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Object convertEvent(Map<String, Object> value) {
|
|
||||||
// just expecting "payload" for now
|
|
||||||
if (logger.isDebugEnabled()) {
|
|
||||||
logger.info("Action Request Value:" + value);
|
|
||||||
}
|
|
||||||
if (value != null) {
|
|
||||||
Object payload = value.get("payload");
|
|
||||||
return convertToFunctionParamType(payload);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object convertToFunctionParamType(Object payload) {
|
|
||||||
try {
|
|
||||||
return this.mapper.convertValue(payload, getInputType());
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
throw new IllegalStateException("Cannot convert event payload", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String serializeBody(Object body) {
|
|
||||||
try {
|
|
||||||
return "{\"result\":" + this.mapper.writeValueAsString(body) + "}";
|
|
||||||
}
|
|
||||||
catch (JsonProcessingException e) {
|
|
||||||
throw new IllegalStateException("Cannot convert output", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
public class OpenWhiskActionRequest {
|
|
||||||
|
|
||||||
@JsonProperty("action_name")
|
|
||||||
private String actionName;
|
|
||||||
|
|
||||||
@JsonProperty("activation_id")
|
|
||||||
private String activationId;
|
|
||||||
|
|
||||||
@JsonProperty("api_key")
|
|
||||||
private String apiKey;
|
|
||||||
|
|
||||||
private String deadline;
|
|
||||||
|
|
||||||
private String namespace;
|
|
||||||
|
|
||||||
private Map<String, Object> value;
|
|
||||||
|
|
||||||
public String getActionName() {
|
|
||||||
return this.actionName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setActionName(String actionName) {
|
|
||||||
this.actionName = actionName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getActivationId() {
|
|
||||||
return this.activationId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setActivationId(String activationId) {
|
|
||||||
this.activationId = activationId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getApiKey() {
|
|
||||||
return this.apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setApiKey(String apiKey) {
|
|
||||||
this.apiKey = apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDeadline() {
|
|
||||||
return this.deadline;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDeadline(String deadline) {
|
|
||||||
this.deadline = deadline;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNamespace() {
|
|
||||||
return this.namespace;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNamespace(String namespace) {
|
|
||||||
this.namespace = namespace;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Object> getValue() {
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(Map<String, Object> value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
|
||||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Dave Syer
|
|
||||||
* @author Mark Fisher
|
|
||||||
* @author Kamesh Sampath
|
|
||||||
*/
|
|
||||||
public class OpenWhiskFunctionInitializer {
|
|
||||||
|
|
||||||
private static Log logger = LogFactory.getLog(OpenWhiskFunctionInitializer.class);
|
|
||||||
|
|
||||||
private Function<Publisher<?>, Publisher<?>> function;
|
|
||||||
|
|
||||||
private Consumer<Publisher<?>> consumer;
|
|
||||||
|
|
||||||
private Supplier<Publisher<?>> supplier;
|
|
||||||
|
|
||||||
private AtomicBoolean initialized = new AtomicBoolean();
|
|
||||||
|
|
||||||
@Autowired(required = false)
|
|
||||||
private FunctionInspector inspector;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FunctionCatalog catalog;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FunctionProperties properties;
|
|
||||||
|
|
||||||
protected void initialize() {
|
|
||||||
logger.info("Initializing - OpenWhisk Function Initializer");
|
|
||||||
if (!this.initialized.compareAndSet(false, true)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String name = this.properties.getName();
|
|
||||||
String type = this.properties.getType();
|
|
||||||
if ("function".equals(type)) {
|
|
||||||
this.function = this.catalog.lookup(Function.class, name);
|
|
||||||
}
|
|
||||||
else if ("consumer".equals(type)) {
|
|
||||||
this.consumer = this.catalog.lookup(Consumer.class, name);
|
|
||||||
}
|
|
||||||
else if ("supplier".equals(type)) {
|
|
||||||
this.supplier = this.catalog.lookup(Supplier.class, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Class<?> getInputType() {
|
|
||||||
if (this.inspector != null) {
|
|
||||||
return this.inspector.getInputType(function());
|
|
||||||
}
|
|
||||||
return Object.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object function() {
|
|
||||||
return this.function != null ? this.function
|
|
||||||
: (this.consumer != null ? this.consumer : this.supplier);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Publisher<?> apply(Publisher<?> input) {
|
|
||||||
if (this.function != null) {
|
|
||||||
return this.function.apply(input);
|
|
||||||
}
|
|
||||||
if (this.consumer != null) {
|
|
||||||
this.consumer.accept(input);
|
|
||||||
return Flux.empty();
|
|
||||||
}
|
|
||||||
if (this.supplier != null) {
|
|
||||||
return this.supplier.get();
|
|
||||||
}
|
|
||||||
throw new IllegalStateException("No function defined");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
public class OpenWhiskInitRequest {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private boolean binary;
|
|
||||||
|
|
||||||
private String main;
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBinary() {
|
|
||||||
return this.binary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBinary(boolean binary) {
|
|
||||||
this.binary = binary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMain() {
|
|
||||||
return this.main;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMain(String main) {
|
|
||||||
this.main = main;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.adapter.openwhisk;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.context.annotation.Scope;
|
|
||||||
import org.springframework.test.context.TestPropertySource;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Kamesh Sampath
|
|
||||||
*/
|
|
||||||
@SpringBootTest
|
|
||||||
@EnableAutoConfiguration
|
|
||||||
@TestPropertySource(locations = "classpath:/application-test.properties")
|
|
||||||
public class OpenWhiskActionHandlerTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
OpenWhiskActionHandler actionHandler;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
ObjectMapper mapper;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHandlerWithPayload() {
|
|
||||||
Map<String, String> testData = new HashMap<>();
|
|
||||||
testData.put("name", "Spring");
|
|
||||||
Map<String, Object> eventData = new HashMap<>();
|
|
||||||
eventData.put("payload", testData);
|
|
||||||
this.actionHandler.init(new OpenWhiskInitRequest());
|
|
||||||
OpenWhiskActionRequest actionRequest = new OpenWhiskActionRequest();
|
|
||||||
actionRequest.setActionName("test_action");
|
|
||||||
actionRequest.setValue(eventData);
|
|
||||||
Object result = this.actionHandler.run(actionRequest);
|
|
||||||
assertThat(result).isNotNull();
|
|
||||||
assertThat(result).isEqualTo(
|
|
||||||
"{\"result\":{\"name\":\"Spring\",\"message\":\"Hello, Spring\"}}");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHandlerWithoutPayload() {
|
|
||||||
Map<String, String> testData = new HashMap<>();
|
|
||||||
testData.put("name", "Spring");
|
|
||||||
this.actionHandler.init(new OpenWhiskInitRequest());
|
|
||||||
OpenWhiskActionRequest actionRequest = new OpenWhiskActionRequest();
|
|
||||||
actionRequest.setActionName("test_action");
|
|
||||||
Object result = this.actionHandler.run(actionRequest);
|
|
||||||
assertThat(result).isNotNull();
|
|
||||||
assertThat(result).isEqualTo("{\"result\":\"No input provided\"}");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@Import({ ContextFunctionCatalogAutoConfiguration.class,
|
|
||||||
JacksonAutoConfiguration.class })
|
|
||||||
protected static class OWFunctionConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Function<Greetings, Greetings> greeter() {
|
|
||||||
return v -> new OpenWhiskActionHandlerTest.Greetings(v.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Scope("prototype")
|
|
||||||
public OpenWhiskActionHandler actionHandler() {
|
|
||||||
return new OpenWhiskActionHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FunctionProperties properties() {
|
|
||||||
return new FunctionProperties();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class Greetings {
|
|
||||||
|
|
||||||
private final String GREETINGS_FORMAT = "Hello, %s";
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
public Greetings() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Greetings(String name) {
|
|
||||||
this.name = name;
|
|
||||||
setMessage("");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMessage() {
|
|
||||||
return this.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMessage(String message) {
|
|
||||||
this.message = String.format(this.GREETINGS_FORMAT,
|
|
||||||
this.name != null ? this.name : "nobody");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
function.name:greeter
|
|
||||||
function.type:function
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<artifactId>spring-cloud-function-task</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
<name>Spring Cloud Function Task Support</name>
|
|
||||||
<description>Spring Cloud Function Task Support</description>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-function-parent</artifactId>
|
|
||||||
<version>4.0.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-task</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-function-context</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>io.projectreactor</groupId>
|
|
||||||
<artifactId>reactor-core</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-logging</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot.experimental</groupId>
|
|
||||||
<artifactId>spring-boot-thin-layout</artifactId>
|
|
||||||
<version>${wrapper.version}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.task;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
// @checkstyle:off
|
|
||||||
@SpringBootApplication(proxyBeanMethods = false)
|
|
||||||
public class TaskApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(TaskApplication.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
// @checkstyle:on
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.task;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.CommandLineRunner;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
|
||||||
import org.springframework.cloud.task.configuration.EnableTask;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@EnableTask
|
|
||||||
@EnableConfigurationProperties(TaskConfigurationProperties.class)
|
|
||||||
@ConditionalOnClass({ EnableTask.class })
|
|
||||||
public class TaskConfiguration {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TaskConfigurationProperties properties;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public CommandLineRunner commandLineRunner(FunctionCatalog registry) {
|
|
||||||
final Supplier<Publisher<Object>> supplier = registry.lookup(Supplier.class, this.properties.getSupplier());
|
|
||||||
final Function<Publisher<Object>, Publisher<Object>> function = registry.lookup(Function.class,
|
|
||||||
this.properties.getFunction());
|
|
||||||
final Consumer<Publisher<Object>> consumer = consumer(registry);
|
|
||||||
CommandLineRunner runner = new CommandLineRunner() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(String... args) throws Exception {
|
|
||||||
consumer.accept(function.apply(supplier.get()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return runner;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Consumer<Publisher<Object>> consumer(FunctionCatalog registry) {
|
|
||||||
Consumer<Publisher<Object>> consumer = registry.lookup(Consumer.class, this.properties.getConsumer());
|
|
||||||
if (consumer != null) {
|
|
||||||
return consumer;
|
|
||||||
}
|
|
||||||
Function<Publisher<Object>, Publisher<Void>> function = registry.lookup(Function.class,
|
|
||||||
this.properties.getConsumer());
|
|
||||||
return flux -> Mono.from(function.apply(flux)).subscribe();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2019 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.cloud.function.task;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Mark Fisher
|
|
||||||
*/
|
|
||||||
@ConfigurationProperties(prefix = "spring.cloud.function.task")
|
|
||||||
public class TaskConfigurationProperties {
|
|
||||||
|
|
||||||
private String supplier;
|
|
||||||
|
|
||||||
private String function;
|
|
||||||
|
|
||||||
private String consumer;
|
|
||||||
|
|
||||||
public String getSupplier() {
|
|
||||||
return this.supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSupplier(String supplier) {
|
|
||||||
this.supplier = supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFunction() {
|
|
||||||
return this.function;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFunction(String function) {
|
|
||||||
this.function = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getConsumer() {
|
|
||||||
return this.consumer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConsumer(String consumer) {
|
|
||||||
this.consumer = consumer;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|
||||||
org.springframework.cloud.function.task.TaskConfiguration
|
|
||||||
Reference in New Issue
Block a user