Java 类com.amazonaws.services.lambda.model.InvocationType 实例源码

项目:aws-lambda-jenkins-plugin    文件:LambdaInvokeServiceTest.java   
@Test
public void testInvokeLambdaFunctionAsynchronous() throws Exception {
    InvokeConfig invokeConfig = new InvokeConfig("function", "{\"key1\": \"value1\"}", false, null);

    when(awsLambdaClient.invoke(any(InvokeRequest.class)))
            .thenReturn(new InvokeResult());

    String result = lambdaInvokeService.invokeLambdaFunction(invokeConfig);

    verify(awsLambdaClient, times(1)).invoke(invokeRequestArg.capture());
    InvokeRequest invokeRequest = invokeRequestArg.getValue();
    assertEquals("function", invokeRequest.getFunctionName());
    assertEquals("{\"key1\": \"value1\"}", new String(invokeRequest.getPayload().array(), Charset.forName("UTF-8")));
    assertEquals(InvocationType.Event.toString(), invokeRequest.getInvocationType());
    verify(jenkinsLogger).log(eq("Lambda invoke request:%n%s%nPayload:%n%s%n"), anyVararg());
    verify(jenkinsLogger).log(eq("Lambda invoke response:%n%s%nPayload:%n%s%n"), anyVararg());
    assertEquals("", result);
}
项目:ooso    文件:Commons.java   
public static void invokeLambda(String function, Object payload, boolean async) {
    String payloadString = GSON.toJson(payload);
    AWSLambda lambda = AWSLambdaProvider.getLambdaClient();

    InvokeRequest request = new InvokeRequest()
            .withFunctionName(function)
            .withInvocationType(async ? InvocationType.Event : InvocationType.RequestResponse)
            .withPayload(payloadString);

    lambda.invoke(request);
}
项目:alexa-meets-polly    文件:SkillClient.java   
public String translate(final String testPhrase, final String language) {
    final Map<String, Slot> slots = new HashMap<>();
    slots.put("termA", Slot.builder().withName("termA").withValue(testPhrase).build());
    slots.put("termB", Slot.builder().withName("termB").build());
    slots.put("language", Slot.builder().withName("language").withValue(language).build());
    final SpeechletRequestEnvelope envelope = givenIntentSpeechletRequestEnvelope("Translate", slots);
    final ObjectMapper mapper = new ObjectMapper();
    String response = null;
    try {
        final AWSLambdaClient awsLambda = new AWSLambdaClient();
        final InvokeRequest invokeRequest = new InvokeRequest()
                .withInvocationType(InvocationType.RequestResponse)
                .withFunctionName(lambdaName)
                .withPayload(mapper.writeValueAsString(envelope));
        final InvokeResult invokeResult = awsLambda.invoke(invokeRequest);
        response = new String(invokeResult.getPayload().array());
    } catch (JsonProcessingException e) {
        log.error(e.getMessage());
    }
    return response;
}
项目:aws-lambda-jenkins-plugin    文件:LambdaInvokeServiceTest.java   
@Test
public void testInvokeLambdaFunctionAsynchronousError() throws Exception {
    InvokeConfig invokeConfig = new InvokeConfig("function", "{\"key1\": \"value1\"}", false, null);

    when(awsLambdaClient.invoke(any(InvokeRequest.class)))
            .thenReturn(new InvokeResult().withFunctionError("Handled"));

    try {
        lambdaInvokeService.invokeLambdaFunction(invokeConfig);
        fail("Should fail with LambdaInvokeException");
    } catch (LambdaInvokeException lie){
        assertEquals("Function returned error of type: Handled", lie.getMessage());
    }

    verify(awsLambdaClient, times(1)).invoke(invokeRequestArg.capture());
    InvokeRequest invokeRequest = invokeRequestArg.getValue();
    assertEquals("function", invokeRequest.getFunctionName());
    assertEquals("{\"key1\": \"value1\"}", new String(invokeRequest.getPayload().array(), Charset.forName("UTF-8")));
    assertEquals(InvocationType.Event.toString(), invokeRequest.getInvocationType());
    verify(jenkinsLogger).log(eq("Lambda invoke request:%n%s%nPayload:%n%s%n"), anyVararg());
    verify(jenkinsLogger).log(eq("Lambda invoke response:%n%s%nPayload:%n%s%n"), anyVararg());
}
项目:alexa-skills-kit-tester-java    文件:AlexaLambdaEndpoint.java   
public Optional<AlexaResponse> fire(AlexaRequest request, String payload) {
    final InvocationType invocationType = request.expectsResponse() ? InvocationType.RequestResponse : InvocationType.Event;
    final InvokeRequest invokeRequest = new InvokeRequest()
            .withInvocationType(invocationType)
            .withFunctionName(lambdaFunctionName)
            .withPayload(payload);
    log.info(String.format("->[INFO] Invoke lambda function '%s'.", lambdaFunctionName));
    log.debug(String.format("->[INFO] with request payload '%s'.", payload));
    final InvokeResult invokeResult = lambdaClient.invoke(invokeRequest);
    return invocationType.equals(InvocationType.RequestResponse) ?
            Optional.of(new AlexaResponse(request, payload, new String(invokeResult.getPayload().array()))) : Optional.empty();
}
项目:aws-lambda-jenkins-plugin    文件:LambdaInvokeService.java   
/**
 * Synchronously or asynchronously invokes an AWS Lambda function.
 * If synchronously invoked, the AWS Lambda log is collected and the response payload is returned
 * @param invokeConfig AWS Lambda invocation configuration
 * @return response payload
 */
public String invokeLambdaFunction(InvokeConfig invokeConfig) throws LambdaInvokeException {
    InvokeRequest invokeRequest = new InvokeRequest()
            .withFunctionName(invokeConfig.getFunctionName())
            .withPayload(invokeConfig.getPayload());

    if(invokeConfig.isSynchronous()){
        invokeRequest
                .withInvocationType(InvocationType.RequestResponse)
                .withLogType(LogType.Tail);
    } else {
        invokeRequest
                .withInvocationType(InvocationType.Event);
    }
    logger.log("Lambda invoke request:%n%s%nPayload:%n%s%n", invokeRequest.toString(), invokeConfig.getPayload());

    InvokeResult invokeResult = client.invoke(invokeRequest);
    String payload = "";
    if(invokeResult.getPayload() != null){
        payload = new String(invokeResult.getPayload().array(), Charset.forName("UTF-8"));
    }
    logger.log("Lambda invoke response:%n%s%nPayload:%n%s%n", invokeResult.toString(), payload);

    if(invokeResult.getLogResult() != null){
        logger.log("Log:%n%s%n", new String(Base64.decode(invokeResult.getLogResult()), Charset.forName("UTF-8")));
    }

    if(StringUtils.isNotEmpty(invokeResult.getFunctionError())){
        throw new LambdaInvokeException("Function returned error of type: " + invokeResult.getFunctionError());
    }

    return payload;
}
项目:aws-lambda-jenkins-plugin    文件:LambdaInvokeServiceTest.java   
@Test
public void testInvokeLambdaFunctionSynchronous() throws Exception {
    final String logBase64 = "bGFtYmRh";
    final String log = "lambda";
    final String requestPayload = "{\"key1\": \"value1\"}";
    final String responsePayload = "{\"key2\": \"value2\"}";

    InvokeConfig invokeConfig = new InvokeConfig("function", requestPayload, true, null);

    InvokeResult invokeResult = new InvokeResult()
            .withLogResult(logBase64)
            .withPayload(ByteBuffer.wrap(responsePayload.getBytes()));

    when(awsLambdaClient.invoke(any(InvokeRequest.class)))
            .thenReturn(invokeResult);

    String result = lambdaInvokeService.invokeLambdaFunction(invokeConfig);

    verify(awsLambdaClient, times(1)).invoke(invokeRequestArg.capture());
    InvokeRequest invokeRequest = invokeRequestArg.getValue();
    assertEquals("function", invokeRequest.getFunctionName());
    assertEquals(LogType.Tail.toString(), invokeRequest.getLogType());
    assertEquals(requestPayload, new String(invokeRequest.getPayload().array(), Charset.forName("UTF-8")));
    assertEquals(InvocationType.RequestResponse.toString(), invokeRequest.getInvocationType());

    ArgumentCaptor<String> stringArgs = ArgumentCaptor.forClass(String.class);
    verify(jenkinsLogger).log(eq("Lambda invoke request:%n%s%nPayload:%n%s%n"), stringArgs.capture());
    List<String> stringArgValues = stringArgs.getAllValues();
    assertEquals(Arrays.asList(invokeRequest.toString(), requestPayload), stringArgValues);

    verify(jenkinsLogger).log(eq("Log:%n%s%n"), eq(log));

    stringArgs = ArgumentCaptor.forClass(String.class);
    verify(jenkinsLogger).log(eq("Lambda invoke response:%n%s%nPayload:%n%s%n"), stringArgs.capture());
    stringArgValues = stringArgs.getAllValues();
    assertEquals(Arrays.asList(invokeResult.toString(), responsePayload), stringArgValues);

    assertEquals(responsePayload, result);
}
项目:openbd-core    文件:LambdaAsyncExecute.java   
/**
 * Executes a lambda function and returns the result of the execution.
 */
@Override
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {

    AmazonKey amazonKey = getAmazonKey( _session, argStruct );

    // Arguments to extract
    String payload = getNamedStringParam( argStruct, "payload", null );
    String functionName = getNamedStringParam( argStruct, "function", null );
    String qualifier = getNamedStringParam( argStruct, "qualifier", null );

    try {

        // Construct the Lambda Client
        InvokeRequest invokeRequest = new InvokeRequest();
        invokeRequest.setInvocationType( InvocationType.Event );
        invokeRequest.setLogType( LogType.Tail );
        invokeRequest.setFunctionName( functionName );
        invokeRequest.setPayload( payload );
        if ( qualifier != null ) {
            invokeRequest.setQualifier( qualifier );
        }

        // Lambda client must be created with credentials
        BasicAWSCredentials awsCreds = new BasicAWSCredentials( amazonKey.getKey(), amazonKey.getSecret() );
        AWSLambda awsLambda = AWSLambdaClientBuilder.standard()
                .withRegion( amazonKey.getAmazonRegion().toAWSRegion().getName() )
                .withCredentials( new AWSStaticCredentialsProvider( awsCreds ) ).build();

        // Execute
        awsLambda.invoke( invokeRequest );

    } catch ( Exception e ) {
        throwException( _session, "AmazonLambdaAsyncExecute: " + e.getMessage() );
        return cfBooleanData.FALSE;
    }

    return cfBooleanData.TRUE;
}
项目:aws-lambda-jenkins-plugin    文件:LambdaInvokeServiceTest.java   
@Test
public void testInvokeLambdaFunctionSynchronousError() throws Exception {
    final String logBase64 = "bGFtYmRh";
    final String log = "lambda";
    final String requestPayload = "{\"key1\": \"value1\"}";
    final String responsePayload = "{\"errorMessage\":\"event_fail\"}";

    InvokeConfig invokeConfig = new InvokeConfig("function", requestPayload, true, null);

    InvokeResult invokeResult = new InvokeResult()
            .withLogResult(logBase64)
            .withPayload(ByteBuffer.wrap(responsePayload.getBytes()))
            .withFunctionError("Unhandled");

    when(awsLambdaClient.invoke(any(InvokeRequest.class)))
            .thenReturn(invokeResult);

    try {
        lambdaInvokeService.invokeLambdaFunction(invokeConfig);
        fail("Should fail with LambdaInvokeException");
    } catch (LambdaInvokeException lie){
        assertEquals("Function returned error of type: Unhandled", lie.getMessage());
    }

    verify(awsLambdaClient, times(1)).invoke(invokeRequestArg.capture());
    InvokeRequest invokeRequest = invokeRequestArg.getValue();
    assertEquals("function", invokeRequest.getFunctionName());
    assertEquals(LogType.Tail.toString(), invokeRequest.getLogType());
    assertEquals(requestPayload, new String(invokeRequest.getPayload().array(), Charset.forName("UTF-8")));
    assertEquals(InvocationType.RequestResponse.toString(), invokeRequest.getInvocationType());

    ArgumentCaptor<String> stringArgs = ArgumentCaptor.forClass(String.class);
    verify(jenkinsLogger).log(eq("Lambda invoke request:%n%s%nPayload:%n%s%n"), stringArgs.capture());
    List<String> stringArgValues = stringArgs.getAllValues();
    assertEquals(Arrays.asList(invokeRequest.toString(), requestPayload), stringArgValues);

    verify(jenkinsLogger).log(eq("Log:%n%s%n"), eq(log));

    stringArgs = ArgumentCaptor.forClass(String.class);
    verify(jenkinsLogger).log(eq("Lambda invoke response:%n%s%nPayload:%n%s%n"), stringArgs.capture());
    stringArgValues = stringArgs.getAllValues();
    assertEquals(Arrays.asList(invokeResult.toString(), responsePayload), stringArgValues);
}
项目:openbd-core    文件:LambdaExecute.java   
/**
 * Executes a lambda function and returns the result of the execution.
 */
@Override
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {

    AmazonKey amazonKey = getAmazonKey( _session, argStruct );

    // Arguments to extract
    String payload = getNamedStringParam( argStruct, "payload", null );
    String functionName = getNamedStringParam( argStruct, "function", null );
    String qualifier = getNamedStringParam( argStruct, "qualifier", null );

    try {

        // Construct the Lambda Client
        InvokeRequest invokeRequest = new InvokeRequest();
        invokeRequest.setInvocationType( InvocationType.RequestResponse );
        invokeRequest.setLogType( LogType.Tail );
        invokeRequest.setFunctionName( functionName );
        invokeRequest.setPayload( payload );
        if ( qualifier != null ) {
            invokeRequest.setQualifier( qualifier );
        }

        // Lambda client must be created with credentials
        BasicAWSCredentials awsCreds = new BasicAWSCredentials( amazonKey.getKey(), amazonKey.getSecret() );
        AWSLambda awsLambda = AWSLambdaClientBuilder.standard()
                .withRegion( amazonKey.getAmazonRegion().toAWSRegion().getName() )
                .withCredentials( new AWSStaticCredentialsProvider( awsCreds ) ).build();

        // Execute and process the results
        InvokeResult result = awsLambda.invoke( invokeRequest );

        // Convert the returned result
        ByteBuffer resultPayload = result.getPayload();
        String resultJson = new String( resultPayload.array(), "UTF-8" );
        Map<String, Object> resultMap = Jackson.fromJsonString( resultJson, Map.class );

        return tagUtils.convertToCfData( resultMap );

    } catch ( Exception e ) {
        throwException( _session, "AmazonLambdaExecute: " + e.getMessage() );
        return cfBooleanData.FALSE;
    }

}