Skip to content

Commit

Permalink
Improve aws_parseInstanceType function
Browse files Browse the repository at this point in the history
  • Loading branch information
kuettai committed Apr 29, 2024
1 parent 921dcfe commit 771a466
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
8 changes: 8 additions & 0 deletions services/Evaluator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import traceback
import botocore
import time

from utils.Config import Config
from utils.Tools import _warn, _info
Expand Down Expand Up @@ -37,10 +38,17 @@ def run(self, serviceName):
for method in methods:
if not rules or str.lower(method[6:]) in rules:
try:

startTime = time.time()
if debugFlag:
print('--- --- fn: ' + method)

getattr(self, method)()
if debugFlag:
timeSpent = round(time.time() - startTime, 3)
if timeSpent >= 0.2:
_warn("Long running checks {}s".format(timeSpent))

cnt += 1
except botocore.exceptions.ClientError as e:
code = e.response['Error']['Code']
Expand Down
7 changes: 4 additions & 3 deletions services/ec2/drivers/Ec2Instance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boto3
import botocore
import datetime
import time
from utils.Config import Config

from datetime import timedelta
Expand Down Expand Up @@ -95,8 +96,7 @@ def _checkSQLServerEdition(self):
self.results['SQLServerEOL'] = [-1, image['Name']]

def _checkInstanceTypeGeneration(self):

instanceArr = aws_parseInstanceFamily(self.ec2InstanceData['InstanceType'])
instanceArr = aws_parseInstanceFamily(self.ec2InstanceData['InstanceType'], region=self.ec2Client.meta.region_name)
instancePrefixArr = instanceArr['prefixDetail']

instancePrefixArr['version'] = int(instancePrefixArr['version'])+1
Expand All @@ -105,7 +105,8 @@ def _checkInstanceTypeGeneration(self):

try:
results = self.ec2Client.describe_instance_types(
InstanceTypes=[newFamily + '.' + size]
InstanceTypes=[newFamily + '.' + size],
MaxResults=1
)
except Exception as e:
self.results['EC2NewGen'] = [1, self.ec2InstanceData['InstanceType']]
Expand Down
2 changes: 1 addition & 1 deletion services/opensearch/drivers/OpensearchCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _checkInstanceVersion(self):
instance_type = self.cluster_config["InstanceType"]
self.results["LatestInstanceVersion"] = [1, instance_type]

instance_info = aws_parseInstanceFamily(instance_type)
instance_info = aws_parseInstanceFamily(instance_type, region=self.osClient.meta.region_name)

instance_prefix_arr = instance_info["prefixDetail"]
instance_prefix_arr["version"] = int(instance_prefix_arr["version"]) + 1
Expand Down
2 changes: 1 addition & 1 deletion services/rds/drivers/RdsCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def getInstInfo(self):
self.addII('isServerless', True)
return

self.instInfo = aws_parseInstanceFamily(self.db['DBInstanceClass'])
self.instInfo = aws_parseInstanceFamily(self.db['DBInstanceClass'], region=self.rdsClient.meta.region_name)
self.addII('instInfo', self.instInfo)
self.addII('IsCluster', False)

Expand Down
11 changes: 7 additions & 4 deletions utils/Tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import boto3
import re
import time

from pprint import pprint
from utils.Config import Config
Expand All @@ -26,8 +27,11 @@ def checkIsPrivateIp(ipaddr):
ip = ipaddr.split('/')
return IPAddress(ip[0]).is_private()

def aws_parseInstanceFamily(instanceFamily: str) -> Dict[str, str]:
CURRENT_REGION = Config.CURRENT_REGION
def aws_parseInstanceFamily(instanceFamily: str, region=None) -> Dict[str, str]:
if region:
CURRENT_REGION = region
else:
CURRENT_REGION = Config.CURRENT_REGION

arr = instanceFamily.split('.')
if len(arr) > 3 or len(arr) == 1:
Expand All @@ -52,9 +56,8 @@ def aws_parseInstanceFamily(instanceFamily: str) -> Dict[str, str]:
ssBoto = Config.get('ssBoto', None)
if not spec:
ec2c = ssBoto.client('ec2', region_name=CURRENT_REGION)

resp = ec2c.describe_instance_types(InstanceTypes=[family])

iType = resp.get('InstanceTypes')
if iType:
info = iType[0]
Expand Down

0 comments on commit 771a466

Please sign in to comment.