Skip to content

Commit 1f0c832

Browse files
🐛 fix minor issues (#31)
Signed-off-by: Pranav Gaikwad <[email protected]>
1 parent 5e55c49 commit 1f0c832

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

aws/reporting/ec2.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
logger = logging.getLogger(__name__)
88

9+
EC2_KEYS = [
10+
'InstanceId',
11+
'InstanceType',
12+
'Placement.AvailabilityZone',
13+
'LaunchTime',
14+
'IamInstanceProfile.Arn',
15+
'Tags.owner',
16+
'Tags.Name',
17+
'Tags.guid'
18+
]
19+
920
def get_all_instances():
1021
all_instances = []
1122
for r in get_all_regions():
@@ -25,19 +36,9 @@ def get_instances_per_region(region):
2536
return instances
2637

2738
def reformat_instance_data(raw_instances):
28-
keys = [
29-
'InstanceId',
30-
'InstanceType',
31-
'Placement.AvailabilityZone',
32-
'LaunchTime',
33-
'IamInstanceProfile.Arn',
34-
'Tags.owner',
35-
'Tags.Name',
36-
'Tags.guid'
37-
]
38-
formatted_instances = reformat_data(raw_instances, keys)
39+
formatted_instances = reformat_data(raw_instances, EC2_KEYS)
3940
for inst in formatted_instances:
40-
region = re.sub(r'(\w+)-(\w+)-(\d)\w+', "\g<1>-\g<2>-\g<3>", inst["AvailabilityZone"])
41+
region = re.sub(r'(\w+)-(\w+)-(\d)\w+', r"\g<1>-\g<2>-\g<3>", inst["AvailabilityZone"])
4142
instance_type = inst['InstanceType']
4243
launch_time = inst['LaunchTime']
4344
try:

aws/reporting/elbs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def reformat_elbs_data(elbs):
4444
az = elb['AvailabilityZones'][0]['ZoneName']
4545
else:
4646
az = elb['AvailabilityZones'][0]
47-
elb['Region'] = re.sub(r'(\w+)-(\w+)-(\d)\w+', "\g<1>-\g<2>-\g<3>", az)
47+
elb['Region'] = re.sub(r'(\w+)-(\w+)-(\d)\w+', r"\g<1>-\g<2>-\g<3>", az)
4848
elb['TotalBill'] = "${}".format(calculate_bill_for_elb(elb['Type'], elb['Region'], elb['CreatedTime'])[2])
4949
elb['CostPerDay'] = "${}".format(calculate_bill_for_elb(elb['Type'], elb['Region'], elb['CreatedTime'])[1])
5050
if 'AvailabilityZones' in elb:

aws/reporting/main.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from cloudformation import delete_stacks
99
from ec2 import get_all_instances, reformat_instance_data, \
1010
get_all_eips, reformat_eips_data, get_all_unused_volumes, \
11-
delete_volume, delete_eip, terminate_instance
11+
delete_volume, delete_eip, terminate_instance, EC2_KEYS
1212
from elbs import get_all_elbs, reformat_elbs_data, delete_classic_elb
1313
from emailer import Emailer
1414
from s3 import get_all_buckets, reformat_buckets_data
@@ -32,6 +32,19 @@ def prepare_old_instances_data(all_instances_sheet, old_instances_sheet, tdelta=
3232
instance['Saved'] = existing_old_instances.get(instance['InstanceId'], {}).get('Saved', '')
3333
instance['Notes'] = existing_old_instances.get(instance['InstanceId'], {}).get('Notes', '')
3434
old_instances.append(instance)
35+
if not old_instances:
36+
dummy_old_instance = {}
37+
for key in EC2_KEYS:
38+
split_keys = key.split('.')
39+
if len(split_keys) == 1:
40+
dummy_old_instance[key] = ''
41+
else:
42+
dummy_old_instance[split_keys[-1]] = ''
43+
dummy_old_instance['TotalBill'] = ''
44+
dummy_old_instance['Cost Per Day'] = ''
45+
dummy_old_instance['Saved'] = ''
46+
dummy_old_instance['Notes'] = ''
47+
return [dummy_old_instance]
3548
return old_instances
3649

3750
def prepare_old_s3_buckets_data(all_s3_buckets_sheet, old_s3_buckets_sheet):
@@ -53,7 +66,7 @@ def terminate_instances(old_instances_sheet, all_instances_sheet):
5366
for inst in old_instances:
5467
if 'save' not in inst['Saved'].lower():
5568
instance_id = inst['InstanceId']
56-
instance_region = re.sub(r'(\w+)-(\w+)-(\d)\w+', "\g<1>-\g<2>-\g<3>", inst["AvailabilityZone"])
69+
instance_region = re.sub(r'(\w+)-(\w+)-(\d)\w+', r"\g<1>-\g<2>-\g<3>", inst["AvailabilityZone"])
5770
instance_ids.append([instance_id, instance_region])
5871
for inst in instance_ids:
5972
response = terminate_instance(inst[0], inst[1])
@@ -262,3 +275,4 @@ def start(argument):
262275

263276
if not skip_summary:
264277
summarySheet.append_data_to_sheet([summaryRow])
278+

aws/reporting/pricing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ def _ec2_pricing_filters(instance_type, region_code):
6666
{
6767
'Field': 'location',
6868
'Type': 'TERM_MATCH',
69-
'Value': _region_filter_map()[region_code]
69+
'Value': _region_filter_map().get(region_code, 'US East (N. Virginia)')
7070
},
7171
{
7272
'Field': 'usageType',
7373
'Type': 'TERM_MATCH',
74-
'Value': _ec2_usage_filter_map(instance_type)[region_code]
74+
'Value': _ec2_usage_filter_map(instance_type).get(region_code, 'US East (N. Virginia)')
7575
},
7676
{
7777
'Field': 'tenancy',
@@ -88,7 +88,7 @@ def _elb_pricing_filters(elb_type, region_code):
8888
{
8989
'Field': 'location',
9090
'Type': 'TERM_MATCH',
91-
'Value': _region_filter_map()[region_code]
91+
'Value': _region_filter_map().get(region_code, 'US East (N. Virginia)')
9292
},
9393
{
9494
'Field': 'operation',
@@ -155,4 +155,4 @@ def calculate_bill_for_elb(elb_type, region_code, launch_time):
155155
"""
156156
price_per_hour = get_price_for_elb(elb_type, region_code)
157157
total_bill, price_per_day = _calculate_bill(launch_time, price_per_hour)
158-
return (price_per_hour, price_per_day, total_bill)
158+
return (price_per_hour, price_per_day, total_bill)

aws/reporting/sheet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def get_custom_range(self, start, end):
5050

5151
def read_custom(self, start, end, indexField=None):
5252
return self.client.service.spreadsheets().values().get(
53-
spreadsheetId=self.sheet_id, range=self.get_custom_range(start, end)).execute()['values']
53+
spreadsheetId=self.sheet_id, range=self.get_custom_range(start, end)).execute().get('values', [])
5454

5555
def read_spreadsheet(self, indexField=None):
5656
return self.from_sheet_data(self.client.service.spreadsheets().values().get(
57-
spreadsheetId=self.sheet_id, range=self.get_sheet_range()).execute()['values'], indexField)
57+
spreadsheetId=self.sheet_id, range=self.get_sheet_range()).execute().get('values', []), indexField)
5858

5959
def load_data_from_sheet(self):
6060
return self.from_sheet_data(self.sheet.get('values', []))

0 commit comments

Comments
 (0)