Search in sources :

Example 1 with FunctionMetadata

use of com.aliyuncs.fc.model.FunctionMetadata 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 FunctionMetadata

use of com.aliyuncs.fc.model.FunctionMetadata in project fc-java-sdk by aliyun.

the class FunctionComputeClient method updateFunction.

public UpdateFunctionResponse updateFunction(UpdateFunctionRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "PUT");
    FunctionMetadata functionMetadata = GSON.fromJson(new String(response.getContent()), FunctionMetadata.class);
    UpdateFunctionResponse updateFunctionResponse = new UpdateFunctionResponse();
    updateFunctionResponse.setFunctionMetadata(functionMetadata);
    updateFunctionResponse.setHeader(response.getHeaders());
    updateFunctionResponse.setContent(response.getContent());
    updateFunctionResponse.setStatus(response.getStatus());
    return updateFunctionResponse;
}
Also used : FunctionMetadata(com.aliyuncs.fc.model.FunctionMetadata) HttpResponse(com.aliyuncs.fc.http.HttpResponse)

Example 3 with FunctionMetadata

use of com.aliyuncs.fc.model.FunctionMetadata in project fc-java-sdk by aliyun.

the class FunctionComputeClient method getFunction.

public GetFunctionResponse getFunction(GetFunctionRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "GET");
    FunctionMetadata functionMetadata = GSON.fromJson(new String(response.getContent()), FunctionMetadata.class);
    GetFunctionResponse getFunctionResponse = new GetFunctionResponse();
    getFunctionResponse.setFunctionMetadata(functionMetadata);
    getFunctionResponse.setHeader(response.getHeaders());
    getFunctionResponse.setContent(response.getContent());
    getFunctionResponse.setStatus(response.getStatus());
    return getFunctionResponse;
}
Also used : FunctionMetadata(com.aliyuncs.fc.model.FunctionMetadata) HttpResponse(com.aliyuncs.fc.http.HttpResponse)

Example 4 with FunctionMetadata

use of com.aliyuncs.fc.model.FunctionMetadata in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCRUDHelper.

private void testCRUDHelper(boolean testTrigger) throws ParseException, InterruptedException {
    // Create Service
    CreateServiceResponse createSResp = createService(SERVICE_NAME);
    assertFalse(Strings.isNullOrEmpty(createSResp.getRequestId()));
    assertFalse(Strings.isNullOrEmpty(createSResp.getServiceId()));
    assertEquals(SERVICE_NAME, createSResp.getServiceName());
    assertEquals(SERVICE_DESC_OLD, createSResp.getDescription());
    assertEquals(ROLE, createSResp.getRole());
    GetServiceResponse svcOldResp = client.getService(new GetServiceRequest(SERVICE_NAME));
    // Update Service
    UpdateServiceRequest updateSReq = new UpdateServiceRequest(SERVICE_NAME);
    updateSReq.setDescription(SERVICE_DESC_NEW);
    Thread.sleep(1000L);
    UpdateServiceResponse updateSResp = client.updateService(updateSReq);
    verifyUpdate(svcOldResp.getServiceName(), updateSResp.getServiceName(), svcOldResp.getServiceId(), updateSResp.getServiceId(), svcOldResp.getLastModifiedTime(), updateSResp.getLastModifiedTime(), svcOldResp.getCreatedTime(), updateSResp.getCreatedTime(), svcOldResp.getDescription(), updateSResp.getDescription());
    // Get Service
    GetServiceRequest getSReq = new GetServiceRequest(SERVICE_NAME);
    GetServiceResponse getSResp = client.getService(getSReq);
    assertEquals(SERVICE_NAME, getSResp.getServiceName());
    assertEquals(svcOldResp.getServiceId(), getSResp.getServiceId());
    assertEquals(ROLE, getSResp.getRole());
    // Create Function
    CreateFunctionResponse createFResp = createFunction(FUNCTION_NAME);
    assertFalse(Strings.isNullOrEmpty(createFResp.getRequestId()));
    assertFalse(Strings.isNullOrEmpty(createFResp.getFunctionId()));
    assertEquals(FUNCTION_NAME, createFResp.getFunctionName());
    assertEquals(FUNCTION_DESC_OLD, createFResp.getDescription());
    // List Functions
    ListFunctionsRequest listFReq = new ListFunctionsRequest(SERVICE_NAME);
    ListFunctionsResponse listFResp = client.listFunctions(listFReq);
    assertFalse(Strings.isNullOrEmpty(listFResp.getRequestId()));
    assertEquals(1, listFResp.getFunctions().length);
    FunctionMetadata funcOld = listFResp.getFunctions()[0];
    assertEquals(FUNCTION_NAME, funcOld.getFunctionName());
    // Update Function
    UpdateFunctionRequest updateFReq = new UpdateFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    updateFReq.setDescription(FUNCTION_DESC_NEW);
    Thread.sleep(1000L);
    client.updateFunction(updateFReq);
    listFResp = client.listFunctions(listFReq);
    assertFalse(Strings.isNullOrEmpty(listFResp.getRequestId()));
    assertEquals(1, listFResp.getFunctions().length);
    FunctionMetadata funcNew = listFResp.getFunctions()[0];
    verifyUpdate(funcOld.getFunctionName(), funcNew.getFunctionName(), funcOld.getFunctionId(), funcNew.getFunctionId(), funcOld.getLastModifiedTime(), funcNew.getLastModifiedTime(), funcOld.getCreatedTime(), funcNew.getCreatedTime(), funcOld.getDescription(), funcNew.getDescription());
    // Get Function
    GetFunctionRequest getFReq = new GetFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    GetFunctionResponse getFResp = client.getFunction(getFReq);
    assertFalse(Strings.isNullOrEmpty(getFResp.getRequestId()));
    assertEquals(FUNCTION_NAME, getFResp.getFunctionName());
    // Get Function Code
    GetFunctionCodeRequest getFCReq = new GetFunctionCodeRequest(SERVICE_NAME, FUNCTION_NAME);
    GetFunctionCodeResponse getFCResp = client.getFunctionCode(getFCReq);
    assertFalse(Strings.isNullOrEmpty(getFResp.getRequestId()));
    String crc64 = fetchFromURL(getFCResp.getCodeUrl());
    assertEquals(crc64, getFCResp.getCodeChecksum());
    // Invoke Function
    InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    InvokeFunctionResponse invkResp = client.invokeFunction(invkReq);
    assertTrue(!Strings.isNullOrEmpty(invkResp.getRequestId()));
    assertEquals("hello world", new String(invkResp.getContent()));
    // Invoke Function Async
    invkReq.setInvocationType(Const.INVOCATION_TYPE_ASYNC);
    invkResp = client.invokeFunction(invkReq);
    assertEquals(HttpURLConnection.HTTP_ACCEPTED, invkResp.getStatus());
    if (testTrigger) {
        // Create Trigger
        String tfPrefix = "prefix";
        String tfSuffix = "suffix";
        createTrigger(TRIGGER_NAME, tfPrefix, tfSuffix);
        // List Triggers
        ListTriggersRequest listTReq = new ListTriggersRequest(SERVICE_NAME, FUNCTION_NAME);
        ListTriggersResponse listTResp = client.listTriggers(listTReq);
        assertFalse(Strings.isNullOrEmpty(listTResp.getRequestId()));
        assertEquals(1, listTResp.getTriggers().length);
        TriggerMetadata triggerOld = listTResp.getTriggers()[0];
        assertEquals(TRIGGER_NAME, triggerOld.getTriggerName());
        // Update Trigger
        String newInvocationRole = INVOCATION_ROLE + "_new";
        String tfPrefixNew = "prefix_new";
        String tfSuffixNew = "suffix_new";
        String[] eventsNew = new String[] { "oss:ObjectCreated:PutObject" };
        OSSTriggerConfig updateTriggerConfig = new OSSTriggerConfig(new String[] { "oss:ObjectCreated:PutObject" }, tfPrefixNew, tfSuffixNew);
        UpdateTriggerRequest updateTReq = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
        updateTReq.setInvocationRole(newInvocationRole);
        updateTReq.setTriggerConfig(updateTriggerConfig);
        UpdateTriggerResponse updateTResp = updateTrigger(updateTReq);
        assertEquals(triggerOld.getTriggerName(), updateTResp.getTriggerName());
        assertNotEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
        assertEquals(triggerOld.getSourceArn(), updateTResp.getSourceArn());
        Gson gson = new Gson();
        OSSTriggerConfig tcOld = gson.fromJson(gson.toJson(triggerOld.getTriggerConfig()), OSSTriggerConfig.class);
        OSSTriggerConfig tcNew = gson.fromJson(gson.toJson(updateTResp.getTriggerConfig()), OSSTriggerConfig.class);
        assertFalse(Arrays.deepEquals(tcOld.getEvents(), tcNew.getEvents()));
        assertNotEquals(tcOld.getFilter().getKey().getPrefix(), tcNew.getFilter().getKey().getPrefix());
        assertNotEquals(tcOld.getFilter().getKey().getSuffix(), tcNew.getFilter().getKey().getSuffix());
        assertEquals(triggerOld.getCreatedTime(), updateTResp.getCreatedTime());
        assertEquals(triggerOld.getTriggerType(), updateTResp.getTriggerType());
        assertNotEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
        Date dateOld = DATE_FORMAT.parse(triggerOld.getLastModifiedTime());
        Date dateNew = DATE_FORMAT.parse(updateTResp.getLastModifiedTime());
        assertTrue(dateOld.before(dateNew));
        // Get Trigger
        GetTriggerRequest getTReq = new GetTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
        GetTriggerResponse getTResp = client.getTrigger(getTReq);
        OSSTriggerConfig getTConfig = gson.fromJson(gson.toJson(getTResp.getTriggerConfig()), OSSTriggerConfig.class);
        assertFalse(Strings.isNullOrEmpty(getTResp.getRequestId()));
        assertEquals(TRIGGER_NAME, getTResp.getTriggerName());
        assertEquals(OSS_SOURCE_ARN, getTResp.getSourceARN());
        assertEquals(TRIGGER_TYPE_OSS, getTResp.getTriggerType());
        assertEquals(newInvocationRole, getTResp.getInvocationRole());
        assertEquals(tfPrefixNew, getTConfig.getFilter().getKey().getPrefix());
        assertEquals(tfSuffixNew, getTConfig.getFilter().getKey().getSuffix());
        assertTrue(Arrays.deepEquals(eventsNew, getTConfig.getEvents()));
        // Delete Trigger
        deleteTrigger(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
    }
    // Delete Function
    DeleteFunctionRequest deleteFReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    int numFunctionsOld = listFResp.getFunctions().length;
    DeleteFunctionResponse deleteFResp = client.deleteFunction(deleteFReq);
    assertFalse(Strings.isNullOrEmpty(deleteFResp.getRequestId()));
    listFResp = client.listFunctions(listFReq);
    try {
        getFunction(FUNCTION_NAME, listFResp.getFunctions());
        fail("Function " + FUNCTION_NAME + " failed to be deleted");
    } catch (RuntimeException e) {
        int numFunctionsNew = (listFResp.getFunctions() == null) ? 0 : listFResp.getFunctions().length;
        assertEquals(numFunctionsOld, numFunctionsNew + 1);
    }
    GetFunctionResponse getFResp2 = null;
    try {
        getFResp2 = client.getFunction(getFReq);
        fail("Get Function " + FUNCTION_NAME + " should have no function returned after delete");
    } catch (ClientException e) {
        assertNull(getFResp2);
    }
    // Delete Service
    DeleteServiceRequest deleteSReq = new DeleteServiceRequest(SERVICE_NAME);
    DeleteServiceResponse deleteSResp = client.deleteService(deleteSReq);
    assertFalse(Strings.isNullOrEmpty(deleteSResp.getRequestId()));
    GetServiceResponse getSResp2 = null;
    try {
        getSResp2 = client.getService(getSReq);
        fail("Get service " + FUNCTION_NAME + " should have no service returned after delete");
    } catch (ClientException e) {
        assertNull(getSResp2);
    }
}
Also used : UpdateTriggerResponse(com.aliyuncs.fc.response.UpdateTriggerResponse) ListFunctionsResponse(com.aliyuncs.fc.response.ListFunctionsResponse) DeleteServiceRequest(com.aliyuncs.fc.request.DeleteServiceRequest) ListTriggersRequest(com.aliyuncs.fc.request.ListTriggersRequest) GetFunctionResponse(com.aliyuncs.fc.response.GetFunctionResponse) Gson(com.google.gson.Gson) DeleteFunctionRequest(com.aliyuncs.fc.request.DeleteFunctionRequest) InvokeFunctionRequest(com.aliyuncs.fc.request.InvokeFunctionRequest) CreateFunctionResponse(com.aliyuncs.fc.response.CreateFunctionResponse) ListTriggersResponse(com.aliyuncs.fc.response.ListTriggersResponse) GetFunctionCodeResponse(com.aliyuncs.fc.response.GetFunctionCodeResponse) GetTriggerResponse(com.aliyuncs.fc.response.GetTriggerResponse) GetFunctionCodeRequest(com.aliyuncs.fc.request.GetFunctionCodeRequest) CreateServiceResponse(com.aliyuncs.fc.response.CreateServiceResponse) DeleteFunctionResponse(com.aliyuncs.fc.response.DeleteFunctionResponse) DeleteServiceResponse(com.aliyuncs.fc.response.DeleteServiceResponse) GetServiceRequest(com.aliyuncs.fc.request.GetServiceRequest) ListFunctionsRequest(com.aliyuncs.fc.request.ListFunctionsRequest) TriggerMetadata(com.aliyuncs.fc.model.TriggerMetadata) InvokeFunctionResponse(com.aliyuncs.fc.response.InvokeFunctionResponse) UpdateTriggerRequest(com.aliyuncs.fc.request.UpdateTriggerRequest) GetTriggerRequest(com.aliyuncs.fc.request.GetTriggerRequest) GetFunctionRequest(com.aliyuncs.fc.request.GetFunctionRequest) OSSTriggerConfig(com.aliyuncs.fc.model.OSSTriggerConfig) UpdateFunctionRequest(com.aliyuncs.fc.request.UpdateFunctionRequest) FunctionMetadata(com.aliyuncs.fc.model.FunctionMetadata) GetServiceResponse(com.aliyuncs.fc.response.GetServiceResponse) UpdateServiceResponse(com.aliyuncs.fc.response.UpdateServiceResponse) ClientException(com.aliyuncs.fc.exceptions.ClientException) UpdateServiceRequest(com.aliyuncs.fc.request.UpdateServiceRequest)

Example 5 with FunctionMetadata

use of com.aliyuncs.fc.model.FunctionMetadata in project fc-java-sdk by aliyun.

the class FunctionComputeClient method createFunction.

public CreateFunctionResponse createFunction(CreateFunctionRequest request) throws ClientException, ServerException {
    HttpResponse response = client.doAction(request, CONTENT_TYPE_APPLICATION_JSON, "POST");
    FunctionMetadata functionMetadata = GSON.fromJson(new String(response.getContent()), FunctionMetadata.class);
    CreateFunctionResponse createFunctionResponse = new CreateFunctionResponse();
    createFunctionResponse.setFunctionMetadata(functionMetadata);
    createFunctionResponse.setHeader(response.getHeaders());
    createFunctionResponse.setContent(response.getContent());
    createFunctionResponse.setStatus(response.getStatus());
    return createFunctionResponse;
}
Also used : FunctionMetadata(com.aliyuncs.fc.model.FunctionMetadata) HttpResponse(com.aliyuncs.fc.http.HttpResponse)

Aggregations

FunctionMetadata (com.aliyuncs.fc.model.FunctionMetadata)5 HttpResponse (com.aliyuncs.fc.http.HttpResponse)3 DeleteFunctionRequest (com.aliyuncs.fc.request.DeleteFunctionRequest)2 ListTriggersRequest (com.aliyuncs.fc.request.ListTriggersRequest)2 ListTriggersResponse (com.aliyuncs.fc.response.ListTriggersResponse)2 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 OSSTriggerConfig (com.aliyuncs.fc.model.OSSTriggerConfig)1 TriggerMetadata (com.aliyuncs.fc.model.TriggerMetadata)1 DeleteServiceRequest (com.aliyuncs.fc.request.DeleteServiceRequest)1 GetFunctionCodeRequest (com.aliyuncs.fc.request.GetFunctionCodeRequest)1 GetFunctionRequest (com.aliyuncs.fc.request.GetFunctionRequest)1 GetServiceRequest (com.aliyuncs.fc.request.GetServiceRequest)1 GetTriggerRequest (com.aliyuncs.fc.request.GetTriggerRequest)1 InvokeFunctionRequest (com.aliyuncs.fc.request.InvokeFunctionRequest)1 ListFunctionsRequest (com.aliyuncs.fc.request.ListFunctionsRequest)1 UpdateFunctionRequest (com.aliyuncs.fc.request.UpdateFunctionRequest)1 UpdateServiceRequest (com.aliyuncs.fc.request.UpdateServiceRequest)1 UpdateTriggerRequest (com.aliyuncs.fc.request.UpdateTriggerRequest)1 CreateFunctionResponse (com.aliyuncs.fc.response.CreateFunctionResponse)1 CreateServiceResponse (com.aliyuncs.fc.response.CreateServiceResponse)1