Skip to content

Commit bf37d34

Browse files
authored
Merge pull request #122 from ifosch/pre-0.1.4
Make code style to pass
2 parents 1ffa111 + 815e51a commit bf37d34

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

accloudtant/aws/prices.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
class Prices(object):
2727
def __init__(self):
2828
with warnings.catch_warnings(record=True) as price_warnings:
29-
curr_url = 'https://aws.amazon.com/ec2/pricing/on-demand/'
30-
curr_ri_url = 'https://aws.amazon.com/ec2/pricing/reserved-instances/pricing/'
31-
prev_url = 'https://aws.amazon.com/ec2/previous-generation/'
29+
base_url = 'https://aws.amazon.com/ec2/'
30+
curr_url = '{}pricing/on-demand/'.format(base_url)
31+
curr_ri_url = '{}pricing/reserved-instances/pricing/'.format(
32+
base_url
33+
)
34+
prev_url = '{}previous-generation/'.format(base_url)
3235
self.prices = process_ec2(curr_url)
3336
prices_curr_ri = process_ec2(curr_ri_url)
3437
prices_prev = process_ec2(prev_url)
@@ -41,13 +44,40 @@ def __init__(self):
4144
for warning in price_warnings:
4245
self.output += "\n{}".format(warning.message)
4346

44-
def update_ri_prices(self, prices_curr_ri, kind):
47+
def update_ri_prices(self, prices_ri, kind):
48+
empty_ri = {
49+
'yrTerm1Standard': {
50+
'noUpfront': {
51+
'effectiveHourly': None,
52+
},
53+
'partialUpfront': {
54+
'effectiveHourly': None,
55+
},
56+
'allUpfront': {
57+
'effectiveHourly': None,
58+
},
59+
},
60+
'yrTerm3Standard': {
61+
'noUpfront': {
62+
'effectiveHourly': None,
63+
},
64+
'partialUpfront': {
65+
'effectiveHourly': None,
66+
},
67+
'allUpfront': {
68+
'effectiveHourly': None,
69+
},
70+
},
71+
}
4572
for region in self.prices[kind]:
46-
if region in prices_curr_ri[kind]:
47-
for instance_type in self.prices[kind][region]:
48-
if instance_type in prices_curr_ri[kind][region]:
49-
if 'ri' in prices_curr_ri[kind][region][instance_type]:
50-
self.prices[kind][region][instance_type]['ri'] = prices_curr_ri[kind][region][instance_type]['ri']
73+
for instance_type in self.prices[kind][region]:
74+
local_prices = self.prices[kind][region][instance_type]
75+
ri_type = prices_ri[kind][region].get(
76+
instance_type,
77+
{'ri': empty_ri},
78+
)
79+
if 'ri' in ri_type:
80+
local_prices['ri'] = ri_type['ri']
5181

5282
def update_previous_generation_prices(self, prices_prev, kind):
5383
for region in self.prices[kind]:
@@ -351,6 +381,7 @@ def process_not_implemented(data, js_name, instances=None):
351381
generic['name']))
352382
return instances
353383

384+
354385
SECTION_NAMES = {
355386
'linux-od.min.js': {
356387
'name': 'On Demand - Linux',

accloudtant/aws/reports.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from accloudtant.aws.prices import Prices
2323
import sys
2424

25+
2526
class Reports(object):
2627
def __init__(self, output_format, logger=None):
2728
if logger is None:
@@ -30,7 +31,7 @@ def __init__(self, output_format, logger=None):
3031
self.logger.addHandler(StreamHandler(sys.stdout))
3132
else:
3233
self.logger = logger
33-
self.output_format=output_format
34+
self.output_format = output_format
3435
ec2 = boto3.resource('ec2')
3536
ec2_client = boto3.client('ec2')
3637
self.counters = {
@@ -77,12 +78,16 @@ def find_reserved_instance(self):
7778
if instance.state == 'stopped':
7879
instance.current = 0.0
7980
try:
80-
instance_all_upfront = instance_size['ri']['yrTerm3Standard']['allUpfront']
81+
ri_price = instance_size['ri']['yrTerm3Standard']['allUpfront']
82+
instance_all_upfront = ri_price
8183
except KeyError:
8284
instance_all_upfront = {
8385
'effectiveHourly': 0.0,
8486
}
85-
instance.best = float(instance_all_upfront['effectiveHourly'])
87+
if instance_all_upfront['effectiveHourly'] is None:
88+
instance.best = 'N/A'
89+
else:
90+
instance.best = float(instance_all_upfront['effectiveHourly'])
8691
for reserved in self.reserved_instances:
8792
if instance.match_reserved_instance(reserved):
8893
instance.reserved = 'Yes'

tests/aws/test_reports.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
def get_future_date(years=1):
2121
return datetime.datetime.now() + datetime.timedelta(years)
2222

23+
2324
def test_reports_table(capsys, monkeypatch, ec2_resource, ec2_client,
2425
process_ec2):
2526
instances = {
@@ -654,8 +655,14 @@ def test_reports_table(capsys, monkeypatch, ec2_resource, ec2_client,
654655
assert(instance.best == mock['best'])
655656
assert(out == expected)
656657

657-
def test_reports_csv(capsys, monkeypatch, ec2_resource, ec2_client,
658-
process_ec2):
658+
659+
def test_reports_csv(
660+
capsys,
661+
monkeypatch,
662+
ec2_resource,
663+
ec2_client,
664+
process_ec2,
665+
):
659666
instances = {
660667
'instances': [{
661668
'id': 'i-912a4392',
@@ -1287,4 +1294,3 @@ def test_reports_csv(capsys, monkeypatch, ec2_resource, ec2_client,
12871294
assert(instance.current == mock['current'])
12881295
assert(instance.best == mock['best'])
12891296
assert(out == expected)
1290-

0 commit comments

Comments
 (0)