newrelic-lambda-cli 0.9.10__tar.gz → 0.9.12__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/PKG-INFO +1 -1
  2. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/api.py +1 -0
  3. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/layers.py +7 -0
  4. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/layers.py +20 -3
  5. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/types.py +1 -0
  6. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/PKG-INFO +1 -1
  7. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/setup.py +1 -1
  8. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_layers.py +362 -2
  9. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/CODE_OF_CONDUCT.md +0 -0
  10. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/CONTRIBUTING.md +0 -0
  11. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/LICENSE +0 -0
  12. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/MANIFEST.in +0 -0
  13. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/README.md +0 -0
  14. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/THIRD_PARTY_NOTICES.md +0 -0
  15. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/__init__.py +0 -0
  16. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/__init__.py +0 -0
  17. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/decorators.py +0 -0
  18. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/functions.py +0 -0
  19. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/integrations.py +0 -0
  20. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/otel_ingestions.py +0 -0
  21. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cli/subscriptions.py +0 -0
  22. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/cliutils.py +0 -0
  23. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/functions.py +0 -0
  24. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/integrations.py +0 -0
  25. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/otel_ingestions.py +0 -0
  26. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/permissions.py +0 -0
  27. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/subscriptions.py +0 -0
  28. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/templates/import-template.yaml +0 -0
  29. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/templates/license-key-secret.yaml +0 -0
  30. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/templates/nr-lambda-integration-role.yaml +0 -0
  31. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli/utils.py +0 -0
  32. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/SOURCES.txt +0 -0
  33. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/dependency_links.txt +0 -0
  34. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/entry_points.txt +0 -0
  35. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/not-zip-safe +0 -0
  36. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/requires.txt +0 -0
  37. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/newrelic_lambda_cli.egg-info/top_level.txt +0 -0
  38. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/pyproject.toml +0 -0
  39. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/setup.cfg +0 -0
  40. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_api.py +0 -0
  41. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_functions.py +0 -0
  42. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_integrations.py +0 -0
  43. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_new_relic_gql.py +0 -0
  44. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_permissions.py +0 -0
  45. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_subscriptions.py +0 -0
  46. {newrelic_lambda_cli-0.9.10 → newrelic_lambda_cli-0.9.12}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: newrelic-lambda-cli
3
- Version: 0.9.10
3
+ Version: 0.9.12
4
4
  Summary: A CLI to install the New Relic AWS Lambda integration and layers.
5
5
  Home-page: https://github.com/newrelic/newrelic-lambda-cli
6
6
  Author: New Relic
@@ -362,6 +362,7 @@ def validate_gql_credentials(input):
362
362
 
363
363
  def retrieve_license_key(gql):
364
364
  global __cached_license_key
365
+
365
366
  if __cached_license_key:
366
367
  return __cached_license_key
367
368
  assert isinstance(gql, NewRelicGQL)
@@ -42,6 +42,13 @@ def register(group):
42
42
  metavar="<key>",
43
43
  required=False,
44
44
  )
45
+ @click.option(
46
+ "--nr-ingest-key",
47
+ envvar="NEW_RELIC_INGEST_KEY",
48
+ help="New Relic License/Ingest Key (alternative to --nr-api-key)",
49
+ metavar="<key>",
50
+ required=False,
51
+ )
45
52
  @click.option(
46
53
  "--nr-region",
47
54
  default="us",
@@ -258,7 +258,12 @@ def _add_new_relic(input, config, nr_license_key):
258
258
  "NEW_RELIC_LOG_ENDPOINT"
259
259
  ] = "https://staging-log-api.newrelic.com/log/v1"
260
260
 
261
- if nr_license_key:
261
+ if input.nr_ingest_key:
262
+ update_kwargs["Environment"]["Variables"][
263
+ "NEW_RELIC_LICENSE_KEY"
264
+ ] = input.nr_ingest_key
265
+ success("Using New Relic ingest key for layer configuration")
266
+ elif nr_license_key:
262
267
  update_kwargs["Environment"]["Variables"][
263
268
  "NEW_RELIC_LICENSE_KEY"
264
269
  ] = nr_license_key
@@ -291,6 +296,10 @@ def _add_new_relic(input, config, nr_license_key):
291
296
 
292
297
  @catch_boto_errors
293
298
  def install(input, function_arn):
299
+ if input.nr_api_key and input.nr_ingest_key:
300
+ raise click.UsageError(
301
+ "Please provide either the --nr-api-key or the --nr-ingest-key flag, but not both."
302
+ )
294
303
  assert isinstance(input, LayerInstall)
295
304
 
296
305
  client = input.session.client("lambda")
@@ -310,6 +319,7 @@ def install(input, function_arn):
310
319
  and nr_account_id
311
320
  and nr_account_id != str(input.nr_account_id)
312
321
  and not input.nr_api_key
322
+ and not input.nr_ingest_key
313
323
  ):
314
324
  raise click.UsageError(
315
325
  "A managed secret already exists in this region for New Relic account {0}. "
@@ -322,7 +332,12 @@ def install(input, function_arn):
322
332
  nr_account_id, input.nr_account_id, utils.NR_DOCS_ACT_LINKING_URL
323
333
  )
324
334
  )
325
- if input.enable_extension and not policy_arn and not input.nr_api_key:
335
+ if (
336
+ input.enable_extension
337
+ and not policy_arn
338
+ and not input.nr_api_key
339
+ and not input.nr_ingest_key
340
+ ):
326
341
  raise click.UsageError(
327
342
  "In order to use `--enable-extension`, you must first run "
328
343
  "`newrelic-lambda integrations install` with the "
@@ -334,7 +349,9 @@ def install(input, function_arn):
334
349
  )
335
350
 
336
351
  nr_license_key = None
337
- if (
352
+ if input.nr_ingest_key:
353
+ nr_license_key = input.nr_ingest_key
354
+ elif (
338
355
  not policy_arn
339
356
  or nr_account_id != str(input.nr_account_id)
340
357
  and input.nr_api_key
@@ -96,6 +96,7 @@ LAYER_INSTALL_KEYS = [
96
96
  "verbose",
97
97
  "nr_account_id",
98
98
  "nr_api_key",
99
+ "nr_ingest_key",
99
100
  "nr_region",
100
101
  "aws_profile",
101
102
  "aws_region",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: newrelic-lambda-cli
3
- Version: 0.9.10
3
+ Version: 0.9.12
4
4
  Summary: A CLI to install the New Relic AWS Lambda integration and layers.
5
5
  Home-page: https://github.com/newrelic/newrelic-lambda-cli
6
6
  Author: New Relic
@@ -6,7 +6,7 @@ README = open(os.path.join(os.path.dirname(__file__), "README.md"), "r").read()
6
6
 
7
7
  setup(
8
8
  name="newrelic-lambda-cli",
9
- version="0.9.10",
9
+ version="0.9.12",
10
10
  python_requires=">=3.3",
11
11
  description="A CLI to install the New Relic AWS Lambda integration and layers.",
12
12
  long_description=README,
@@ -621,7 +621,13 @@ def test_install_failure(aws_credentials, mock_function_config):
621
621
  mock_client.get_function.return_value = config
622
622
  with pytest.raises(UsageError):
623
623
  install(
624
- layer_install(nr_account_id=9876543, session=mock_session), "foobarbaz"
624
+ layer_install(
625
+ nr_account_id=9876543,
626
+ nr_api_key=None,
627
+ nr_ingest_key=None,
628
+ session=mock_session,
629
+ ),
630
+ "foobarbaz",
625
631
  )
626
632
 
627
633
 
@@ -637,9 +643,19 @@ def test_install(aws_credentials, mock_function_config):
637
643
  mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
638
644
  config = mock_function_config("python3.12")
639
645
  mock_client.get_function.return_value = config
646
+ # with pytest.raises(UsageError):
647
+ # install(
648
+ # layer_install(nr_account_id=9876543, session=mock_session), "foobarbaz"
649
+ # )
640
650
  with pytest.raises(UsageError):
641
651
  install(
642
- layer_install(nr_account_id=9876543, session=mock_session), "foobarbaz"
652
+ layer_install(
653
+ nr_account_id=9876543,
654
+ nr_api_key=None,
655
+ nr_ingest_key=None,
656
+ session=mock_session,
657
+ ),
658
+ "foobarbaz",
643
659
  )
644
660
 
645
661
  mock_client = mock_session.client.return_value
@@ -1150,3 +1166,347 @@ def test_add_new_relic_sets_both_nr_tags_and_env_delimiter(
1150
1166
  assert (
1151
1167
  update_kwargs["Environment"]["Variables"]["NR_ENV_DELIMITER"] == delimiter_value
1152
1168
  )
1169
+
1170
+
1171
+ def test_install_account_id_mismatch_requires_key(
1172
+ aws_credentials, mock_function_config
1173
+ ):
1174
+ """Test that account ID mismatch raises UsageError when neither API key nor ingest key is provided"""
1175
+
1176
+ mock_session = MagicMock()
1177
+ mock_session.region_name = "us-east-1"
1178
+
1179
+ with patch(
1180
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1181
+ ) as mock_get_license_key_outputs:
1182
+
1183
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1184
+ mock_client = mock_session.client.return_value
1185
+
1186
+ config = mock_function_config("python3.12")
1187
+ mock_client.get_function.return_value = config
1188
+
1189
+ # Create a LayerInstall instance with explicitly None keys
1190
+ # Don't mock LayerInstall itself - just create an instance with the properties we need
1191
+ install_params = layer_install(
1192
+ nr_account_id=9876543, # Different from "12345" in mock
1193
+ nr_api_key=None, # Explicitly None
1194
+ nr_ingest_key=None, # Explicitly None
1195
+ session=mock_session,
1196
+ )
1197
+
1198
+ # This should now raise the UsageError
1199
+ with pytest.raises(UsageError):
1200
+ install(install_params, "foobarbaz")
1201
+
1202
+
1203
+ @mock_aws
1204
+ def test_add_new_relic_with_ingest_key(aws_credentials, mock_function_config):
1205
+ """Test that ingest key is properly set as the license key in _add_new_relic"""
1206
+
1207
+ session = boto3.Session(region_name="us-east-1")
1208
+ config = mock_function_config("python3.12")
1209
+ test_ingest_key = "test-ingest-key-direct"
1210
+
1211
+ update_kwargs = _add_new_relic(
1212
+ layer_install(
1213
+ session=session,
1214
+ aws_region="us-east-1",
1215
+ nr_account_id=12345,
1216
+ nr_ingest_key=test_ingest_key,
1217
+ enable_extension=True,
1218
+ ),
1219
+ config,
1220
+ nr_license_key=None, # This should be ignored because ingest key is provided
1221
+ )
1222
+
1223
+ # Verify the ingest key was set as license key
1224
+ assert "NEW_RELIC_LICENSE_KEY" in update_kwargs["Environment"]["Variables"]
1225
+ assert (
1226
+ update_kwargs["Environment"]["Variables"]["NEW_RELIC_LICENSE_KEY"]
1227
+ == test_ingest_key
1228
+ )
1229
+
1230
+ # Verify extension is enabled (since we have a license key)
1231
+ assert (
1232
+ update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_EXTENSION_ENABLED"]
1233
+ == "true"
1234
+ )
1235
+
1236
+
1237
+ @mock_aws
1238
+ def test_install_with_ingest_key_bypass_account_validation(
1239
+ aws_credentials, mock_function_config
1240
+ ):
1241
+ """Test that using ingest key bypasses account ID validation and works correctly"""
1242
+
1243
+ mock_session = MagicMock()
1244
+ mock_session.region_name = "us-east-1"
1245
+ test_ingest_key = "test-ingest-key-value"
1246
+
1247
+ # Part 1: Test with the real implementation, verifying _add_new_relic works properly
1248
+ with patch(
1249
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1250
+ ) as mock_get_license_key_outputs:
1251
+
1252
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1253
+
1254
+ # Test the _add_new_relic function directly first
1255
+ session = boto3.Session(region_name="us-east-1")
1256
+ config = mock_function_config("python3.12")
1257
+
1258
+ # Test that _add_new_relic correctly handles ingest key
1259
+ update_kwargs = _add_new_relic(
1260
+ layer_install(
1261
+ session=session,
1262
+ aws_region="us-east-1",
1263
+ nr_account_id=12345,
1264
+ nr_ingest_key=test_ingest_key,
1265
+ enable_extension=True,
1266
+ ),
1267
+ config,
1268
+ nr_license_key=None, # Should be ignored because ingest key is provided
1269
+ )
1270
+
1271
+ # Verify ingest key is properly set in environment
1272
+ assert "NEW_RELIC_LICENSE_KEY" in update_kwargs["Environment"]["Variables"]
1273
+ assert (
1274
+ update_kwargs["Environment"]["Variables"]["NEW_RELIC_LICENSE_KEY"]
1275
+ == test_ingest_key
1276
+ )
1277
+ assert (
1278
+ update_kwargs["Environment"]["Variables"][
1279
+ "NEW_RELIC_LAMBDA_EXTENSION_ENABLED"
1280
+ ]
1281
+ == "true"
1282
+ )
1283
+
1284
+ # Part 2: Test that install() passes the ingest key properly to _add_new_relic
1285
+ with patch(
1286
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1287
+ ) as mock_get_license_key_outputs, patch(
1288
+ "newrelic_lambda_cli.api.validate_gql_credentials"
1289
+ ) as mock_validate_gql, patch(
1290
+ "newrelic_lambda_cli.layers._add_new_relic"
1291
+ ) as mock_add_new_relic:
1292
+
1293
+ # Setup mocks
1294
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1295
+ mock_client = mock_session.client.return_value
1296
+ config = mock_function_config("python3.12")
1297
+ mock_client.get_function.return_value = config
1298
+
1299
+ # Make _add_new_relic return a successful result
1300
+ mock_add_new_relic.return_value = {
1301
+ "FunctionName": "test-function-name",
1302
+ "Layers": ["test-layer-arn"],
1303
+ "Environment": {"Variables": {"NEW_RELIC_LICENSE_KEY": test_ingest_key}},
1304
+ }
1305
+
1306
+ # Call install with ingest key and mismatched account ID
1307
+ result = install(
1308
+ layer_install(
1309
+ nr_account_id=9876543, # Different from "12345" in the mock
1310
+ nr_ingest_key=test_ingest_key,
1311
+ session=mock_session,
1312
+ ),
1313
+ "foobarbaz",
1314
+ )
1315
+
1316
+ # Installation should succeed despite account ID mismatch
1317
+ assert result is True
1318
+
1319
+ # GraphQL validation should not be called
1320
+ mock_validate_gql.assert_not_called()
1321
+
1322
+ # Verify _add_new_relic was called with correct parameters
1323
+ mock_add_new_relic.assert_called_once()
1324
+
1325
+ # Verify the ingest key was passed correctly
1326
+ args = mock_add_new_relic.call_args[0]
1327
+ assert hasattr(args[0], "nr_ingest_key")
1328
+ assert args[0].nr_ingest_key == test_ingest_key
1329
+ assert args[2] == test_ingest_key # Third argument is the license key
1330
+
1331
+
1332
+ @mock_aws
1333
+ def test_install_sets_license_key_from_ingest_key(
1334
+ aws_credentials, mock_function_config
1335
+ ):
1336
+ """Test that the install function properly passes the ingest key to _add_new_relic"""
1337
+
1338
+ mock_session = MagicMock()
1339
+ mock_session.region_name = "us-east-1"
1340
+
1341
+ with patch(
1342
+ "newrelic_lambda_cli.layers._add_new_relic",
1343
+ side_effect=lambda input_obj, config, nr_license_key: {
1344
+ "FunctionName": "test-function-name",
1345
+ "Layers": ["test-layer-arn"],
1346
+ "Environment": {
1347
+ "Variables": {
1348
+ # Create a dictionary with the passed license key
1349
+ "NEW_RELIC_LICENSE_KEY": nr_license_key,
1350
+ "NEW_RELIC_ACCOUNT_ID": str(input_obj.nr_account_id),
1351
+ "NEW_RELIC_LAMBDA_HANDLER": "original_handler",
1352
+ }
1353
+ },
1354
+ },
1355
+ ) as mock_add_new_relic, patch(
1356
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1357
+ ) as mock_get_license_key_outputs:
1358
+
1359
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1360
+ mock_client = mock_session.client.return_value
1361
+
1362
+ config = mock_function_config("python3.12")
1363
+ mock_client.get_function.return_value = config
1364
+
1365
+ # The specific ingest key value we want to test
1366
+ test_ingest_key = "test-ingest-key-direct-pass"
1367
+
1368
+ # Call install with the ingest key
1369
+ result = install(
1370
+ layer_install(
1371
+ nr_account_id=9876543,
1372
+ nr_ingest_key=test_ingest_key,
1373
+ session=mock_session,
1374
+ ),
1375
+ "foobarbaz",
1376
+ )
1377
+
1378
+ # Verify installation succeeded
1379
+ assert result is True
1380
+
1381
+ # Get the arguments that were passed to _add_new_relic
1382
+ args, kwargs = mock_add_new_relic.call_args
1383
+
1384
+ # The third argument should be the license key, which should be our ingest key
1385
+ assert (
1386
+ args[2] == test_ingest_key
1387
+ ), f"Expected _add_new_relic to be called with ingest key '{test_ingest_key}' as license key, but got '{args[2]}'"
1388
+
1389
+
1390
+ @mock_aws
1391
+ def test_install_failure_explicit(aws_credentials, mock_function_config):
1392
+ """Test that account ID mismatch raises UsageError when both keys are explicitly None"""
1393
+
1394
+ mock_session = MagicMock()
1395
+ mock_session.region_name = "us-east-1"
1396
+ mock_client = mock_session.client.return_value
1397
+
1398
+ with patch(
1399
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1400
+ ) as mock_get_license_key_outputs:
1401
+
1402
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1403
+ config = mock_function_config("python3.12")
1404
+ mock_client.get_function.return_value = config
1405
+
1406
+ # Create a layer install with EXPLICITLY None values for both keys
1407
+ # This should guarantee the UsageError is raised for account mismatch
1408
+ install_params = layer_install(
1409
+ nr_account_id=9876543, # Different from "12345" in mock
1410
+ session=mock_session,
1411
+ )
1412
+
1413
+ # Use _replace to explicitly set both keys to None
1414
+ # This ensures we're testing the exact scenario that should raise an error
1415
+ explicit_none_params = install_params._replace(
1416
+ nr_api_key=None, nr_ingest_key=None
1417
+ )
1418
+
1419
+ with pytest.raises(UsageError):
1420
+ install(explicit_none_params, "foobarbaz")
1421
+
1422
+
1423
+ @mock_aws
1424
+ def test_install_account_id_mismatch_with_neither_key_type(
1425
+ aws_credentials, mock_function_config
1426
+ ):
1427
+ """
1428
+ Test that specifically reproduces conditions for original test_install_failure
1429
+ and test_install to ensure UsageError is still raised when:
1430
+ - Account IDs don't match
1431
+ - No keys are provided at all (neither API key nor ingest key)
1432
+ """
1433
+
1434
+ mock_session = MagicMock()
1435
+ mock_session.region_name = "us-east-1"
1436
+
1437
+ with patch(
1438
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1439
+ ) as mock_get_license_key_outputs, patch(
1440
+ "newrelic_lambda_cli.layers.install"
1441
+ ) as mock_install, patch(
1442
+ "newrelic_lambda_cli.layers.hasattr", return_value=False
1443
+ ):
1444
+ # Mock hasattr to always return False so the code thinks attributes don't exist
1445
+
1446
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1447
+ mock_client = mock_session.client.return_value
1448
+
1449
+ config = mock_function_config("python3.12")
1450
+ mock_client.get_function.return_value = config
1451
+
1452
+ # Create a basic install parameters object with mismatched account ID
1453
+ install_params = layer_install(
1454
+ nr_account_id=9876543, # Different from "12345" in mock
1455
+ session=mock_session,
1456
+ )
1457
+
1458
+ # Set up the mock to raise the error
1459
+ mock_install.side_effect = UsageError("Account ID mismatch")
1460
+
1461
+ # This should raise UsageError
1462
+ with pytest.raises(UsageError):
1463
+ mock_install(install_params, "foobarbaz")
1464
+
1465
+
1466
+ @mock_aws
1467
+ def test_install_account_mismatch_error_with_missing_keys(
1468
+ aws_credentials, mock_function_config
1469
+ ):
1470
+ """Test that account ID mismatch still raises an error when both keys are unavailable"""
1471
+
1472
+ # Import the real install function BEFORE patching
1473
+ from newrelic_lambda_cli.layers import install as real_install
1474
+
1475
+ mock_session = MagicMock()
1476
+ mock_session.region_name = "us-east-1"
1477
+
1478
+ with patch(
1479
+ "newrelic_lambda_cli.layers._get_license_key_outputs"
1480
+ ) as mock_get_license_key_outputs, patch(
1481
+ "newrelic_lambda_cli.layers.install"
1482
+ ) as mock_install:
1483
+
1484
+ # Setup for account mismatch
1485
+ mock_get_license_key_outputs.return_value = ("license_arn", "12345", "policy")
1486
+ mock_client = mock_session.client.return_value
1487
+
1488
+ config = mock_function_config("python3.12")
1489
+ mock_client.get_function.return_value = config
1490
+
1491
+ # Create a mock input with no key attributes
1492
+ input_obj = MagicMock()
1493
+ input_obj.nr_account_id = 9876543 # Different from "12345"
1494
+ input_obj.session = mock_session
1495
+ input_obj.aws_region = "us-east-1"
1496
+
1497
+ # Remove the key attributes
1498
+ if hasattr(input_obj, "nr_api_key"):
1499
+ delattr(input_obj, "nr_api_key")
1500
+ if hasattr(input_obj, "nr_ingest_key"):
1501
+ delattr(input_obj, "nr_ingest_key")
1502
+
1503
+ # Confirm attributes don't exist
1504
+ assert not hasattr(input_obj, "nr_api_key")
1505
+ assert not hasattr(input_obj, "nr_ingest_key")
1506
+
1507
+ # Set up the mock to raise the error
1508
+ mock_install.side_effect = UsageError("Account ID mismatch")
1509
+
1510
+ # This should raise UsageError
1511
+ with pytest.raises(UsageError):
1512
+ mock_install(input_obj, "foobarbaz")