Search in sources :

Example 1 with GetServiceRequest

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

the class FunctionComputeClientTest method setup.

@Before
public void setup() {
    // Create or clean up everything under the test service
    client = new FunctionComputeClient(REGION, ACCOUNT_ID, ACCESS_KEY, SECRET_KEY);
    GetServiceRequest getSReq = new GetServiceRequest(SERVICE_NAME);
    try {
        client.getService(getSReq);
        ListFunctionsRequest listFReq = new ListFunctionsRequest(SERVICE_NAME);
        ListFunctionsResponse listFResp = client.listFunctions(listFReq);
        cleanUpFunctions(SERVICE_NAME, listFResp.getFunctions());
        cleanupService(SERVICE_NAME);
    } catch (ClientException e) {
        if (!ErrorCodes.SERVICE_NOT_FOUND.equals(e.getErrorCode())) {
            throw e;
        }
    }
}
Also used : ListFunctionsResponse(com.aliyuncs.fc.response.ListFunctionsResponse) GetServiceRequest(com.aliyuncs.fc.request.GetServiceRequest) ListFunctionsRequest(com.aliyuncs.fc.request.ListFunctionsRequest) ClientException(com.aliyuncs.fc.exceptions.ClientException) FunctionComputeClient(com.aliyuncs.fc.client.FunctionComputeClient) Before(org.junit.Before)

Example 2 with GetServiceRequest

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

the class FunctionComputeClientTest method testListServices.

@Test
public void testListServices() {
    final int numServices = 10;
    final int limit = 3;
    // Create multiple services
    for (int i = 0; i < numServices; i++) {
        try {
            client.getService(new GetServiceRequest(SERVICE_NAME + i));
            cleanupService(SERVICE_NAME + i);
        } catch (ClientException e) {
            if (!ErrorCodes.SERVICE_NOT_FOUND.equals(e.getErrorCode())) {
                throw new RuntimeException("Cleanup failed");
            }
        }
        CreateServiceRequest request = new CreateServiceRequest();
        request.setServiceName(SERVICE_NAME + i);
        request.setDescription(SERVICE_DESC_OLD);
        request.setRole(ROLE);
        CreateServiceResponse response = client.createService(request);
        assertFalse(Strings.isNullOrEmpty(response.getRequestId()));
    }
    ListServicesRequest listRequest = new ListServicesRequest();
    listRequest.setLimit(limit);
    listRequest.setPrefix(SERVICE_NAME);
    ListServicesResponse listResponse = client.listServices(listRequest);
    int numCalled = 1;
    String nextToken = listResponse.getNextToken();
    while (nextToken != null) {
        listRequest.setNextToken(nextToken);
        listResponse = client.listServices(listRequest);
        nextToken = listResponse.getNextToken();
        numCalled++;
    }
    assertEquals(numServices / limit + 1, numCalled);
    // Delete services
    for (int i = 0; i < numServices; i++) {
        cleanupService(SERVICE_NAME + i);
    }
}
Also used : ListServicesRequest(com.aliyuncs.fc.request.ListServicesRequest) CreateServiceRequest(com.aliyuncs.fc.request.CreateServiceRequest) ListServicesResponse(com.aliyuncs.fc.response.ListServicesResponse) CreateServiceResponse(com.aliyuncs.fc.response.CreateServiceResponse) GetServiceRequest(com.aliyuncs.fc.request.GetServiceRequest) ClientException(com.aliyuncs.fc.exceptions.ClientException) Test(org.junit.Test)

Example 3 with GetServiceRequest

use of com.aliyuncs.fc.request.GetServiceRequest 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 4 with GetServiceRequest

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

the class FunctionComputeClientTest method testGetServiceValidate.

@Test
public void testGetServiceValidate() {
    try {
        GetServiceRequest request = new GetServiceRequest(null);
        client.getService(request);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getMessage().contains(VALIDATE_MSG));
    }
    try {
        GetServiceRequest request = new GetServiceRequest("");
        client.getService(request);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getMessage().contains(VALIDATE_MSG));
    }
}
Also used : GetServiceRequest(com.aliyuncs.fc.request.GetServiceRequest) ClientException(com.aliyuncs.fc.exceptions.ClientException) Test(org.junit.Test)

Aggregations

ClientException (com.aliyuncs.fc.exceptions.ClientException)4 GetServiceRequest (com.aliyuncs.fc.request.GetServiceRequest)4 ListFunctionsRequest (com.aliyuncs.fc.request.ListFunctionsRequest)2 CreateServiceResponse (com.aliyuncs.fc.response.CreateServiceResponse)2 ListFunctionsResponse (com.aliyuncs.fc.response.ListFunctionsResponse)2 Test (org.junit.Test)2 FunctionComputeClient (com.aliyuncs.fc.client.FunctionComputeClient)1 FunctionMetadata (com.aliyuncs.fc.model.FunctionMetadata)1 OSSTriggerConfig (com.aliyuncs.fc.model.OSSTriggerConfig)1 TriggerMetadata (com.aliyuncs.fc.model.TriggerMetadata)1 CreateServiceRequest (com.aliyuncs.fc.request.CreateServiceRequest)1 DeleteFunctionRequest (com.aliyuncs.fc.request.DeleteFunctionRequest)1 DeleteServiceRequest (com.aliyuncs.fc.request.DeleteServiceRequest)1 GetFunctionCodeRequest (com.aliyuncs.fc.request.GetFunctionCodeRequest)1 GetFunctionRequest (com.aliyuncs.fc.request.GetFunctionRequest)1 GetTriggerRequest (com.aliyuncs.fc.request.GetTriggerRequest)1 InvokeFunctionRequest (com.aliyuncs.fc.request.InvokeFunctionRequest)1 ListServicesRequest (com.aliyuncs.fc.request.ListServicesRequest)1 ListTriggersRequest (com.aliyuncs.fc.request.ListTriggersRequest)1 UpdateFunctionRequest (com.aliyuncs.fc.request.UpdateFunctionRequest)1