Skip to content

Commit e75574b

Browse files
feat: Implement backward compatibility in layers update. (#350)
* feat: Implement backward compatibility in layers update.
1 parent a25eccd commit e75574b

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

newrelic_lambda_cli/layers.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@ def index(region, runtime, architecture):
4747
]
4848

4949

50-
def layer_selection(available_layers, runtime, architecture):
50+
def layer_selection(
51+
available_layers, runtime, architecture, upgrade=False, existing_layer_arn=None
52+
):
53+
if upgrade and existing_layer_arn:
54+
base_arn = existing_layer_arn.rsplit(":", 1)[0]
55+
56+
for i, layer in enumerate(available_layers):
57+
candidate_arn = layer["LatestMatchingVersion"]["LayerVersionArn"]
58+
candidate_base_arn = candidate_arn.rsplit(":", 1)[0]
59+
if candidate_base_arn == base_arn:
60+
return candidate_arn
61+
5162
if len(available_layers) == 1:
5263
return available_layers[0]["LatestMatchingVersion"]["LayerVersionArn"]
5364

@@ -147,8 +158,16 @@ def _add_new_relic(input, config, nr_license_key):
147158
% (config["Configuration"]["FunctionArn"], runtime, architecture)
148159
)
149160
return False
150-
151-
new_relic_layer = layer_selection(available_layers, runtime, architecture)
161+
existing_layer_arn = (
162+
existing_newrelic_layer[0] if existing_newrelic_layer else None
163+
)
164+
new_relic_layer = layer_selection(
165+
available_layers,
166+
runtime,
167+
architecture,
168+
upgrade=input.upgrade,
169+
existing_layer_arn=existing_layer_arn,
170+
)
152171

153172
update_kwargs = {
154173
"FunctionName": config["Configuration"]["FunctionArn"],

tests/test_layers.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ def test_add_new_relic(aws_credentials, mock_function_config):
143143
)
144144

145145
layer_selection_mock.assert_called_with(
146-
mock_index.return_value, "java11", "x86_64"
146+
mock_index.return_value,
147+
"java11",
148+
"x86_64",
149+
upgrade=None,
150+
existing_layer_arn=None,
147151
)
148152
assert "original_handler" in config["Configuration"]["Handler"]
149153

@@ -417,7 +421,6 @@ def test_add_new_relic_nodejs(aws_credentials, mock_function_config):
417421

418422
runtime = "nodejs20.x"
419423

420-
# --- Scenario 1: Standard Node.js Handler (ESM disabled) ---
421424
print(f"\nTesting Node.js ({runtime}) Standard Handler...")
422425
original_std_handler = "original_handler"
423426
config_std = mock_function_config(runtime)
@@ -430,11 +433,16 @@ def test_add_new_relic_nodejs(aws_credentials, mock_function_config):
430433
enable_extension_function_logs=True,
431434
)
432435

433-
update_kwargs_std = _add_new_relic(
434-
install_opts_std,
435-
config_std,
436-
nr_license_key=nr_license_key,
437-
)
436+
with patch("sys.stdout.isatty") as mock_isatty, patch(
437+
"newrelic_lambda_cli.layers.click.prompt"
438+
) as mock_prompt:
439+
mock_isatty.return_value = True
440+
mock_prompt.return_value = 0
441+
update_kwargs_std = _add_new_relic(
442+
install_opts_std,
443+
config_std,
444+
nr_license_key=nr_license_key,
445+
)
438446

439447
assert update_kwargs_std is not False, "Expected update_kwargs, not False"
440448
assert (
@@ -479,11 +487,16 @@ def test_add_new_relic_nodejs(aws_credentials, mock_function_config):
479487
esm=True,
480488
)
481489

482-
update_kwargs_esm = _add_new_relic(
483-
install_opts_esm,
484-
config_esm,
485-
nr_license_key=nr_license_key,
486-
)
490+
with patch("sys.stdout.isatty") as mock_isatty, patch(
491+
"newrelic_lambda_cli.layers.click.prompt"
492+
) as mock_prompt:
493+
mock_isatty.return_value = True
494+
mock_prompt.return_value = 0
495+
update_kwargs_esm = _add_new_relic(
496+
install_opts_esm,
497+
config_esm,
498+
nr_license_key=nr_license_key,
499+
)
487500

488501
assert update_kwargs_esm is not False, "Expected update_kwargs, not False"
489502
assert (

0 commit comments

Comments
 (0)