-
Notifications
You must be signed in to change notification settings - Fork 54
/
offers.py
606 lines (506 loc) · 24.6 KB
/
offers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from collections import defaultdict
import logging
from typing import Any, Dict, List, Optional, Set, Type # noqa
import six
from .constants import (
REGION_SHORTS,
EC2_LEASE_CONTRACT_LENGTH,
EC2_OFFERING_CLASS,
EC2_PURCHASE_OPTION,
RDS_LEASE_CONTRACT_LENGTH,
RDS_OFFERING_CLASS,
RDS_PURCHASE_OPTION
)
OFFER_CLASS_MAP = {}
logger = logging.getLogger(__name__)
def implements(offer_name):
"""Decorator to keep track of offer-specific class implementations."""
def wrapper(cls):
OFFER_CLASS_MAP[offer_name] = cls
return cls
return wrapper
def get_offer_class(offer_name): # type: (str) -> Type[AWSOffer]
return OFFER_CLASS_MAP.get(offer_name, AWSOffer)
class AWSOffer(object):
def __init__(self, offer_data): # type: (Dict[str, Any]) -> None
self._offer_data = offer_data
self.default_region = None # type: Optional[str]
@property
def raw(self):
"""The entire underlying offer data."""
return self._offer_data
def search_skus(self, **attributes): # type: (**str) -> Set[str]
"""Search for all SKUs matching the given attributes.
Note that attributes given in pythonic snake_case notation will
automatically be converted to camelCase to match AWS pricing data
convention.
:return: a set of matching SKUs
"""
attributes = self._pythonify_attributes(attributes)
result = set()
for sku, product in six.iteritems(self._offer_data['products']):
product_attributes = product['attributes']
all_match = True
for attr_name, attr_value in six.iteritems(attributes):
if not product_attributes.get(attr_name) == attr_value:
all_match = False
break
if all_match:
result.add(sku)
return result
@staticmethod
def _pythonify_attributes(attributes):
# type: (Dict[str, str]) -> Dict[str, str]
result = {}
for attr_name, attr_value in six.iteritems(attributes):
if '_' in attr_name:
attr_name = ''.join(w.capitalize() or '_'
for w in attr_name.split('_'))
attr_name = attr_name[0].lower() + attr_name[1:]
result[attr_name] = attr_value
return result
def _normalize_region(self, region): # type: (Optional[str]) -> str
region = region or self.default_region
if not region:
raise ValueError("No region is set.")
if region in REGION_SHORTS: # Use long-name to match pricing API
region = REGION_SHORTS[region]
return region
@staticmethod
def hash_attributes(*attributes): # type: (*str) -> str
"""Generate a hash for attributes to use in reverse mappings."""
return '|'.join(attributes)
def _generate_reverse_sku_mapping(self,
*attribute_names, # type: str
**kwargs # type: Dict[str, str]
):
# type: (...) -> Dict[str, str]
"""Generate a reverse mapping from a hash of product attributes to SKU.
Only hashes that are unique across all products will be included in the
result; products which collide on hash will be discarded.
"""
product_families = kwargs.get('product_families')
result = {} # type: Dict[str, str]
# There are cases where the set of attributes are not unique across all
# products and cause hash collisions. In these cases, we take note of
# the collision and do not include these products in the mapping.
attribute_collisions = set()
for sku, product in six.iteritems(self._offer_data['products']):
# Introduced for Data transfer SKU's that are not like regular EC2 offers
try:
if product_families and product['productFamily'] not in product_families:
continue
except KeyError:
continue
attrs = [product['attributes'][attr]
for attr in attribute_names if attr in product['attributes']]
key = self.hash_attributes(*attrs)
if key in result:
# There is an attribute collision, so do not include any of
# these results in the reverse mapping.
attribute_collisions.add(key)
del result[key]
elif key not in attribute_collisions:
result[key] = sku
logger.debug('Discarded {} products when generating reverse mapping.'
.format(len(attribute_collisions)))
return result
@implements('AmazonEC2')
class EC2Offer(AWSOffer):
HOURS_IN_YEAR = 24 * 365
def __init__(self, *args, **kwargs):
super(EC2Offer, self).__init__(*args, **kwargs)
self.default_operating_system = None
self.default_tenancy = 'Shared'
self.default_license_model = 'No License required'
self.default_preinstalled_software = 'NA'
self._reverse_sku = self._generate_reverse_sku_mapping(
'instanceType', 'operatingSystem', 'tenancy', 'licenseModel',
'preInstalledSw', 'location',
# Both families are queried assuming that instance names will never clash between
# them. This should be true given metal instance naming conventions thus far (instance
# size is 'metal').
product_families=['Compute Instance', 'Compute Instance (bare metal)']
)
# Lazily-loaded cache to hold offerTermCodes within a SKU
self._reserved_terms_to_offer_term_code = defaultdict(dict)
def get_sku(self,
instance_type, # type: str
operating_system=None, # type: Optional[str]
tenancy=None, # type: Optional[str]
license_model=None, # type: Optional[str]
preinstalled_software=None, # type: Optional[str]
region=None # type: Optional[str]
):
# type: (...) -> str
region = self._normalize_region(region)
operating_system = operating_system or self.default_operating_system
tenancy = tenancy or self.default_tenancy
license_model = license_model or self.default_license_model
preinstalled_software = (preinstalled_software or
self.default_preinstalled_software)
attributes = [instance_type, operating_system, tenancy, license_model,
preinstalled_software, region]
if not all(attributes):
raise ValueError("All attributes are required: {}"
.format(attributes))
sku = self._reverse_sku.get(self.hash_attributes(*attributes))
if sku is None:
raise ValueError("Unable to lookup SKU for attributes: {}"
.format(attributes))
return sku
def ondemand_hourly(self,
instance_type, # type: str
operating_system=None, # type: Optional[str]
tenancy=None, # type: Optional[str]
license_model=None, # type: Optional[str]
preinstalled_software=None, # type: Optional[str]
region=None, # type: Optional[str]
):
# type: (...) -> float
sku = self.get_sku(
instance_type,
operating_system=operating_system,
tenancy=tenancy,
license_model=license_model,
preinstalled_software=preinstalled_software,
region=region
)
term = self._offer_data['terms']['OnDemand'][sku]
price_dimensions = next(six.itervalues(term))['priceDimensions']
price_dimension = next(six.itervalues(price_dimensions))
raw_price = price_dimension['pricePerUnit']['USD']
return float(raw_price)
def reserved_hourly(self,
instance_type, # type: str
operating_system=None, # type: Optional[str]
tenancy=None, # type: Optional[str]
license_model=None, # type: Optional[str]
preinstalled_software=None, # type: Optional[str]
lease_contract_length=None, # type: Optional[str]
offering_class=EC2_OFFERING_CLASS.STANDARD, # type: str
purchase_option=None, # type: Optional[str]
amortize_upfront=True, # type: bool
region=None, # type: Optional[str]
):
# type: (...) -> float
self._validate_reserved_price_args(
lease_contract_length, offering_class, purchase_option)
assert lease_contract_length is not None
assert offering_class is not None
assert purchase_option is not None
sku = self.get_sku(
instance_type,
operating_system=operating_system,
tenancy=tenancy,
license_model=license_model,
preinstalled_software=preinstalled_software,
region=region,
)
term_attributes = [
lease_contract_length,
offering_class,
purchase_option
]
term = self._get_reserved_offer_term(sku, term_attributes)
price_dimensions = term['priceDimensions'].values()
hourly_dimension = next(d for d in price_dimensions
if d['unit'].lower() == 'hrs')
upfront_dimension = next((d for d in price_dimensions
if d['description'] == 'Upfront Fee'), None)
raw_hourly = hourly_dimension['pricePerUnit']['USD']
raw_upfront = upfront_dimension['pricePerUnit']['USD'] if upfront_dimension else 0
hourly = float(raw_hourly)
upfront = float(raw_upfront)
if amortize_upfront:
hours = self._get_hours_in_lease_contract_length(
lease_contract_length)
hourly += (upfront / hours)
return hourly
def _get_reserved_offer_term(self, sku, term_attributes):
# type: (str, List[str]) -> Dict[str, Any]
term_attributes_hash = self.hash_attributes(*term_attributes)
all_terms = self._offer_data['terms']['Reserved'][sku]
sku_terms = self._reserved_terms_to_offer_term_code[sku]
if term_attributes_hash not in sku_terms:
for term_sku, term in six.iteritems(all_terms):
hashed = self._hash_reserved_term_attributes(term)
sku_terms[hashed] = term['offerTermCode']
code = sku_terms[term_attributes_hash]
return all_terms['.'.join([sku, code])]
def _hash_reserved_term_attributes(self, term):
attrs = term['termAttributes']
return self.hash_attributes(
attrs['LeaseContractLength'],
attrs['OfferingClass'],
attrs['PurchaseOption']
)
@classmethod
def _get_hours_in_lease_contract_length(cls, lease_contract_length):
if lease_contract_length == '1yr':
return cls.HOURS_IN_YEAR
elif lease_contract_length == '3yr':
return 3 * cls.HOURS_IN_YEAR
raise ValueError("Unknown lease contract length: {}"
.format(lease_contract_length))
def reserved_upfront(self,
instance_type, # type: str
operating_system=None, # type: Optional[str]
tenancy=None, # type: Optional[str]
license_model=None, # type: Optional[str]
preinstalled_software=None, # type: Optional[str]
lease_contract_length=None, # type: Optional[str]
offering_class=EC2_OFFERING_CLASS.STANDARD, # type: str
purchase_option=None, # type: Optional[str]
region=None, # type: Optional[str]
):
# type: (...) -> float
self._validate_reserved_price_args(
lease_contract_length, offering_class, purchase_option)
assert lease_contract_length is not None
assert offering_class is not None
assert purchase_option is not None
sku = self.get_sku(
instance_type,
operating_system=operating_system,
tenancy=tenancy,
license_model=license_model,
preinstalled_software=preinstalled_software,
region=region,
)
term_attributes = [
lease_contract_length,
offering_class,
purchase_option
]
term = self._get_reserved_offer_term(sku, term_attributes)
price_dimensions = term['priceDimensions'].values()
upfront_dimension = next((d for d in price_dimensions
if d['description'] == 'Upfront Fee'), None)
raw_upfront = upfront_dimension['pricePerUnit']['USD'] if upfront_dimension else 0
return float(raw_upfront)
@classmethod
def _validate_reserved_price_args(cls,
lease_contract_length, # type: Optional[str]
offering_class, # type: Optional[str]
purchase_option, # type: Optional[str]
):
# type: (...) -> None
if lease_contract_length not in EC2_LEASE_CONTRACT_LENGTH.values():
valid_options = EC2_LEASE_CONTRACT_LENGTH.values()
raise ValueError(
"Lease contract '{}' is invalid. Valid options are: {}"
.format(lease_contract_length, valid_options)
)
if offering_class not in EC2_OFFERING_CLASS.values():
valid_options = EC2_OFFERING_CLASS.values()
raise ValueError(
"Offering class '{}' is invalid. Valid options are: {}"
.format(offering_class, valid_options)
)
if purchase_option not in EC2_PURCHASE_OPTION.values():
valid_options = EC2_PURCHASE_OPTION.values()
raise ValueError(
"Purchase option '{}' is invalid. Valid options are: {}"
.format(purchase_option, valid_options)
)
if (lease_contract_length == EC2_LEASE_CONTRACT_LENGTH.ONE_YEAR and
offering_class == 'convertible'):
raise ValueError("The convertible offering class is not available "
"on a 1year lease.")
@implements('AmazonRDS')
class RDSOffer(AWSOffer):
HOURS_IN_YEAR = 24 * 365
def __init__(self, *args, **kwargs):
super(RDSOffer, self).__init__(*args, **kwargs)
self.default_deployment_option = 'Single-AZ'
self._reverse_sku = self._generate_reverse_sku_mapping(
'instanceType',
'databaseEngine',
'deploymentOption',
'licenseModel',
'location',
'databaseEdition',
product_families=['Database Instance']
)
# Lazily-loaded cache to hold offerTermCodes within a SKU
self._reserved_terms_to_offer_term_code = defaultdict(dict) # type: Dict[str, Dict]
def get_sku(self,
instance_type, # type: str
database_engine, # type: str
license_model=None, # type: Optional[str]
deployment_option=None, # type: Optional[str]
database_edition=None, # type: Optional[str]
region=None # type: Optional[str]
):
region = self._normalize_region(region)
deployment_option = deployment_option or self.default_deployment_option
if license_model is None:
raise ValueError("License model is required")
attributes = [instance_type, database_engine,
deployment_option, license_model, region] # type: List[str]
if database_edition is not None:
attributes.append(database_edition)
if not all(attributes):
raise ValueError("All attributes are required: {}"
.format(attributes))
sku = self._reverse_sku.get(self.hash_attributes(*attributes))
if sku is None:
raise ValueError("Unable to lookup SKU for attributes: {}"
.format(attributes))
return sku
def ondemand_hourly(self,
instance_type, # type: str
database_engine, # type: str
license_model=None, # type: str
deployment_option=None, # type: Optional[str]
database_edition=None, # type: Optional[str]
region=None # type: Optional[str]
):
# type: (...) -> float
sku = self.get_sku(
instance_type,
database_engine,
deployment_option=deployment_option,
license_model=license_model,
database_edition=database_edition,
region=region
)
term = self._offer_data['terms']['OnDemand'][sku]
price_dimensions = next(six.itervalues(term))['priceDimensions']
price_dimension = next(six.itervalues(price_dimensions))
raw_price = price_dimension['pricePerUnit']['USD']
return float(raw_price)
def reserved_hourly(self,
instance_type, # type: str
database_engine, # type: str
license_model=None, # type: str
deployment_option=None, # type: Optional[str]
lease_contract_length=None, # type: Optional[str]
offering_class=RDS_OFFERING_CLASS.STANDARD, # type: str
purchase_option=None, # type: Optional[str]
amortize_upfront=True, # type: bool
database_edition=None, # type: Optional[str]
region=None, # type: Optional[str]
):
# type: (...) -> float
self._validate_reserved_price_args(
lease_contract_length, offering_class, purchase_option)
assert lease_contract_length is not None
assert offering_class is not None
assert purchase_option is not None
sku = self.get_sku(
instance_type,
database_engine,
deployment_option=deployment_option,
license_model=license_model,
database_edition=database_edition,
region=region,
)
term_attributes = [
lease_contract_length,
offering_class,
purchase_option
]
term = self._get_reserved_offer_term(sku, term_attributes)
price_dimensions = term['priceDimensions'].values()
hourly_dimension = next(d for d in price_dimensions
if d['unit'].lower() == 'hrs')
upfront_dimension = next((d for d in price_dimensions
if d['description'] == 'Upfront Fee'), None)
raw_hourly = hourly_dimension['pricePerUnit']['USD']
raw_upfront = upfront_dimension['pricePerUnit']['USD'] if upfront_dimension else 0
hourly = float(raw_hourly)
upfront = float(raw_upfront)
if amortize_upfront:
hours = self._get_hours_in_lease_contract_length(
lease_contract_length)
hourly += (upfront / hours)
return hourly
def _get_reserved_offer_term(self, sku, term_attributes):
# type: (str, List[str]) -> Dict[str, Any]
term_attributes_hash = self.hash_attributes(*term_attributes)
all_terms = self._offer_data['terms']['Reserved'][sku]
sku_terms = self._reserved_terms_to_offer_term_code[sku]
if term_attributes_hash not in sku_terms:
for term_sku, term in six.iteritems(all_terms):
hashed = self._hash_reserved_term_attributes(term)
sku_terms[hashed] = term['offerTermCode']
code = sku_terms[term_attributes_hash]
return all_terms['.'.join([sku, code])]
def _hash_reserved_term_attributes(self, term):
attrs = term['termAttributes']
return self.hash_attributes(
attrs['LeaseContractLength'],
attrs['OfferingClass'],
attrs['PurchaseOption']
)
@classmethod
def _get_hours_in_lease_contract_length(cls, lease_contract_length):
if lease_contract_length == '1yr':
return cls.HOURS_IN_YEAR
elif lease_contract_length == '3yr':
return 3 * cls.HOURS_IN_YEAR
raise ValueError("Unknown lease contract length: {}"
.format(lease_contract_length))
def reserved_upfront(self,
instance_type, # type: str
database_engine, # type: str
license_model=None, # type: str
deployment_option=None, # type: Optional[str]
lease_contract_length=None, # type: Optional[str]
offering_class=RDS_OFFERING_CLASS.STANDARD, # type: str
purchase_option=None, # type: Optional[str]
database_edition=None, # type: Optional[str]
region=None, # type: Optional[str]
):
# type: (...) -> float
self._validate_reserved_price_args(
lease_contract_length, offering_class, purchase_option)
assert lease_contract_length is not None
assert offering_class is not None
assert purchase_option is not None
sku = self.get_sku(
instance_type,
database_engine,
deployment_option=deployment_option,
license_model=license_model,
database_edition=database_edition,
region=region,
)
term_attributes = [
lease_contract_length,
offering_class,
purchase_option
]
term = self._get_reserved_offer_term(sku, term_attributes)
price_dimensions = term['priceDimensions'].values()
upfront_dimension = next((d for d in price_dimensions
if d['description'] == 'Upfront Fee'), None)
raw_upfront = upfront_dimension['pricePerUnit']['USD'] if upfront_dimension else 0
return float(raw_upfront)
@classmethod
def _validate_reserved_price_args(cls,
lease_contract_length, # type: Optional[str]
offering_class, # type: str
purchase_option, # type: Optional[str]
):
# type: (...) -> None
if lease_contract_length not in RDS_LEASE_CONTRACT_LENGTH.values():
valid_options = RDS_LEASE_CONTRACT_LENGTH.values()
raise ValueError(
"Lease contract '{}' is invalid. Valid options are: {}"
.format(lease_contract_length, valid_options)
)
if offering_class not in RDS_OFFERING_CLASS.values():
valid_options = RDS_OFFERING_CLASS.values()
raise ValueError(
"Offering class '{}' is invalid. Valid options are: {}"
.format(offering_class, valid_options)
)
if purchase_option not in RDS_PURCHASE_OPTION.values():
valid_options = RDS_PURCHASE_OPTION.values()
raise ValueError(
"Purchase option '{}' is invalid. Valid options are: {}"
.format(purchase_option, valid_options)
)