Skip to content

Commit cfba5b1

Browse files
fix multi-line f-strings. fix not parsing all VPCs
1 parent c818f69 commit cfba5b1

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

reference-artifacts/Custom-Scripts/lza-upgrade/tools/network-drift-detection/lza-upgrade-check.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ def process_vpc_config(account, vpc, vpc_dict):
139139
def flatten_subnet_config(vpc_name, subnets):
140140
"""Takes subnet object from ASEA config and generate list of subnets to be created per AZ"""
141141
return [
142-
{"Name": f"{subnet['name']}_{vpc_name}_az{d['az']}_net",
143-
"route-table": f"{d['route-table']}_rt"}
142+
{"Name": f"{subnet['name']}_{vpc_name}_az{d['az']}_net", "route-table": f"{d['route-table']}_rt"}
144143
for subnet in subnets
145144
for d in subnet["definitions"]
146145
if not d.get('disabled', False)
@@ -278,8 +277,7 @@ def get_transit_gateway_route_tables(ec2_client, tgw_id: str) -> List[Dict]:
278277
blackhole_routes = get_transit_gateway_routes(
279278
ec2_client, tgwrt["TransitGatewayRouteTableId"], "blackhole")
280279
except Exception as e:
281-
logger.error(f"Failed to get routes for table {
282-
tgwrt['TransitGatewayRouteTableId']}: {str(e)}")
280+
logger.error(f"Failed to get routes for table {tgwrt['TransitGatewayRouteTableId']}: {str(e)}")
283281
active_routes = []
284282

285283
name = next((tag["Value"] for tag in tgwrt.get("Tags", [])
@@ -322,8 +320,7 @@ def get_transit_gateway_routes(ec2_client, tgwrt_id: str, state: str) -> List[Di
322320
"""
323321
valid_states = ['active', 'blackhole', 'deleted', 'deleting', 'pending']
324322
if state not in valid_states:
325-
raise ValueError(f"Invalid route state. Must be one of: {
326-
', '.join(valid_states)}")
323+
raise ValueError(f"Invalid route state. Must be one of: {', '.join(valid_states)}")
327324

328325
try:
329326
response = ec2_client.search_transit_gateway_routes(
@@ -376,10 +373,12 @@ def get_vpc_route_tables(ec2_client, vpcId):
376373
r = {"Name": name,
377374
"RouteTableId": rt["RouteTableId"],
378375
"VpcId": rt["VpcId"],
376+
"Main": any([asso["Main"] for asso in rt["Associations"] if "Main" in asso]),
379377
"SubnetAssociations": [asso["SubnetId"] for asso in rt["Associations"] if "SubnetId" in asso],
380378
"Routes": rt["Routes"],
381379
"RawResponse": rt
382380
}
381+
383382
rt_list.append(r)
384383

385384
return rt_list
@@ -474,9 +473,12 @@ def analyze_vpcs(vpc_from_config, account_list, role_to_assume, region):
474473
if f"{rt['name']}_rt" == drt["Name"]]
475474
if len(crt) == 0:
476475
logger.warning(
477-
f"Route table {drt['Name']} exists in VPC {dv} but not in config")
478-
drift["route_tables_not_in_config"].append(
479-
{"RouteTable": drt["Name"], "Vpc": dv})
476+
f"Route table {drt['Name']} exists in VPC {dv} but not in config. {'(Main)' if drt['Main'] else ''}")
477+
478+
# Do not add to drift if its the main route table and there are no Subnet Associations
479+
if not drt['Main'] or len(drt['SubnetAssociations']) > 0:
480+
drift["route_tables_not_in_config"].append(
481+
{"RouteTable": drt["Name"], "Vpc": dv})
480482
continue
481483

482484
# check if all route tables from the config exist in the environment
@@ -536,7 +538,7 @@ def analyze_vpcs(vpc_from_config, account_list, role_to_assume, region):
536538
vpc_details[dv] = {
537539
"Account": account, "RouteTables": d_rtables, "Subnets": d_subnets}
538540

539-
return {"Drift": drift, "VpcDetails": vpc_details}
541+
return {"Drift": drift, "VpcDetails": vpc_details}
540542

541543

542544
def get_tgw_from_config(asea_config, region):

0 commit comments

Comments
 (0)