Search in sources :

Example 1 with FunctionConfiguration

use of com.amazonaws.services.lambda.model.FunctionConfiguration in project aws-doc-sdk-examples by awsdocs.

the class ListFunctions method main.

public static void main(String[] args) {
    // snippet-start:[lambda.java1.list.main]
    ListFunctionsResult functionResult = null;
    try {
        AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(Regions.US_WEST_2).build();
        functionResult = awsLambda.listFunctions();
        List<FunctionConfiguration> list = functionResult.getFunctions();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            FunctionConfiguration config = (FunctionConfiguration) iter.next();
            System.out.println("The function name is " + config.getFunctionName());
        }
    } catch (ServiceException e) {
        System.out.println(e);
    }
// snippet-end:[lambda.java1.list.main]
}
Also used : ListFunctionsResult(com.amazonaws.services.lambda.model.ListFunctionsResult) AWSLambda(com.amazonaws.services.lambda.AWSLambda) ServiceException(com.amazonaws.services.lambda.model.ServiceException) FunctionConfiguration(com.amazonaws.services.lambda.model.FunctionConfiguration) Iterator(java.util.Iterator) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider)

Example 2 with FunctionConfiguration

use of com.amazonaws.services.lambda.model.FunctionConfiguration in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getAmazonResourceNamesOfAPI.

// AWS Lambda: rest api operation to get ARNs
@Override
public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) {
    JSONObject arns = new JSONObject();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        String endpointConfigString = api.getEndpointConfig();
        if (!StringUtils.isEmpty(endpointConfigString)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject endpointConfig = (JSONObject) jsonParser.parse(endpointConfigString);
            if (endpointConfig != null) {
                if (endpointConfig.containsKey(APIConstants.AMZN_ACCESS_KEY) && endpointConfig.containsKey(APIConstants.AMZN_SECRET_KEY) && endpointConfig.containsKey(APIConstants.AMZN_REGION)) {
                    String accessKey = (String) endpointConfig.get(APIConstants.AMZN_ACCESS_KEY);
                    String secretKey = (String) endpointConfig.get(APIConstants.AMZN_SECRET_KEY);
                    String region = (String) endpointConfig.get(APIConstants.AMZN_REGION);
                    AWSCredentialsProvider credentialsProvider;
                    AWSLambda awsLambda;
                    if (StringUtils.isEmpty(accessKey) && StringUtils.isEmpty(secretKey) && StringUtils.isEmpty(region)) {
                        credentialsProvider = DefaultAWSCredentialsProviderChain.getInstance();
                        awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).build();
                    } else if (!StringUtils.isEmpty(accessKey) && !StringUtils.isEmpty(secretKey) && !StringUtils.isEmpty(region)) {
                        if (secretKey.length() == APIConstants.AWS_ENCRYPTED_SECRET_KEY_LENGTH) {
                            CryptoUtil cryptoUtil = CryptoUtil.getDefaultCryptoUtil();
                            secretKey = new String(cryptoUtil.base64DecodeAndDecrypt(secretKey), APIConstants.DigestAuthConstants.CHARSET);
                        }
                        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
                        credentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
                        awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).withRegion(region).build();
                    } else {
                        log.error("Missing AWS Credentials");
                        return null;
                    }
                    ListFunctionsResult listFunctionsResult = awsLambda.listFunctions();
                    List<FunctionConfiguration> functionConfigurations = listFunctionsResult.getFunctions();
                    arns.put("count", functionConfigurations.size());
                    JSONArray list = new JSONArray();
                    for (FunctionConfiguration functionConfiguration : functionConfigurations) {
                        list.put(functionConfiguration.getFunctionArn());
                    }
                    arns.put("list", list);
                    return Response.ok().entity(arns.toString()).build();
                }
            }
        }
    } catch (SdkClientException e) {
        if (e.getCause() instanceof UnknownHostException) {
            arns.put("error", "No internet connection to connect the given access method.");
            log.error("No internet connection to connect the given access method of API : " + apiId, e);
            return Response.serverError().entity(arns.toString()).build();
        } else {
            arns.put("error", "Unable to access Lambda functions under the given access method.");
            log.error("Unable to access Lambda functions under the given access method of API : " + apiId, e);
            return Response.serverError().entity(arns.toString()).build();
        }
    } catch (ParseException e) {
        String errorMessage = "Error while parsing endpoint config of the API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (CryptoException | UnsupportedEncodingException e) {
        String errorMessage = "Error while decrypting the secret key of the API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving the API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) ListFunctionsResult(com.amazonaws.services.lambda.model.ListFunctionsResult) UnknownHostException(java.net.UnknownHostException) FunctionConfiguration(com.amazonaws.services.lambda.model.FunctionConfiguration) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) CryptoUtil(org.wso2.carbon.core.util.CryptoUtil) JSONObject(org.json.simple.JSONObject) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) CryptoException(org.wso2.carbon.core.util.CryptoException) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Aggregations

AWSLambda (com.amazonaws.services.lambda.AWSLambda)2 FunctionConfiguration (com.amazonaws.services.lambda.model.FunctionConfiguration)2 ListFunctionsResult (com.amazonaws.services.lambda.model.ListFunctionsResult)2 SdkClientException (com.amazonaws.SdkClientException)1 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 ServiceException (com.amazonaws.services.lambda.model.ServiceException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 Iterator (java.util.Iterator)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 API (org.wso2.carbon.apimgt.api.model.API)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1