Skip to content

Commit efafc02

Browse files
committed
py lint
1 parent 017066a commit efafc02

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Resources:
151151
152152
REGIONS = [r.strip() for r in os.environ.get("REGIONS").split(',') if r]
153153
ROLE_NAME = os.environ['ROLE_NAME']
154-
bucket = os.environ['DESTINATION_BUCKET']
154+
BUCKET = os.environ['DESTINATION_BUCKET']
155155
156156
logger = logging.getLogger(__name__)
157157
logger.setLevel(getattr(logging, os.environ.get('LOG_LEVEL', 'INFO').upper(), logging.INFO))
@@ -174,15 +174,6 @@ Resources:
174174
return obj.strftime("%Y-%m-%d %H:%M:%S")
175175
return obj
176176
177-
def sort_dict(d):
178-
""" Sort dicts and lists recursively """
179-
if isinstance(d, dict):
180-
return {k: sort_dict(v) for k, v in sorted(d.items())}
181-
elif isinstance(d, list):
182-
return [sort_dict(v) for v in d]
183-
else:
184-
return d
185-
186177
@contextmanager
187178
def s3_json_file(s3_client: boto3.client, bucket: str, s3_path: str):
188179
"""
@@ -196,7 +187,7 @@ Resources:
196187
# Create temporary file
197188
temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8')
198189
def write_json(data) -> None:
199-
temp_file.write(json.dumps(sort_dict(data), default=json_converter) + '\n')
190+
temp_file.write(json.dumps(data, default=json_converter) + '\n')
200191
yield write_json
201192
if not temp_file.closed:
202193
temp_file.close()
@@ -216,7 +207,7 @@ Resources:
216207
except OSError as e:
217208
print(f"Warning: Could not delete temporary file {temp_file.name}: {e}")
218209
219-
def lambda_handler(event, context):
210+
def lambda_handler(event, context): #pylint: disable=unused-argument,too-many-branches,too-many-locals,too-many-statements
220211
logger.info(f"Event: {event}")
221212
if 'account' not in event:
222213
raise ValueError(
@@ -239,7 +230,7 @@ Resources:
239230
aws_session_token=creds["SessionToken"]
240231
)
241232
s3 = boto3.client('s3') # local s3
242-
s3_uploader = partial(s3_json_file, s3, bucket)
233+
s3_uploader = partial(s3_json_file, s3, BUCKET)
243234
244235
for region in REGIONS:
245236
try:
@@ -251,8 +242,8 @@ Resources:
251242
}
252243
status_key = f"{module_name}/{module_name}-status/payer_id={payer_id}/account_id={account_id}/region_code={region}/status.json"
253244
try:
254-
status_obj = json.loads(s3.get_object(Bucket=bucket, Key=status_key)['Body'].read().decode('utf-8'))
255-
except s3.exceptions.NoSuchKey as exc:
245+
status_obj = json.loads(s3.get_object(Bucket=BUCKET, Key=status_key)['Body'].read().decode('utf-8'))
246+
except s3.exceptions.NoSuchKey:
256247
pass # this is fine if there no status file
257248
last_collection_time = datetime.strptime(status_obj["last_read"], "%Y-%m-%d %H:%M:%S")
258249
collection_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
@@ -262,7 +253,7 @@ Resources:
262253
app_arn = app_summary['appArn']
263254
app_id = app_arn.split('/')[-1]
264255
app = resilience_client.describe_app(appArn=app_arn)['app']
265-
256+
266257
# Initialize data structure
267258
app_data = {
268259
'region': region,
@@ -309,15 +300,15 @@ Resources:
309300
with s3_uploader(f'{module_name}/{module_name}-resiliency_policy/payer_id={payer_id}/account_id={account_id}/region_code={region}/{policy_id}.json') as write_policy:
310301
write_policy(policy_response['policy'])
311302
312-
except Exception as e:
303+
except Exception as e: #pylint: disable=broad-exception-caught
313304
print(f"Error getting policy details: {str(e)}")
314305
315306
# Loop over list of assessments to get the latest successful
316307
latest_assessment = None
317308
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:
318309
for assessment in paginate(resilience_client.list_app_assessments, 'assessmentSummaries', appArn=app_arn):
319310
if assessment['assessmentStatus'] == 'Success':
320-
if not latest_assessment or latest_assessment['endTime'] < assessment['endTime']:
311+
if not latest_assessment or latest_assessment['endTime'] < assessment['endTime']: #pylint: disable=unsubscriptable-object
321312
latest_assessment = assessment
322313
write_assessment(assessment)
323314
@@ -403,25 +394,25 @@ Resources:
403394
rec['app_arn'] = app_arn
404395
write(rec)
405396
406-
397+
407398
with s3_uploader(f'{module_name}/{module_name}-applications/payer_id={payer_id}/account={account_id}/{region}-{app_id}.json') as write_app:
408399
write_app(app_data)
409400
with s3_uploader(f'{module_name}/{module_name}-application-details/payer_id={payer_id}/account_id={account_id}/{region}-{app_id}.json') as write_app:
410401
write_app(app)
411402
412403
# Write the time to s3
413404
status_obj["last_read"] = collection_time
414-
s3.put_object(Bucket=bucket, Key=status_key, Body=json.dumps(status_obj), ContentType='application/json')
405+
s3.put_object(Bucket=BUCKET, Key=status_key, Body=json.dumps(status_obj), ContentType='application/json')
415406
416-
except Exception as e:
407+
except Exception as e: #pylint: disable=broad-exception-caught
417408
print(f"Error pulling data in region {region}: {str(e)}")
418409
419410
return {
420411
'statusCode': 200,
421412
'body': {
422413
'message': 'Exports and copy completed successfully',
423414
'account': account_id,
424-
'bucket': bucket,
415+
'bucket': BUCKET,
425416
}
426417
}
427418

0 commit comments

Comments
 (0)