Search in sources :

Example 21 with ErrorData

use of org.apache.pulsar.common.policies.data.ErrorData in project incubator-pulsar by apache.

the class FunctionApiV2ResourceTest method testListFunctionsMissingArguments.

private void testListFunctionsMissingArguments(String tenant, String namespace, String missingFieldName) {
    Response response = resource.listFunctions(tenant, namespace);
    assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
    assertEquals(new ErrorData(missingFieldName + " is not provided").reason, ((ErrorData) response.getEntity()).reason);
}
Also used : Response(javax.ws.rs.core.Response) ErrorData(org.apache.pulsar.common.policies.data.ErrorData)

Example 22 with ErrorData

use of org.apache.pulsar.common.policies.data.ErrorData 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 23 with ErrorData

use of org.apache.pulsar.common.policies.data.ErrorData 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)

Example 24 with ErrorData

use of org.apache.pulsar.common.policies.data.ErrorData in project incubator-pulsar by apache.

the class FunctionsImpl method deregisterFunction.

@DELETE
@Path("/{tenant}/{namespace}/{functionName}")
public Response deregisterFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName) {
    // validate parameters
    try {
        validateDeregisterRequestParams(tenant, namespace, functionName);
    } catch (IllegalArgumentException e) {
        log.error("Invalid deregister 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 to deregister does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    CompletableFuture<RequestResult> completableFuture = functionMetaDataManager.deregisterFunction(tenant, namespace, functionName);
    RequestResult requestResult = null;
    try {
        requestResult = completableFuture.get();
        if (!requestResult.isSuccess()) {
            return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(requestResult.getMessage())).build();
        }
    } catch (ExecutionException e) {
        log.error("Execution Exception while deregistering function @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getCause().getMessage())).build();
    } catch (InterruptedException e) {
        log.error("Interrupted Exception while deregistering function @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.REQUEST_TIMEOUT).type(MediaType.APPLICATION_JSON).build();
    }
    return Response.status(Status.OK).entity(requestResult.toJson()).build();
}
Also used : RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) ExecutionException(java.util.concurrent.ExecutionException) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 25 with ErrorData

use of org.apache.pulsar.common.policies.data.ErrorData in project incubator-pulsar by apache.

the class FunctionsImpl method listFunctions.

@GET
@Path("/{tenant}/{namespace}")
public Response listFunctions(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace) {
    // validate parameters
    try {
        validateListFunctionRequestParams(tenant, namespace);
    } catch (IllegalArgumentException e) {
        log.error("Invalid listFunctions request @ /{}/{}", tenant, namespace, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    Collection<String> functionStateList = functionMetaDataManager.listFunctions(tenant, namespace);
    return Response.status(Status.OK).entity(new Gson().toJson(functionStateList.toArray())).build();
}
Also used : FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) Gson(com.google.gson.Gson) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ErrorData (org.apache.pulsar.common.policies.data.ErrorData)26 Response (javax.ws.rs.core.Response)17 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Test (org.testng.annotations.Test)12 IOException (java.io.IOException)9 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)9 Path (javax.ws.rs.Path)8 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)8 RequestResult (org.apache.pulsar.functions.worker.request.RequestResult)8 InputStream (java.io.InputStream)6 Namespace (org.apache.distributedlog.api.namespace.Namespace)6 ExecutionException (java.util.concurrent.ExecutionException)5 FunctionConfig (org.apache.pulsar.functions.proto.Function.FunctionConfig)5 GET (javax.ws.rs.GET)4 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)2 PackageLocationMetaData (org.apache.pulsar.functions.proto.Function.PackageLocationMetaData)2 FunctionRuntimeManager (org.apache.pulsar.functions.worker.FunctionRuntimeManager)2 Gson (com.google.gson.Gson)1 DELETE (javax.ws.rs.DELETE)1