Search in sources :

Example 16 with FunctionConfig

use of org.apache.pulsar.functions.proto.Function.FunctionConfig in project incubator-pulsar by apache.

the class FunctionsImpl method updateFunction.

@PUT
@Path("/{tenant}/{namespace}/{functionName}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response updateFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName, @FormDataParam("data") final InputStream uploadedInputStream, @FormDataParam("data") final FormDataContentDisposition fileDetail, @FormDataParam("functionConfig") final String functionConfigJson) {
    FunctionConfig functionConfig;
    // validate parameters
    try {
        functionConfig = validateUpdateRequestParams(tenant, namespace, functionName, uploadedInputStream, fileDetail, functionConfigJson);
    } catch (IllegalArgumentException e) {
        log.error("Invalid update function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    // function state
    FunctionMetaData.Builder functionMetaDataBuilder = FunctionMetaData.newBuilder().setFunctionConfig(functionConfig).setCreateTime(System.currentTimeMillis()).setVersion(0);
    PackageLocationMetaData.Builder packageLocationMetaDataBuilder = PackageLocationMetaData.newBuilder().setPackagePath(String.format("%s/%s/%s/%s", tenant, namespace, functionName, Utils.getUniquePackageName(fileDetail.getFileName())));
    functionMetaDataBuilder.setPackageLocation(packageLocationMetaDataBuilder);
    return updateRequest(functionMetaDataBuilder.build(), uploadedInputStream);
}
Also used : FunctionConfig(org.apache.pulsar.functions.proto.Function.FunctionConfig) FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) PackageLocationMetaData(org.apache.pulsar.functions.proto.Function.PackageLocationMetaData) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 17 with FunctionConfig

use of org.apache.pulsar.functions.proto.Function.FunctionConfig in project incubator-pulsar by apache.

the class FunctionsImpl method registerFunction.

@POST
@Path("/{tenant}/{namespace}/{functionName}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response registerFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName, @FormDataParam("data") final InputStream uploadedInputStream, @FormDataParam("data") final FormDataContentDisposition fileDetail, @FormDataParam("functionConfig") final String functionConfigJson) {
    FunctionConfig functionConfig;
    // validate parameters
    try {
        functionConfig = validateUpdateRequestParams(tenant, namespace, functionName, uploadedInputStream, fileDetail, functionConfigJson);
    } catch (IllegalArgumentException e) {
        log.error("Invalid register function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.error("Function {}/{}/{} already exists", tenant, namespace, functionName);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s already exists", functionName))).build();
    }
    // function state
    FunctionMetaData.Builder functionMetaDataBuilder = FunctionMetaData.newBuilder().setFunctionConfig(functionConfig).setCreateTime(System.currentTimeMillis()).setVersion(0);
    PackageLocationMetaData.Builder packageLocationMetaDataBuilder = PackageLocationMetaData.newBuilder().setPackagePath(String.format("%s/%s/%s/%s", tenant, namespace, functionName, Utils.getUniquePackageName(fileDetail.getFileName())));
    functionMetaDataBuilder.setPackageLocation(packageLocationMetaDataBuilder);
    return updateRequest(functionMetaDataBuilder.build(), uploadedInputStream);
}
Also used : FunctionConfig(org.apache.pulsar.functions.proto.Function.FunctionConfig) FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) PackageLocationMetaData(org.apache.pulsar.functions.proto.Function.PackageLocationMetaData) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

FunctionConfig (org.apache.pulsar.functions.proto.Function.FunctionConfig)17 Test (org.testng.annotations.Test)9 Message (org.apache.pulsar.client.api.Message)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 ExecutorService (java.util.concurrent.ExecutorService)7 Cleanup (lombok.Cleanup)7 MessageId (org.apache.pulsar.client.api.MessageId)7 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)7 Matchers.anyString (org.mockito.Matchers.anyString)7 ErrorData (org.apache.pulsar.common.policies.data.ErrorData)5 Response (javax.ws.rs.core.Response)4 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)4 ExecutionException (java.util.concurrent.ExecutionException)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)3 IOException (java.io.IOException)2 POST (javax.ws.rs.POST)2 PackageLocationMetaData (org.apache.pulsar.functions.proto.Function.PackageLocationMetaData)2 Gson (com.google.gson.Gson)1