Search in sources :

Example 1 with DeleteFunctionRequest

use of com.aliyuncs.fc.request.DeleteFunctionRequest in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method cleanUpFunctions.

private void cleanUpFunctions(String serviceName, FunctionMetadata[] functions) {
    for (FunctionMetadata function : functions) {
        ListTriggersRequest listReq = new ListTriggersRequest(serviceName, function.getFunctionName());
        ListTriggersResponse listTResp = client.listTriggers(listReq);
        cleanUpTriggers(serviceName, function.getFunctionName(), listTResp.getTriggers());
        System.out.println("All triggers for Function " + function.getFunctionName() + " are deleted");
        DeleteFunctionRequest deleteFReq = new DeleteFunctionRequest(serviceName, function.getFunctionName());
        client.deleteFunction(deleteFReq);
    }
}
Also used : FunctionMetadata(com.aliyuncs.fc.model.FunctionMetadata) ListTriggersResponse(com.aliyuncs.fc.response.ListTriggersResponse) ListTriggersRequest(com.aliyuncs.fc.request.ListTriggersRequest) DeleteFunctionRequest(com.aliyuncs.fc.request.DeleteFunctionRequest)

Example 2 with DeleteFunctionRequest

use of com.aliyuncs.fc.request.DeleteFunctionRequest in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCreateFunctionSetZipFile.

@Test
public void testCreateFunctionSetZipFile() throws IOException {
    createService(SERVICE_NAME);
    // Create a function
    CreateFunctionRequest createFuncReq = new CreateFunctionRequest(SERVICE_NAME);
    createFuncReq.setFunctionName(FUNCTION_NAME);
    createFuncReq.setDescription("Function for test");
    createFuncReq.setMemorySize(128);
    createFuncReq.setHandler("hello_world.handler");
    createFuncReq.setRuntime("nodejs4.4");
    // Setup code directory
    String tmpDir = "/tmp/fc_test_" + UUID.randomUUID();
    String funcFilePath = tmpDir + "/" + "hello_world.js";
    new File(tmpDir).mkdir();
    PrintWriter out = new PrintWriter(funcFilePath);
    out.println("'use strict'; module.exports.handler = function(event, context, callback) {console.log('hello world'); callback(null, 'hello world');};");
    out.close();
    String zipFilePath = tmpDir + "/" + "hello_world.zip";
    ZipUtils.zipDir(new File(tmpDir), zipFilePath);
    File zipFile = new File(zipFilePath);
    byte[] buffer = new byte[(int) zipFile.length()];
    FileInputStream fis = new FileInputStream(zipFilePath);
    fis.read(buffer);
    fis.close();
    Code code = new Code().setZipFile(buffer);
    createFuncReq.setCode(code);
    createFuncReq.setTimeout(10);
    client.createFunction(createFuncReq);
    // Invoke the function
    InvokeFunctionRequest request = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    InvokeFunctionResponse response = client.invokeFunction(request);
    assertEquals("hello world", new String(response.getContent()));
    // Cleanups
    client.deleteFunction(new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME));
    client.deleteService(new DeleteServiceRequest(SERVICE_NAME));
    new File(zipFilePath).delete();
    new File(funcFilePath).delete();
    new File(tmpDir).delete();
}
Also used : InvokeFunctionRequest(com.aliyuncs.fc.request.InvokeFunctionRequest) InvokeFunctionResponse(com.aliyuncs.fc.response.InvokeFunctionResponse) CreateFunctionRequest(com.aliyuncs.fc.request.CreateFunctionRequest) DeleteServiceRequest(com.aliyuncs.fc.request.DeleteServiceRequest) DeleteFunctionRequest(com.aliyuncs.fc.request.DeleteFunctionRequest) File(java.io.File) Code(com.aliyuncs.fc.model.Code) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

DeleteFunctionRequest (com.aliyuncs.fc.request.DeleteFunctionRequest)2 Code (com.aliyuncs.fc.model.Code)1 FunctionMetadata (com.aliyuncs.fc.model.FunctionMetadata)1 CreateFunctionRequest (com.aliyuncs.fc.request.CreateFunctionRequest)1 DeleteServiceRequest (com.aliyuncs.fc.request.DeleteServiceRequest)1 InvokeFunctionRequest (com.aliyuncs.fc.request.InvokeFunctionRequest)1 ListTriggersRequest (com.aliyuncs.fc.request.ListTriggersRequest)1 InvokeFunctionResponse (com.aliyuncs.fc.response.InvokeFunctionResponse)1 ListTriggersResponse (com.aliyuncs.fc.response.ListTriggersResponse)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 PrintWriter (java.io.PrintWriter)1 Test (org.junit.Test)1