Skip to content

Commit 7714679

Browse files
committed
Merge branch 'VED-901-Test-Extended-Attribute' into VED-902-1-extended-attribute
2 parents e56fa4a + a8a5164 commit 7714679

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

lambdas/filenameprocessor/src/constants.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
)
1212

1313
SOURCE_BUCKET_NAME = os.getenv("SOURCE_BUCKET_NAME")
14+
15+
# We have used an internal temporary bucket here and an acutal dps bucket will replace this
1416
DPS_DESTINATION_BUCKET_NAME = os.getenv("ACK_BUCKET_NAME")
17+
EXPECTED_BUCKET_OWNER_ACCOUNT = os.getenv("ACCOUNT_ID")
1518
AUDIT_TABLE_NAME = os.getenv("AUDIT_TABLE_NAME")
1619
AUDIT_TABLE_TTL_DAYS = os.getenv("AUDIT_TABLE_TTL_DAYS")
1720
VALID_VERSIONS = ["V5"]
1821

1922
VACCINE_TYPE_TO_DISEASES_HASH_KEY = "vacc_to_diseases"
2023
ODS_CODE_TO_SUPPLIER_SYSTEM_HASH_KEY = "ods_code_to_supplier"
21-
EXTENDED_ATTRIBUTES_PREFIXES = "Vaccination_Extended_Attributes"
24+
EXTENDED_ATTRIBUTES_FILE_PREFIX = "Vaccination_Extended_Attributes"
25+
DPS_DESTINATION_PREFIX = "dps_destination/"
2226

2327
ERROR_TYPE_TO_STATUS_CODE_MAP = {
2428
VaccineTypePermissionsError: 403,

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from audit_table import upsert_audit_table
1515
from common.aws_s3_utils import (
16-
copy_file_outside_bucket,
16+
copy_file_to_external_bucket,
1717
delete_file,
1818
is_file_in_bucket,
1919
move_file,
@@ -23,8 +23,9 @@
2323
from common.models.errors import UnhandledAuditTableError
2424
from constants import (
2525
DPS_DESTINATION_BUCKET_NAME,
26+
DPS_DESTINATION_PREFIX,
2627
ERROR_TYPE_TO_STATUS_CODE_MAP,
27-
EXTENDED_ATTRIBUTES_PREFIXES,
28+
EXTENDED_ATTRIBUTES_FILE_PREFIX,
2829
SOURCE_BUCKET_NAME,
2930
FileNotProcessedReason,
3031
FileStatus,
@@ -83,7 +84,7 @@ def handle_record(record) -> dict:
8384
s3_response = get_s3_client().get_object(Bucket=bucket_name, Key=file_key)
8485
created_at_formatted_string, expiry_timestamp = get_creation_and_expiry_times(s3_response)
8586

86-
if file_key.startswith(EXTENDED_ATTRIBUTES_PREFIXES):
87+
if file_key.startswith(EXTENDED_ATTRIBUTES_FILE_PREFIX):
8788
return handle_extended_attributes_file(
8889
file_key,
8990
bucket_name,
@@ -113,7 +114,7 @@ def handle_unexpected_bucket_name(bucket_name: str, file_key: str) -> dict:
113114
"""Handles scenario where Lambda was not invoked by the data-sources bucket. Should not occur due to terraform
114115
config and overarching design"""
115116
try:
116-
if file_key.startswith(EXTENDED_ATTRIBUTES_PREFIXES):
117+
if file_key.startswith(EXTENDED_ATTRIBUTES_FILE_PREFIX):
117118
extended_attribute_identifier = validate_extended_attributes_file_key(file_key)
118119
logger.error(
119120
"Unable to process file %s due to unexpected bucket name %s",
@@ -162,7 +163,9 @@ def handle_unexpected_bucket_name(bucket_name: str, file_key: str) -> dict:
162163
}
163164

164165

165-
def handle_batch_file(file_key, bucket_name, message_id, created_at_formatted_string, expiry_timestamp) -> dict:
166+
def handle_batch_file(
167+
file_key: str, bucket_name: str, message_id: str, created_at_formatted_string: str, expiry_timestamp: str
168+
) -> dict:
166169
"""
167170
Processes a single record for batch file.
168171
Returns a dictionary containing information to be included in the logs.
@@ -244,7 +247,7 @@ def handle_batch_file(file_key, bucket_name, message_id, created_at_formatted_st
244247

245248

246249
def handle_extended_attributes_file(
247-
file_key, bucket_name, message_id, created_at_formatted_string, expiry_timestamp
250+
file_key: str, bucket_name: str, message_id: str, created_at_formatted_string: str, expiry_timestamp: str
248251
) -> dict:
249252
"""
250253
Processes a single record for extended attributes file.
@@ -261,19 +264,18 @@ def handle_extended_attributes_file(
261264

262265
try:
263266
extended_attribute_identifier = validate_extended_attributes_file_key(file_key)
264-
queue_name = extended_attribute_identifier
265267

266268
upsert_audit_table(
267269
message_id,
268270
file_key,
269271
created_at_formatted_string,
270272
expiry_timestamp,
271-
queue_name,
273+
extended_attribute_identifier,
272274
FileStatus.PROCESSING,
273275
)
274276

275277
dest_file_key = f"dps_destination/{file_key}"
276-
copy_file_outside_bucket(bucket_name, file_key, DPS_DESTINATION_BUCKET_NAME, dest_file_key)
278+
copy_file_to_external_bucket(bucket_name, file_key, DPS_DESTINATION_BUCKET_NAME, dest_file_key)
277279
is_file_in_bucket(DPS_DESTINATION_BUCKET_NAME, dest_file_key)
278280
delete_file(bucket_name, dest_file_key)
279281

@@ -282,7 +284,7 @@ def handle_extended_attributes_file(
282284
file_key,
283285
created_at_formatted_string,
284286
expiry_timestamp,
285-
queue_name,
287+
extended_attribute_identifier,
286288
FileStatus.PROCESSED,
287289
)
288290

@@ -291,7 +293,7 @@ def handle_extended_attributes_file(
291293
"message": "Extended Attributes file successfully processed",
292294
"file_key": file_key,
293295
"message_id": message_id,
294-
"queue_name": queue_name,
296+
"queue_name": extended_attribute_identifier,
295297
}
296298

297299
except ( # pylint: disable=broad-exception-caught
@@ -307,8 +309,8 @@ def handle_extended_attributes_file(
307309
file_status = get_file_status_for_error(error)
308310

309311
# NB if we got InvalidFileKeyError we won't have a valid queue name
310-
if not queue_name:
311-
queue_name = "unknown"
312+
if not extended_attribute_identifier:
313+
extended_attribute_identifier = "unknown"
312314

313315
# Move file to archive
314316
move_file(bucket_name, file_key, f"archive/{file_key}")
@@ -318,7 +320,7 @@ def handle_extended_attributes_file(
318320
file_key,
319321
created_at_formatted_string,
320322
expiry_timestamp,
321-
queue_name,
323+
extended_attribute_identifier,
322324
file_status,
323325
error_details=str(error),
324326
)

lambdas/filenameprocessor/tests/test_lambda_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_lambda_handler_extended_attributes_success(self):
265265
with (
266266
patch("file_name_processor.uuid4", return_value=test_cases[0].message_id),
267267
patch(
268-
"file_name_processor.copy_file_outside_bucket",
268+
"file_name_processor.copy_file_to_external_bucket",
269269
side_effect=lambda src_bucket, key, dst_bucket, dst_key: (
270270
s3_client.put_object(
271271
Bucket=BucketNames.DESTINATION,
@@ -332,7 +332,7 @@ def test_lambda_handler_extended_attributes_failure(self):
332332
with (
333333
patch("file_name_processor.uuid4", return_value=test_cases[0].message_id),
334334
patch(
335-
"file_name_processor.copy_file_outside_bucket",
335+
"file_name_processor.copy_file_to_external_bucket",
336336
side_effect=lambda src_bucket, key, dst_bucket, dst_key: ( # effectively do nothing
337337
None,
338338
),

lambdas/filenameprocessor/tests/utils_for_tests/values_for_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ class MockFileDetails:
9595
extended_attributes_file = FileDetails(
9696
vaccine_type="Vaccination_Extended_Attributes", file_number=1, organization_code="X8E5B"
9797
)
98-
# extended_attributes_file = "Vaccination_Extended_Attributes_v1_5_RJ123_20000101T00000001.csv"
9998

10099

101100
MOCK_FILE_HEADERS = (

lambdas/shared/src/common/aws_s3_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def move_file(bucket_name: str, source_file_key: str, destination_file_key: str)
1919
logger.info("File moved from %s to %s", source_file_key, destination_file_key)
2020

2121

22-
def copy_file_outside_bucket(source_bucket: str, source_key: str, destination_bucket: str, destination_key: str) -> None:
22+
def copy_file_to_external_bucket(
23+
source_bucket: str, source_key: str, destination_bucket: str, destination_key: str
24+
) -> None:
2325
s3_client = get_s3_client()
2426
s3_client.copy_object(
2527
CopySource={"Bucket": source_bucket, "Key": source_key},

lambdas/shared/tests/test_common/test_s3_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_move_file_outside_bucket_copies_then_deletes(self):
9090
self.s3.get_object(Bucket=self.destination_bucket, Key=destination_key)
9191

9292
# Execute copy across buckets
93-
aws_s3_utils.copy_file_outside_bucket(
93+
aws_s3_utils.copy_file_to_external_bucket(
9494
source_bucket=self.source_bucket,
9595
source_key=source_key,
9696
destination_bucket=self.destination_bucket,

0 commit comments

Comments
 (0)