Skip to content

Commit ec7d5e5

Browse files
committed
fixes
1 parent 39e904b commit ec7d5e5

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

data-collection/deploy/module-resilience-hub.yaml

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,21 @@ Resources:
192192
# file will be uploaded to s3 at the end
193193
"""
194194
temp_file = None
195+
records = 0
195196
try:
196197
# Create temporary file
197198
temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8')
198199
def write_json(data) -> None:
200+
records += 1
199201
temp_file.write(json.dumps(sort_dict(data), default=json_converter) + '\n')
200202
yield write_json
201203
if not temp_file.closed:
202204
temp_file.close()
203-
print(f"Uploading JSON file to s3://{bucket}/{s3_path}")
204205
s3_client.upload_file(temp_file.name, bucket, s3_path)
205-
print(f"Successfully uploaded JSON to s3://{bucket}/{s3_path}")
206+
print(f"Uploaded {records} records to s3://{bucket}/{s3_path}")
206207
207208
except Exception as e:
208-
print(f"Error during S3 JSON upload: {str(e)}")
209+
print(f"Error during S3 upload s3://{bucket}/{s3_path}: {str(e)}")
209210
raise
210211
211212
finally:
@@ -247,18 +248,15 @@ Resources:
247248
resilience_client = assumed_session.client('resiliencehub', region_name=region)
248249
249250
# Read the latest read date (if any)
250-
status_obj = {
251251
status_obj = {
252252
"last_read": (datetime.now().date() - timedelta(days=365 * 10)).strftime("%Y-%m-%d %H:%M:%S"),
253253
}
254254
status_key = f"{module_name}/{module_name}-status/payer_id={payer_id}/account_id={account_id}/region_code={region}/status.json"
255255
try:
256256
status_obj = json.loads(s3.get_object(Bucket=bucket, Key=status_key)['Body'].read().decode('utf-8'))
257-
status_obj = json.loads(s3.get_object(Bucket=bucket, Key=status_key)['Body'].read().decode('utf-8'))
258257
except s3.exceptions.NoSuchKey as exc:
259258
pass # this is fine if there no status file
260259
last_collection_time = datetime.strptime(status_obj["last_read"], "%Y-%m-%d %H:%M:%S")
261-
last_collection_time = datetime.strptime(status_obj["last_read"], "%Y-%m-%d %H:%M:%S")
262260
collection_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
263261
264262
# Loop over list of apps to retrieve details
@@ -318,7 +316,7 @@ Resources:
318316
319317
# Loop over list of assessments to get the latest successful
320318
latest_assessment = None
321-
with s3_uploader(f'{module_name}/{module_name}-assessments/payer_id={payer_id}/account_id={account_id}/region_code={region}/app={app_id}/all.json') as write_assessment:
319+
with s3_uploader(f'{module_name}/{module_name}-assessments/payer_id={payer_id}/account_id={account_id}/region_code={region}/app_id={app_id}/all.json') as write_assessment:
322320
for assessment in paginate(resilience_client.list_app_assessments, 'assessmentSummaries', appArn=app_arn):
323321
if assessment['assessmentStatus'] == 'Success':
324322
if not latest_assessment or latest_assessment['endTime'] < assessment['endTime']:
@@ -329,15 +327,16 @@ Resources:
329327
if latest_assessment:
330328
331329
app_data['last_assessment_time'] = latest_assessment['endTime']
332-
app_data['last_assessment_arn'] = latest_assessment['assessmentArn']
330+
app_data['last_assessment_arn'] = assessment_arn = latest_assessment['assessmentArn']
333331
app_data['resiliency_score'] = latest_assessment.get('resiliencyScore')
334332
335-
with s3_uploader(f'{module_name}/{module_name}-app_component_recommendations/payer_id={payer_id}/account_id={account_id}/region_code={region}/app={app_id}/latest.json') as write:
333+
with s3_uploader(f'{module_name}/{module_name}-app_component_recommendations_latest/payer_id={payer_id}/account_id={account_id}/region_code={region}/app_id={app_id}/latest.json') as write:
336334
for rec in paginate(resilience_client.list_app_component_recommendations, 'componentRecommendations', assessmentArn=latest_assessment['assessmentArn']):
337335
print(f'Record Value Test {rec}')
336+
rec['assessment_arn'] = assessment_arn
338337
write(rec)
339338
340-
with s3_uploader(f'{module_name}/{module_name}-alarm_recommendations/payer_id={payer_id}/account_id={account_id}/region_code={region}/app={app_id}/latest.json') as write:
339+
with s3_uploader(f'{module_name}/{module_name}-alarm_recommendations_latest/payer_id={payer_id}/account_id={account_id}/region_code={region}/app_id={app_id}/latest.json') as write:
341340
total = outstanding = excluded = 0
342341
for rec in paginate(resilience_client.list_alarm_recommendations, 'alarmRecommendations', assessmentArn=latest_assessment['assessmentArn']):
343342
status = rec.get('recommendationStatus', '') # 'recommendationStatus': 'Implemented'|'Inactive'|'NotImplemented'|'Excluded',
@@ -346,12 +345,13 @@ Resources:
346345
outstanding += 1
347346
elif status == 'Excluded':
348347
excluded += 1
348+
rec['assessment_arn'] = assessment_arn
349349
write(rec)
350350
app_data['alarms_total'] = str(total)
351351
app_data['alarms_outstanding'] = str(outstanding)
352352
app_data['alarms_excluded'] = str(excluded)
353353
354-
with s3_uploader(f'{module_name}/{module_name}-sop_recommendations/payer_id={payer_id}/account_id={account_id}/region_code={region}/app={app_id}/latest.json') as write:
354+
with s3_uploader(f'{module_name}/{module_name}-sop_recommendations_latest/payer_id={payer_id}/account_id={account_id}/region_code={region}/app_id={app_id}/latest.json') as write:
355355
total = outstanding = excluded = 0
356356
for rec in paginate(resilience_client.list_sop_recommendations, 'sopRecommendations', assessmentArn=latest_assessment['assessmentArn']):
357357
status = rec.get('recommendationStatus', '') # 'recommendationStatus': 'Implemented'|'Inactive'|'NotImplemented'|'Excluded',
@@ -360,12 +360,13 @@ Resources:
360360
outstanding += 1
361361
elif status == 'Excluded':
362362
excluded += 1
363+
rec['assessment_arn'] = assessment_arn
363364
write(rec)
364365
app_data['sops_total'] = str(total)
365366
app_data['sops_outstanding'] = str(outstanding)
366367
app_data['sops_excluded'] = str(excluded)
367368
368-
with s3_uploader(f'{module_name}/{module_name}-test_recommendations/payer_id={payer_id}/account_id={account_id}/region_code={region}/app={app_id}/latest.json') as write:
369+
with s3_uploader(f'{module_name}/{module_name}-test_recommendations_latest/payer_id={payer_id}/account_id={account_id}/region_code={region}/app_id={app_id}/latest.json') as write:
369370
total = outstanding = excluded = 0
370371
for rec in paginate(resilience_client.list_test_recommendations, 'testRecommendations', assessmentArn=latest_assessment['assessmentArn']):
371372
test_type = rec.get('type', '') # 'type': 'Software'|'Hardware'|'AZ'|'Region'
@@ -376,17 +377,19 @@ Resources:
376377
outstanding += 1
377378
elif status == 'Excluded':
378379
excluded += 1
380+
rec['assessment_arn'] = assessment_arn
379381
write(rec)
380382
app_data['fis_tests_total'] = str(total)
381383
app_data['fis_tests_outstanding'] = str(outstanding)
382384
app_data['fis_tests_excluded'] = str(excluded)
383385
384-
with s3_uploader(f'{module_name}/{module_name}-compliance_drift/payer_id={payer_id}/account_id={account_id}/region_code={region}/app={app_id}/latest.json') as write:
386+
with s3_uploader(f'{module_name}/{module_name}-compliance_drifts_latest/payer_id={payer_id}/account_id={account_id}/region_code={region}/app_id={app_id}/latest.json') as write:
385387
app_component_drifts = 0
386388
for rec in paginate(resilience_client.list_app_assessment_compliance_drifts, 'complianceDrifts', assessmentArn=latest_assessment['assessmentArn']):
387389
drift_type = rec.get('driftType', '') # 'driftType': 'ApplicationCompliance'|'AppComponentResiliencyComplianceStatus',
388390
if drift_type == 'ApplicationCompliance':
389391
app_component_drifts += 1
392+
rec['assessment_arn'] = assessment_arn
390393
write(rec)
391394
app_data['app_component_drifts'] = str(app_component_drifts)
392395
@@ -471,10 +474,10 @@ Resources:
471474
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-resiliency_policy/"
472475
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-assessments/"
473476
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-app_component_recommendations/"
474-
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-alarm_recommendations/"
475-
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-sop_recommendations/"
476-
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-test_recommendations/"
477-
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-compliance_drift/"
477+
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-alarm_recommendations_latest/"
478+
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-sop_recommendations_latest/"
479+
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-test_recommendations_latest/"
480+
- Path: !Sub "s3://${DestinationBucket}/${CFDataName}/${CFDataName}-compliance_drifts_latest/"
478481
SchemaChangePolicy:
479482
UpdateBehavior: UPDATE_IN_DATABASE
480483
DeleteBehavior: DEPRECATE_IN_DATABASE

0 commit comments

Comments
 (0)