Exemplo n.º 1
0
def _remove_new_relic(config, region):
    runtime = config["Configuration"]["Runtime"]
    if runtime not in utils.RUNTIME_CONFIG:
        failure(
            "Unsupported Lambda runtime for '%s': %s"
            % (config["Configuration"]["FunctionArn"], runtime)
        )
        return

    handler = config["Configuration"]["Handler"]

    # Detect non-New Relic handler and error if necessary.
    if not utils.is_valid_handler(runtime, handler):
        failure(
            "New Relic installation (via layers) not auto-detected for the specified "
            "function '%s'. Unrecognized handler in deployed function."
            % config["Configuration"]["FunctionArn"]
        )
        return

    env_handler = (
        config["Configuration"]
        .get("Environment", {})
        .get("Variables", {})
        .get("NEW_RELIC_LAMBDA_HANDLER")
    )

    if not env_handler:
        failure(
            "New Relic installation (via layers) not auto-detected for the specified "
            "function '%s'. Environment variable NEW_RELIC_LAMBDA_HANDLER not found."
            % config["Configuration"]["FunctionArn"]
        )
        return

    # Delete New Relic env vars
    config["Configuration"]["Environment"]["Variables"] = {
        key: value
        for key, value in config["Configuration"]
        .get("Environment", {})
        .get("Variables", {})
        .items()
        if not key.startswith("NEW_RELIC")
    }

    # Remove New Relic layers
    layers = [
        layer["Arn"]
        for layer in config["Configuration"].get("Layers")
        if not layer["Arn"].startswith(utils.get_arn_prefix(region))
    ]

    return {
        "FunctionName": config["Configuration"]["FunctionArn"],
        "Handler": env_handler,
        "Environment": config["Configuration"]["Environment"],
        "Layers": layers,
    }
Exemplo n.º 2
0
def _remove_new_relic(input, config):
    assert isinstance(input, LayerUninstall)

    aws_region = input.session.region_name

    runtime = config["Configuration"]["Runtime"]
    if runtime not in utils.RUNTIME_CONFIG:
        failure("Unsupported Lambda runtime for '%s': %s" %
                (config["Configuration"]["FunctionArn"], runtime))
        return True

    handler = config["Configuration"]["Handler"]

    # For java runtimes we need to remove the method name before
    # validating because method names are variable
    if "java" in runtime:
        handler = handler.split("::", 1)[0] + "::"

    # Detect non-New Relic handler and error if necessary.
    if not utils.is_valid_handler(runtime, handler):
        failure(
            "New Relic installation (via layers) not auto-detected for the specified "
            "function '%s'. Unrecognized handler in deployed function." %
            config["Configuration"]["FunctionArn"])
        return False

    env_handler = (config["Configuration"].get("Environment", {}).get(
        "Variables", {}).get("NEW_RELIC_LAMBDA_HANDLER"))

    # Delete New Relic env vars
    config["Configuration"]["Environment"]["Variables"] = {
        key: value
        for key, value in config["Configuration"].get("Environment", {}).get(
            "Variables", {}).items() if key not in NEW_RELIC_ENV_VARS
    }

    # Remove New Relic layers
    layers = [
        layer["Arn"] for layer in config["Configuration"].get("Layers")
        if not layer["Arn"].startswith(utils.get_arn_prefix(aws_region))
    ]

    return {
        "FunctionName":
        config["Configuration"]["FunctionArn"],
        "Handler":
        env_handler if env_handler else config["Configuration"]["Handler"],
        "Environment":
        config["Configuration"]["Environment"],
        "Layers":
        layers,
    }
Exemplo n.º 3
0
def test_is_valid_handler():
    assert is_valid_handler("fakeruntime", "not.a.valid.handler") is False
    assert is_valid_handler("python3.8",
                            "newrelic_lambda_wrapper.handler") is True