Skip to content

Commit aa3c766

Browse files
committed
refactor for dps
1 parent c8e6ea1 commit aa3c766

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

infrastructure/instance/file_name_processor.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ resource "aws_lambda_function" "file_processor_lambda" {
290290

291291
environment {
292292
variables = {
293-
ACCOUNT_ID = var.dspp_core_account_id
293+
ACCOUNT_ID = var.immunisation_account_id
294+
DPS_ACCOUNT_ID = var.dspp_core_account_id
294295
SOURCE_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
295296
ACK_BUCKET_NAME = aws_s3_bucket.batch_data_destination_bucket.bucket
296297
DPS_BUCKET_NAME = "nhsd-dspp-core-ref-extended-attributes-gdp"

lambdas/filenameprocessor/src/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
# We have used an internal temporary bucket here and an actual dps bucket will replace this
1616
DPS_DESTINATION_BUCKET_NAME = os.getenv("DPS_BUCKET_NAME")
17-
EXPECTED_BUCKET_OWNER_ACCOUNT = os.getenv("ACCOUNT_ID")
17+
EXPECTED_SOURCE_BUCKET_ACCOUNT = os.getenv("ACCOUNT_ID")
18+
EXPECTED_DPS_DESTINATION_ACCOUNT = os.getenv("DPS_ACCOUNT_ID")
1819
AUDIT_TABLE_NAME = os.getenv("AUDIT_TABLE_NAME")
1920
AUDIT_TABLE_TTL_DAYS = os.getenv("AUDIT_TABLE_TTL_DAYS")
2021
VALID_VERSIONS = ["V5"]

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
DPS_DESTINATION_BUCKET_NAME,
2222
DPS_DESTINATION_PREFIX,
2323
ERROR_TYPE_TO_STATUS_CODE_MAP,
24-
EXPECTED_BUCKET_OWNER_ACCOUNT,
24+
EXPECTED_DPS_DESTINATION_ACCOUNT,
25+
EXPECTED_SOURCE_BUCKET_ACCOUNT,
2526
EXTENDED_ATTRIBUTES_ARCHIVE_PREFIX,
2627
EXTENDED_ATTRIBUTES_FILE_PREFIX,
2728
SOURCE_BUCKET_NAME,
@@ -270,8 +271,8 @@ def handle_extended_attributes_file(
270271
file_key,
271272
DPS_DESTINATION_BUCKET_NAME,
272273
dest_file_key,
273-
EXPECTED_BUCKET_OWNER_ACCOUNT,
274-
EXPECTED_BUCKET_OWNER_ACCOUNT,
274+
EXPECTED_DPS_DESTINATION_ACCOUNT,
275+
EXPECTED_SOURCE_BUCKET_ACCOUNT,
275276
)
276277

277278
move_file(bucket_name, file_key, f"{EXTENDED_ATTRIBUTES_ARCHIVE_PREFIX}/{file_key}")

lambdas/filenameprocessor/tests/test_lambda_handler.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ def test_lambda_handler_extended_attributes_success(self, mock_get_redis_client)
270270
mock_redis.hkeys = Mock(return_value=["COVID", *all_vaccine_types_in_this_test_file])
271271
mock_get_redis_client.return_value = mock_redis
272272

273+
# Ensure DPS destination bucket exists in moto so copy succeeds
274+
try:
275+
s3_client.create_bucket(Bucket=BucketNames.DPS_DESTINATION)
276+
except Exception:
277+
pass
278+
273279
# Patch uuid4 (message id), and prevent external copy issues by simulating move
274280
with (
275281
patch("file_name_processor.validate_permissions_for_extended_attributes_files", return_value="X8E5B_COVID"),
@@ -288,10 +294,15 @@ def test_lambda_handler_extended_attributes_success(self, mock_get_redis_client)
288294
self.assertEqual(item[AuditTableKeys.STATUS]["S"], "Processed")
289295
self.assertEqual(item[AuditTableKeys.TIMESTAMP]["S"], test_cases[0].created_at_formatted_string)
290296
self.assertEqual(item[AuditTableKeys.EXPIRES_AT]["N"], str(test_cases[0].expires_at))
291-
# File should be moved to destination/
292-
dest_key = f"dps_destination/{test_cases[0].file_key}"
293-
retrieved = s3_client.get_object(Bucket=BucketNames.DESTINATION, Key=dest_key)
294-
self.assertIsNotNone(retrieved)
297+
# File should be moved to source extended-attributes-archive/<file_key>
298+
archived_key = f"extended-attributes-archive/{test_cases[0].file_key}"
299+
archived_obj = s3_client.get_object(Bucket=BucketNames.SOURCE, Key=archived_key)
300+
self.assertIsNotNone(archived_obj)
301+
302+
# Also verify file copied to DPS destination bucket under dps_destination/<file_key>
303+
dps_key = f"dps_destination/{test_cases[0].file_key}"
304+
copied_obj = s3_client.get_object(Bucket=BucketNames.DPS_DESTINATION, Key=dps_key)
305+
self.assertIsNotNone(copied_obj)
295306

296307
# No SQS and no ack file
297308
self.assert_no_sqs_message()
@@ -443,6 +454,12 @@ def test_lambda_handler_extended_attributes_extension_checks(self, mock_get_redi
443454
# .CSV accepted
444455
csv_key = MockFileDetails.extended_attributes_file.file_key
445456
s3_client.put_object(Bucket=BucketNames.SOURCE, Key=csv_key, Body=MOCK_EXTENDED_ATTRIBUTES_FILE_CONTENT)
457+
# Ensure DPS destination bucket exists so copy can succeed
458+
try:
459+
s3_client.create_bucket(Bucket=BucketNames.DPS_DESTINATION)
460+
except Exception:
461+
pass
462+
446463
with (
447464
patch("file_name_processor.validate_permissions_for_extended_attributes_files", return_value="X8E5B_COVID"),
448465
# Ensure EA DAT case passes permission validation by returning CUDS for X8E5B
@@ -453,8 +470,11 @@ def test_lambda_handler_extended_attributes_extension_checks(self, mock_get_redi
453470
patch("file_name_processor.uuid4", return_value="EA_csv_id"),
454471
):
455472
lambda_handler(self.make_event([self.make_record(csv_key)]), None)
456-
# Ensure processed path hit by checking destination (implementation currently uses single slash)
457-
s3_client.get_object(Bucket=BucketNames.DESTINATION, Key=f"dps_destination/{csv_key}")
473+
474+
# Ensure processed path hit by checking archive move in source bucket
475+
s3_client.get_object(Bucket=BucketNames.SOURCE, Key=f"extended-attributes-archive/{csv_key}")
476+
# And verify copy to DPS destination
477+
s3_client.get_object(Bucket=BucketNames.DPS_DESTINATION, Key=f"dps_destination/{csv_key}")
458478

459479
# .DAT accepted
460480
dat_key = MockFileDetails.extended_attributes_file.file_key[:-3] + "dat"
@@ -464,7 +484,8 @@ def test_lambda_handler_extended_attributes_extension_checks(self, mock_get_redi
464484
patch("file_name_processor.uuid4", return_value="EA_dat_id"),
465485
):
466486
lambda_handler(self.make_event([self.make_record(dat_key)]), None)
467-
s3_client.get_object(Bucket=BucketNames.DESTINATION, Key=f"dps_destination/{dat_key}")
487+
s3_client.get_object(Bucket=BucketNames.SOURCE, Key=f"extended-attributes-archive/{dat_key}")
488+
s3_client.get_object(Bucket=BucketNames.DPS_DESTINATION, Key=f"dps_destination/{dat_key}")
468489

469490
# Invalid extension fails
470491
bad_ext_key = csv_key[:-3] + "txt"

lambdas/filenameprocessor/tests/utils_for_tests/mock_environment_variables.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BucketNames:
1212
CONFIG = "immunisation-batch-internal-dev-data-configs"
1313
SOURCE = "immunisation-batch-internal-dev-data-sources"
1414
DESTINATION = "immunisation-batch-internal-dev-data-destinations"
15+
DPS_DESTINATION = "nhsd-dspp-core-ref-extended-attributes-gdp"
1516
# Mock firehose bucket used for testing only (due to limitations of the moto testing package)
1617
MOCK_FIREHOSE = "mock-firehose-bucket"
1718

@@ -36,7 +37,9 @@ class Sqs:
3637
MOCK_ENVIRONMENT_DICT = {
3738
"SOURCE_BUCKET_NAME": BucketNames.SOURCE,
3839
"ACK_BUCKET_NAME": BucketNames.DESTINATION,
40+
"DPS_BUCKET_NAME": BucketNames.DESTINATION,
3941
"ACCOUNT_ID": MOCK_ACCOUNT_ID,
42+
"DPS_ACCOUNT_ID": MOCK_ACCOUNT_ID,
4043
"QUEUE_URL": "https://sqs.eu-west-2.amazonaws.com/123456789012/imms-batch-file-created-queue.fifo",
4144
"REDIS_HOST": "localhost",
4245
"REDIS_PORT": "6379",

lambdas/filenameprocessor/tests/utils_for_tests/utils_for_filenameprocessor_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def __init__(
9191
for bucket_name in [
9292
BucketNames.SOURCE,
9393
BucketNames.DESTINATION,
94+
BucketNames.DPS_DESTINATION,
9495
BucketNames.CONFIG,
9596
BucketNames.MOCK_FIREHOSE,
9697
]:
@@ -136,6 +137,7 @@ def __init__(
136137
for bucket_name in [
137138
BucketNames.SOURCE,
138139
BucketNames.DESTINATION,
140+
BucketNames.DPS_DESTINATION,
139141
BucketNames.CONFIG,
140142
BucketNames.MOCK_FIREHOSE,
141143
]:

0 commit comments

Comments
 (0)