feat: use of jmespath for ec2 inventory
This commit is contained in:
+8
-11
@@ -11,7 +11,7 @@ import concurrent.futures
|
||||
from openpyxl import load_workbook
|
||||
from openpyxl.worksheet.worksheet import Worksheet
|
||||
import projectlib.aws
|
||||
|
||||
import jmespath
|
||||
|
||||
def getVolumeSize(region_name: str, id: str) -> int:
|
||||
client = boto3.client('ec2', region_name=region_name)
|
||||
@@ -21,16 +21,13 @@ def getVolumeSize(region_name: str, id: str) -> int:
|
||||
def getResources(region_name: str) -> list[list[str | int]]:
|
||||
return_data = []
|
||||
client = boto3.client('ec2', region_name=region_name)
|
||||
response = client.describe_instances()
|
||||
for r in response['Reservations']:
|
||||
for i in r['Instances']:
|
||||
name_tag = 'Unset'
|
||||
for tag in i['Tags']:
|
||||
if tag['Key'] == "Name":
|
||||
name_tag = tag['Value']
|
||||
break
|
||||
rootVolSize = getVolumeSize(region_name=region_name, id=i['BlockDeviceMappings'][0]['Ebs']['VolumeId'])
|
||||
return_data.append([i['InstanceId'], name_tag, i.get('PlatformDetails'), i['InstanceType'], i['PrivateIpAddress'], rootVolSize, i['Placement']['AvailabilityZone']])
|
||||
paginator = client.get_paginator('describe_instances')
|
||||
for page in paginator.paginate():
|
||||
for r in page['Reservations']:
|
||||
for i in r['Instances']:
|
||||
name_tag = jmespath.search("Tags[?Key=='Name'] | [0].Value", i)
|
||||
rootVolSize = getVolumeSize(region_name=region_name, id=i['BlockDeviceMappings'][0]['Ebs']['VolumeId'])
|
||||
return_data.append([i['InstanceId'], name_tag, i.get('PlatformDetails'), i['InstanceType'], i['PrivateIpAddress'], rootVolSize, i['Placement']['AvailabilityZone']])
|
||||
return return_data
|
||||
|
||||
# Main function
|
||||
|
||||
Reference in New Issue
Block a user