Skip to content

Commit

Permalink
Add example of fn2fn via service broker
Browse files Browse the repository at this point in the history
  • Loading branch information
shukawam committed Dec 14, 2023
1 parent c2bf5e3 commit 117102e
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 0 deletions.
5 changes: 5 additions & 0 deletions function-to-function/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# function-to-function

## overview

function(client) -> streaming -> connector hub -> function(executor)
11 changes: 11 additions & 0 deletions function-to-function/client/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
schema_version: 20180708
name: client
version: 0.0.1
runtime: java
build_image: fnproject/fn-java-fdk-build:jdk17-1.0.182
run_image: fnproject/fn-java-fdk:jre17-1.0.182
cmd: me.shukawam.EntryPoint::handleRequest
config:
LOCAL: false
STREAMING_OCID: ocid1.stream.oc1.ap-tokyo-1.amaaaaaassl65iqax4mlhw46vdc3ain5ackfpkn7pgtawyw6hdltg2qqpxva
STREAMING_ENDPOINT: https://cell-1.streaming.ap-tokyo-1.oci.oraclecloud.com
88 changes: 88 additions & 0 deletions function-to-function/client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<fdk.version>1.0.182</fdk.version>
<oci.sdk.version>3.31.0</oci.sdk.version>
</properties>
<groupId>me.shukawam</groupId>
<artifactId>client</artifactId>
<version>1.0.0</version>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-bom</artifactId>
<version>${oci.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>api</artifactId>
<version>${fdk.version}</version>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>testing-core</artifactId>
<version>${fdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>testing-junit4</artifactId>
<version>${fdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-core</artifactId>
<version>${oci.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-streaming</artifactId>
<version>${oci.sdk.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.shukawam;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import com.fnproject.fn.api.FnConfiguration;
import com.fnproject.fn.api.RuntimeContext;
import com.oracle.bmc.auth.AbstractAuthenticationDetailsProvider;
import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider;
import com.oracle.bmc.auth.ResourcePrincipalAuthenticationDetailsProvider;
import com.oracle.bmc.streaming.StreamClient;
import com.oracle.bmc.streaming.model.PutMessagesDetails;
import com.oracle.bmc.streaming.model.PutMessagesDetailsEntry;
import com.oracle.bmc.streaming.requests.PutMessagesRequest;
import com.oracle.bmc.streaming.responses.PutMessagesResponse;

public class EntryPoint {
private static final Logger LOGGER = Logger.getLogger(EntryPoint.class.getName());
private AbstractAuthenticationDetailsProvider provider;
private StreamClient client;
private String streamOcid;

@FnConfiguration
public void config(RuntimeContext ctx) throws IOException {
var isLocal = ctx.getConfigurationByKey("LOCAL").get();
LOGGER.info(isLocal);
if ("true".equals(isLocal)) {
provider = InstancePrincipalsAuthenticationDetailsProvider.builder().build();
} else {
provider = ResourcePrincipalAuthenticationDetailsProvider.builder().build();
}
var streamingEndpoint = ctx.getConfigurationByKey("STREAMING_ENDPOINT")
.orElse("https://cell-1.streaming.ap-tokyo-1.oci.oraclecloud.com");
streamOcid = ctx.getConfigurationByKey("STREAMING_OCID").orElse("");
LOGGER.info(streamingEndpoint);
LOGGER.info(streamOcid);
client = StreamClient.builder().endpoint(streamingEndpoint).build(provider);
}

public Response handleRequest(Request request) {
var response = putMessage(request.getMessage());
if (response.getPutMessagesResult().getEntries() != null) {
return new Response("ack");
} else {
return new Response("failed.");
}
}

public PutMessagesResponse putMessage(String value) {
List<PutMessagesDetailsEntry> messages = List.of(
PutMessagesDetailsEntry.builder().value(value.getBytes()).build());
LOGGER.info("Put message to streaming");
return client.putMessages(
PutMessagesRequest.builder().streamId(streamOcid).putMessagesDetails(
PutMessagesDetails.builder().messages(messages).build()).build());
}
}
21 changes: 21 additions & 0 deletions function-to-function/client/src/main/java/me/shukawam/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.shukawam;

public class Request {
private String message;

public Request() {
}

public Request(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.shukawam;

public class Response {
private String status;

public Response() {
}

public Response(String status) {
this.status = status;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.shukawam;

import static org.junit.Assert.assertEquals;

import org.junit.Rule;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fnproject.fn.testing.FnTestingRule;

public class EntryPointTest {

@Rule
public final FnTestingRule testing = FnTestingRule.createDefault();

@Test
public void sholdReturnAck() {
var mapper = new ObjectMapper();
try {
testing
.setConfig("LOCAL", "true")
.setConfig("STREAMING_OCID", "ocid1.stream.oc1.ap-tokyo-1.amaaaaaassl65iqax4mlhw46vdc3ain5ackfpkn7pgtawyw6hdltg2qqpxva")
.setConfig("STREAMING_ENDPOINT", "https://cell-1.streaming.ap-tokyo-1.oci.oraclecloud.com")
.givenEvent()
.withHeader("content-type", "applicatoin/json")
.withBody(mapper.writeValueAsString(new Request("Hello world")))
.enqueue();
testing.thenRun(EntryPoint.class, "handleRequest");
} catch (JsonProcessingException e) {
e.printStackTrace();
}
var result = testing.getOnlyResult();
var expected = "{\"status\":\"ack\"}";
assertEquals(expected, result.getBodyAsString());
}
}
7 changes: 7 additions & 0 deletions function-to-function/executor/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
schema_version: 20180708
name: executor
version: 0.0.1
runtime: java
build_image: fnproject/fn-java-fdk-build:jdk17-1.0.182
run_image: fnproject/fn-java-fdk:jre17-1.0.182
cmd: me.shukawam.EntryPoint::handleRequest
61 changes: 61 additions & 0 deletions function-to-function/executor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<fdk.version>1.0.182</fdk.version>
</properties>
<groupId>me.shukawam</groupId>
<artifactId>executor</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>api</artifactId>
<version>${fdk.version}</version>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>testing-core</artifactId>
<version>${fdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>testing-junit4</artifactId>
<version>${fdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.shukawam;

import java.util.List;
import java.util.logging.Logger;

public class EntryPoint {
private static final Logger LOGGER = Logger.getLogger(EntryPoint.class.getName());

public String handleRequest(List<Request> requests) {
requests.forEach(r -> {
LOGGER.info(String.format("input: %s", r));
});
return "ok";
}
}
Loading

0 comments on commit 117102e

Please sign in to comment.