refector: optimized concurrent.futures
This commit is contained in:
+9
-12
@@ -16,15 +16,14 @@ def getRegions(all_regions=False):
|
||||
response = ec2.describe_regions(AllRegions=all_regions)
|
||||
return [region['RegionName'] for region in response['Regions']]
|
||||
|
||||
def printResources(region_name: str) -> list[list[str | int]]:
|
||||
def getResources(region_name: str) -> list[list[str | int]]:
|
||||
return_data = []
|
||||
client = boto3.client('ec2', region_name=region_name)
|
||||
response = client.describe_subnets()
|
||||
for i in response['Subnets']:
|
||||
default_vpc_check = client.describe_vpcs(VpcIds=[i['VpcId']])
|
||||
for v in default_vpc_check['Vpcs']:
|
||||
if i:
|
||||
return_data.append([i['SubnetId'], i['VpcId'], i['CidrBlock'], i['AvailabilityZone'], v['IsDefault']])
|
||||
return_data.append([i['SubnetId'], i['VpcId'], i['CidrBlock'], i['AvailabilityZone'], v['IsDefault']])
|
||||
return return_data
|
||||
|
||||
# Main function
|
||||
@@ -33,16 +32,14 @@ def main() -> None:
|
||||
wb = load_workbook('aws-inventory.xlsx')
|
||||
ws = wb.create_sheet("Subnets")
|
||||
|
||||
final_data = []
|
||||
ws.append(["SubnetId", "VpcId", "CidrBlock", "AvailabilityZone", "InDefaultVpc"])
|
||||
with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor:
|
||||
futures = [executor.submit(printResources, region_name=r) for r in getRegions()]
|
||||
for future in concurrent.futures.as_completed(futures):
|
||||
region_data = future.result()
|
||||
final_data.extend(region_data)
|
||||
|
||||
for row in final_data:
|
||||
ws.append(row)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
|
||||
results = executor.map(getResources, getRegions())
|
||||
for region_rows in results:
|
||||
# append to worksheet only if resoruces are found in the region
|
||||
if region_rows:
|
||||
for row in region_rows:
|
||||
ws.append(row)
|
||||
|
||||
wb.save('aws-inventory.xlsx')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user