Search in sources :

Example 1 with ListTagsResult

use of com.amazonaws.services.lambda.model.ListTagsResult in project bender by Nextdoor.

the class BaseHandlerTest method tagsCloudformation.

@Test
public void tagsCloudformation() throws HandlerException {
    BaseHandler.CONFIG_FILE = "/config/handler_config_tags_duplicate.json";
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:test_tags");
    AWSLambdaClientFactory mockFactory = mock(AWSLambdaClientFactory.class);
    AWSLambda mockLambda = mock(AWSLambda.class);
    doReturn(mockLambda).when(mockFactory).newInstance();
    ListTagsResult mockResult = mock(ListTagsResult.class);
    HashMap<String, String> functionTags = new HashMap<String, String>() {

        {
            put("f1", "foo");
            put("aws:cloudformation:foo", "foo");
            put("aws:cloudformation:bar", "bar");
        }
    };
    doReturn(functionTags).when(mockResult).getTags();
    doReturn(mockResult).when(mockLambda).listTags(any());
    handler.lambdaClientFactory = mockFactory;
    handler.init(context);
    Map<String, String> actual = handler.monitor.getTagsMap();
    HashMap<String, String> expected = new HashMap<String, String>() {

        {
            put("f1", "foo");
            put("u1", "bar");
        }
    };
    assertTrue(actual.entrySet().containsAll(expected.entrySet()));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) HashMap(java.util.HashMap) TestContext(com.nextdoor.bender.aws.TestContext) ListTagsResult(com.amazonaws.services.lambda.model.ListTagsResult) AWSLambdaClientFactory(com.nextdoor.bender.aws.AWSLambdaClientFactory) Test(org.junit.Test)

Example 2 with ListTagsResult

use of com.amazonaws.services.lambda.model.ListTagsResult in project bender by Nextdoor.

the class BaseHandlerTest method testLambdaFunctionTags.

@Test
public void testLambdaFunctionTags() throws HandlerException {
    BaseHandler.CONFIG_FILE = "/config/handler_config_tags.json";
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:test_tags");
    AWSLambdaClientFactory mockFactory = mock(AWSLambdaClientFactory.class);
    AWSLambda mockLambda = mock(AWSLambda.class);
    doReturn(mockLambda).when(mockFactory).newInstance();
    ListTagsResult mockResult = mock(ListTagsResult.class);
    HashMap<String, String> expected = new HashMap<String, String>() {

        {
            put("t1", "foo");
            put("t2", "bar");
        }
    };
    doReturn(expected).when(mockResult).getTags();
    doReturn(mockResult).when(mockLambda).listTags(any());
    handler.lambdaClientFactory = mockFactory;
    handler.init(context);
    Map<String, String> actual = handler.monitor.getTagsMap();
    assertTrue(actual.entrySet().containsAll(expected.entrySet()));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) HashMap(java.util.HashMap) TestContext(com.nextdoor.bender.aws.TestContext) ListTagsResult(com.amazonaws.services.lambda.model.ListTagsResult) AWSLambdaClientFactory(com.nextdoor.bender.aws.AWSLambdaClientFactory) Test(org.junit.Test)

Example 3 with ListTagsResult

use of com.amazonaws.services.lambda.model.ListTagsResult in project bender by Nextdoor.

the class BaseHandlerTest method tagsDuplicate.

@Test
public void tagsDuplicate() throws HandlerException {
    BaseHandler.CONFIG_FILE = "/config/handler_config_tags_duplicate.json";
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:test_tags");
    AWSLambdaClientFactory mockFactory = mock(AWSLambdaClientFactory.class);
    AWSLambda mockLambda = mock(AWSLambda.class);
    doReturn(mockLambda).when(mockFactory).newInstance();
    ListTagsResult mockResult = mock(ListTagsResult.class);
    HashMap<String, String> functionTags = new HashMap<String, String>() {

        {
            put("f1", "foo");
            put("f2", "foo");
        }
    };
    doReturn(functionTags).when(mockResult).getTags();
    doReturn(mockResult).when(mockLambda).listTags(any());
    handler.lambdaClientFactory = mockFactory;
    handler.init(context);
    Map<String, String> actual = handler.monitor.getTagsMap();
    HashMap<String, String> expected = new HashMap<String, String>() {

        {
            put("f1", "foo");
            put("f2", "foo");
            put("u1", "bar");
        }
    };
    assertTrue(actual.entrySet().containsAll(expected.entrySet()));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) HashMap(java.util.HashMap) TestContext(com.nextdoor.bender.aws.TestContext) ListTagsResult(com.amazonaws.services.lambda.model.ListTagsResult) AWSLambdaClientFactory(com.nextdoor.bender.aws.AWSLambdaClientFactory) Test(org.junit.Test)

Example 4 with ListTagsResult

use of com.amazonaws.services.lambda.model.ListTagsResult in project bender by Nextdoor.

the class BaseHandler method init.

/**
 * Loads @{link com.nextdoor.bender.config.Configuration} from a resource file and initializes
 * classes.
 *
 * @param ctx function context as specified when function is invoked by lambda.
 * @throws HandlerException error while loading the @{link
 *         com.nextdoor.bender.config.Configuration}.
 */
public void init(Context ctx) throws HandlerException {
    /*
     * Function alias is the last part of the Function ARN
     */
    String alias = null;
    String[] tokens = ctx.getInvokedFunctionArn().split(":");
    if (tokens.length == 7) {
        alias = "$LATEST";
    } else if (tokens.length == 8) {
        alias = tokens[7];
    }
    BenderLayout.ALIAS = alias;
    BenderLayout.VERSION = ctx.getFunctionVersion();
    /*
     * Create a new monitor and then get a static copy of it
     */
    monitor = Monitor.getInstance();
    monitor.addTag("functionName", ctx.getFunctionName());
    monitor.addTag("functionVersion", alias);
    String configFile;
    /*
     * TODO: Replace this to always use env vars. Code was written prior to lambda env vars
     * existing.
     */
    if (System.getenv("BENDER_CONFIG") != null) {
        configFile = System.getenv("BENDER_CONFIG");
    } else if (CONFIG_FILE == null) {
        configFile = "/config/" + alias;
    } else {
        configFile = CONFIG_FILE;
    }
    logger.info(String.format("Bender Initializing (config: %s)", configFile));
    try {
        if (configFile.startsWith("s3://")) {
            config = BenderConfig.load(s3ClientFactory, new AmazonS3URI(configFile));
        } else if (configFile.startsWith("file://")) {
            File file = new File(configFile.replaceFirst("file://", ""));
            String string = FileUtils.readFileToString(file);
            config = BenderConfig.load(configFile, string);
        } else {
            config = BenderConfig.load(configFile);
        }
    } catch (ConfigurationException | IOException e) {
        throw new HandlerException("Error loading configuration: " + e.getMessage(), e);
    }
    HandlerResources handlerResources;
    try {
        handlerResources = new HandlerResources(config);
    } catch (ClassNotFoundException e) {
        throw new HandlerException("Unable to load resource: " + e.getMessage(), e);
    }
    /*
     * Add user tags
     */
    monitor.addTags(config.getHandlerConfig().getMetricTags());
    /*
     * Add Lambda function tags. These will override duplicate user tags.
     */
    if (config.getHandlerConfig().getIncludeFunctionTags()) {
        AWSLambda lambda = this.lambdaClientFactory.newInstance();
        ListTagsResult res = lambda.listTags(new ListTagsRequest().withResource(ctx.getInvokedFunctionArn()));
        monitor.addTagsMap(/*
           * Filter out tags that come from CloudFormation
           */
        res.getTags().entrySet().stream().filter(map -> !map.getKey().startsWith("aws:cloudformation")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
    }
    /*
     * Register reporters
     */
    monitor.addReporters(handlerResources.getReporters());
    /*
     * Init other things
     */
    wrapper = handlerResources.getWrapperFactory().newInstance();
    ser = handlerResources.getSerializerProcessor();
    setIpcService(new IpcSenderService(handlerResources.getTransportFactory()));
    sources = new ArrayList<Source>(handlerResources.getSources().values());
    queueSize = config.getHandlerConfig().getQueueSize();
    initialized = true;
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) AWSLambda(com.amazonaws.services.lambda.AWSLambda) IOException(java.io.IOException) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) Source(com.nextdoor.bender.config.Source) ListTagsRequest(com.amazonaws.services.lambda.model.ListTagsRequest) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) ListTagsResult(com.amazonaws.services.lambda.model.ListTagsResult) File(java.io.File) HandlerResources(com.nextdoor.bender.config.HandlerResources)

Aggregations

AWSLambda (com.amazonaws.services.lambda.AWSLambda)4 ListTagsResult (com.amazonaws.services.lambda.model.ListTagsResult)4 AWSLambdaClientFactory (com.nextdoor.bender.aws.AWSLambdaClientFactory)3 TestContext (com.nextdoor.bender.aws.TestContext)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ListTagsRequest (com.amazonaws.services.lambda.model.ListTagsRequest)1 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)1 ConfigurationException (com.nextdoor.bender.config.ConfigurationException)1 HandlerResources (com.nextdoor.bender.config.HandlerResources)1 Source (com.nextdoor.bender.config.Source)1 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)1 File (java.io.File)1 IOException (java.io.IOException)1