Skip to content

Commit

Permalink
Use new log ingestion name and fallback to old
Browse files Browse the repository at this point in the history
  • Loading branch information
hmstepanek committed Jan 5, 2024
1 parent 7747959 commit 476b407
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
2 changes: 1 addition & 1 deletion newrelic_lambda_cli/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_aliased_functions(input):
function
for function in input.functions
if function.lower()
not in ("all", "installed", "not-installed", "newrelic-log-ingestion")
not in ("all", "installed", "not-installed") and "newrelic-log-ingestion" not in function.lower()
and function not in input.excludes
]

Expand Down
88 changes: 70 additions & 18 deletions newrelic_lambda_cli/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,56 @@ def _get_cf_stack_status(session, stack_name, nr_account_id=None):
else:
return res["Stacks"][0]["StackStatus"]

def get_unique_newrelic_log_ingestion_name(session):
stack_id = _get_cf_stack_id(session, stack_name=INGEST_STACK_NAME)
return "newrelic-log-ingestion-%s"%(stack_id.split("/")[2].split("-")[4])

def get_newrelic_log_ingestion_function(session):
unique_log_ingestion_name = get_unique_newrelic_log_ingestion_name(session)
old_log_ingestion_name = "newrelic-log-ingestion"

function = get_function(session, unique_log_ingestion_name)
if function is None:
function = get_function(session, old_log_ingestion_name)
return function

def _get_cf_stack_id(session, stack_name, nr_account_id=None):
"""Returns the StackId of the CloudFormation stack if it exists"""
try:
res = session.client("cloudformation").describe_stacks(StackName=stack_name)
if nr_account_id is not None:
stack_output_account_id = _get_stack_output_value(
session, ["NrAccountId"]
).get("NrAccountId")
# Checking outputs here to protect against installs done
# with older CLI versions. We don't want to constantly warn users
# who installed on previous versions with no outputs.
if stack_output_account_id and stack_output_account_id != str(
nr_account_id
):
warning(
"WARNING: Managed secret already exists in this region for "
"New Relic account {0}.\n"
"Current CLI behavior limits the setup of one managed "
"secret per region.\n"
"To set up an additional secret for New Relic account {1} "
"see our docs:\n{2}.\n"
"Or run this command with --disable-license-key-secret to "
"avoid attempting to create a new managed secret.".format(
stack_output_account_id, nr_account_id, NR_DOCS_ACT_LINKING_URL
)
)
except botocore.exceptions.ClientError as e:
if (
e.response
and "ResponseMetadata" in e.response
and "HTTPStatusCode" in e.response["ResponseMetadata"]
and e.response["ResponseMetadata"]["HTTPStatusCode"] in (400, 404)
):
return None
raise click.UsageError(str(e))
else:
return res["Stacks"][0]["StackId"]

# TODO: Merge this with create_integration_role?
def _create_role(input):
Expand Down Expand Up @@ -200,6 +250,8 @@ def _import_log_ingestion_function(input, nr_license_key):
)

with open(template_path) as template:
unique_log_ingestion_name = get_unique_newrelic_log_ingestion_name(input.session)

change_set_name = "%s-IMPORT-%d" % (INGEST_STACK_NAME, int(time.time()))
click.echo("Creating change set: %s" % change_set_name)

Expand All @@ -217,7 +269,7 @@ def _import_log_ingestion_function(input, nr_license_key):
{
"ResourceType": "AWS::Lambda::Function",
"LogicalResourceId": "NewRelicLogIngestionFunctionNoCap",
"ResourceIdentifier": {"FunctionName": "newrelic-log-ingestion"},
"ResourceIdentifier": {"FunctionName": unique_log_ingestion_name},
}
],
)
Expand Down Expand Up @@ -342,8 +394,9 @@ def update_log_ingestion_function(input):

# We can't change props during import, so let's set them to their current values
lambda_client = input.session.client("lambda")
unique_log_ingestion_name = get_unique_newrelic_log_ingestion_name(input.session)
old_props = lambda_client.get_function_configuration(
FunctionName="newrelic-log-ingestion"
FunctionName=unique_log_ingestion_name
)
old_role_name = old_props["Role"].split("/")[-1]
old_nr_license_key = old_props["Environment"]["Variables"]["LICENSE_KEY"]
Expand Down Expand Up @@ -517,12 +570,12 @@ def install_log_ingestion(
Returns True for success and False for failure.
"""
assert isinstance(input, IntegrationInstall)
function = get_function(input.session, "newrelic-log-ingestion")
function = get_newrelic_log_ingestion_function(input.session)
if function is None:
stack_status = _check_for_ingest_stack(input.session)
if stack_status is None:
click.echo(
"Setting up 'newrelic-log-ingestion' function in region: %s"
"Setting up newrelic-log-ingestion function in region: %s"
% input.session.region_name
)
try:
Expand All @@ -531,7 +584,7 @@ def install_log_ingestion(
nr_license_key,
)
except Exception as e:
failure("Failed to create 'newrelic-log-ingestion' function: %s" % e)
failure("Failed to create newrelic-log-ingestion function: %s" % e)
return False
else:
failure(
Expand All @@ -543,12 +596,11 @@ def install_log_ingestion(
return False
else:
success(
"The 'newrelic-log-ingestion' function already exists in region %s, "
"The newrelic-log-ingestion function already exists in region %s, "
"skipping" % input.session.region_name
)
return True


@catch_boto_errors
def update_log_ingestion(input):
"""
Expand All @@ -558,15 +610,6 @@ def update_log_ingestion(input):
"""
assert isinstance(input, IntegrationUpdate)

function = get_function(input.session, "newrelic-log-ingestion")
if function is None:
failure(
"No 'newrelic-log-ingestion' function in region '%s'. "
"Run 'newrelic-lambda integrations install' to install it."
% input.session.region_name
)
return False

stack_status = _check_for_ingest_stack(input.session)
if stack_status is None:
failure(
Expand All @@ -578,10 +621,19 @@ def update_log_ingestion(input):
)
return False

function = get_newrelic_log_ingestion_function(input.session)
if function is None:
failure(
"No newrelic-log-ingestion function in region '%s'. "
"Run 'newrelic-lambda integrations install' to install it."
% input.session.region_name
)
return False

try:
update_log_ingestion_function(input)
except Exception as e:
failure("Failed to update 'newrelic-log-ingestion' function: %s" % e)
failure("Failed to update newrelic-log-ingestion function: %s" % e)
return False
else:
return True
Expand All @@ -592,7 +644,7 @@ def get_log_ingestion_license_key(session):
"""
Fetches the license key value from the log ingestion function
"""
function = get_function(session, "newrelic-log-ingestion")
function = get_newrelic_log_ingestion_function(session)
if function:
return function["Configuration"]["Environment"]["Variables"]["LICENSE_KEY"]
return None
Expand Down
5 changes: 3 additions & 2 deletions newrelic_lambda_cli/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from newrelic_lambda_cli.cliutils import failure, success, warning
from newrelic_lambda_cli.functions import get_function
from newrelic_lambda_cli.integrations import get_newrelic_log_ingestion_function
from newrelic_lambda_cli.types import (
LayerInstall,
SubscriptionInstall,
Expand Down Expand Up @@ -83,10 +84,10 @@ def _remove_subscription_filter(session, function_name, filter_name):
@catch_boto_errors
def create_log_subscription(input, function_name):
assert isinstance(input, SubscriptionInstall)
destination = get_function(input.session, "newrelic-log-ingestion")
destination = get_newrelic_log_ingestion_function(input.session)
if destination is None:
failure(
"Could not find 'newrelic-log-ingestion' function. Is the New Relic AWS "
"Could not find newrelic-log-ingestion function. Is the New Relic AWS "
"integration installed?"
)
return False
Expand Down
4 changes: 2 additions & 2 deletions newrelic_lambda_cli/templates/import-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Resources:
Key: 466768951184/arn:aws:serverlessrepo:us-east-1:463657938898:applications-NewRelic-log-ingestion-versions-2.2.1/2dcb4087-186a-42c8-bc1f-cf93780786c0
Description: Sends log data from CloudWatch Logs and S3 to New Relic Infrastructure (Cloud integrations) and New Relic Logging
Handler: function.lambda_handler
FunctionName: newrelic-log-ingestion
FunctionName: !Join ['-', ['newrelic-log-ingestion', !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref AWS::StackId]]]]]]
MemorySize:
Ref: MemorySize
Runtime: python3.9
Expand All @@ -84,7 +84,7 @@ Resources:
Key: 466768951184/arn:aws:serverlessrepo:us-east-1:463657938898:applications-NewRelic-log-ingestion-versions-2.2.1/2dcb4087-186a-42c8-bc1f-cf93780786c0
Description: Sends log data from CloudWatch Logs and S3 to New Relic Infrastructure (Cloud integrations) and New Relic Logging
Handler: function.lambda_handler
FunctionName: newrelic-log-ingestion
FunctionName: !Join ['-', ['newrelic-log-ingestion', !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref AWS::StackId]]]]]]
MemorySize:
Ref: MemorySize
Runtime: python3.9
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="newrelic-lambda-cli",
version="0.7.6",
version="0.7.7",
python_requires=">=3.3",
description="A CLI to install the New Relic AWS Lambda integration and layers.",
long_description=README,
Expand Down

0 comments on commit 476b407

Please sign in to comment.