-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathupdate_specs_from_pricing.py
executable file
·244 lines (208 loc) · 9.78 KB
/
update_specs_from_pricing.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
#!/usr/bin/env python
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
"""
Updates our dynamic patches from the pricing API
This script requires Boto3 and Credentials to call the Pricing API
"""
import boto3
import json
import logging
LOGGER = logging.getLogger('cfnlint')
region_map = {
'Any': 'all',
'AWS GovCloud (US-East)': 'us-gov-east-1',
'AWS GovCloud (US-West)': 'us-gov-west-1',
'Africa (Cape Town)': 'af-south-1',
'Asia Pacific (Hong Kong)': 'ap-east-1',
'Asia Pacific (Mumbai)': 'ap-south-1',
'Asia Pacific (Osaka-Local)': 'ap-northeast-3',
'Asia Pacific (Seoul)': 'ap-northeast-2',
'Asia Pacific (Singapore)': 'ap-southeast-1',
'Asia Pacific (Sydney)': 'ap-southeast-2',
'Asia Pacific (Tokyo)': 'ap-northeast-1',
'Canada (Central)': 'ca-central-1',
'China (Beijing)': 'cn-north-1',
'China (Ningxia)': 'cn-northwest-1',
'EU (Frankfurt)': 'eu-central-1',
'EU (Ireland)': 'eu-west-1',
'EU (London)': 'eu-west-2',
'EU (Milan)': 'eu-south-1',
'EU (Paris)': 'eu-west-3',
'EU (Stockholm)': 'eu-north-1',
'Middle East (Bahrain)': 'me-south-1',
'South America (Sao Paulo)': 'sa-east-1',
'US East (N. Virginia)': 'us-east-1',
'US East (Ohio)': 'us-east-2',
'US West (N. California)': 'us-west-1',
'US West (Oregon)': 'us-west-2',
'US West (Los Angeles)': 'us-west-2',
}
session = boto3.session.Session()
client = session.client('pricing', region_name='us-east-1')
def configure_logging():
"""Setup Logging"""
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
LOGGER.setLevel(logging.INFO)
log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(log_formatter)
# make sure all other log handlers are removed before adding it back
for handler in LOGGER.handlers:
LOGGER.removeHandler(handler)
LOGGER.addHandler(ch)
def update_outputs(key, values, outputs):
""" update outputs with appropriate results """
for region in values:
element = {
"op": "add",
"path": "/ValueTypes/%s/AllowedValues" % key,
"value": sorted(values[region])
}
outputs[region].append(element)
return outputs
def get_paginator(service):
LOGGER.info('Get ' + service + ' pricing')
return client.get_paginator('get_products').paginate(
ServiceCode=service,
FormatVersion='aws_v1',
)
def get_dax_pricing():
results = {}
for page in get_paginator('AmazonDAX'):
for price_item in page.get('PriceList', []):
products = json.loads(price_item)
product = products.get('product', {})
if product:
if product.get('productFamily') in ['DAX']:
if not results.get(region_map[product.get('attributes').get('location')]):
results[region_map[product.get('attributes').get('location')]] = set()
results[region_map[product.get('attributes').get('location')]].add(
product.get('attributes').get('usagetype').split(':')[1]
)
return results
def get_mq_pricing():
remap = {
'mq.m5.2xl': 'mq.m5.2xlarge',
'mq.m5.4xl': 'mq.m5.4xlarge'
}
results = {}
for page in get_paginator('AmazonMQ'):
for price_item in page.get('PriceList', []):
products = json.loads(price_item)
product = products.get('product', {})
if product:
if product.get('productFamily') in ['Broker Instances']:
if not results.get(region_map[product.get('attributes').get('location')]):
results[region_map[product.get('attributes').get('location')]] = set()
usage_type = product.get('attributes').get('usagetype').split(':')[1]
results[region_map[product.get('attributes').get('location')]].add(
remap.get(usage_type, usage_type)
)
return results
def get_rds_pricing():
product_map = {
'2': ['mysql'],
'3': ['oracle-se1'],
'4': ['oracle-se'],
'5': ['oracle-ee'],
'6': ['oracle-se1'],
'8': ['sqlserver-se'],
'9': ['sqlserver-ee'],
'10': ['sqlserver-ex'],
'11': ['sqlserver-web'],
'12': ['sqlserver-se'],
'14': ['postgres'],
'15': ['sqlserver-ee'],
'16': ['aurora-mysql', 'aurora'],
'18': ['mariadb'],
'19': ['oracle-se2'],
'20': ['oracle-se2'],
'21': ['aurora-postgresql'],
}
license_map = {
'License included': 'license-included',
'Bring your own license': 'bring-your-own-license',
'No license required': 'general-public-license'
}
rds_specs = {}
results = {}
for page in get_paginator('AmazonRDS'):
for price_item in page.get('PriceList', []):
products = json.loads(price_item)
product = products.get('product', {})
if product:
if product.get('productFamily') in ['Database Instance']:
# Get overall instance types
if not results.get(region_map[product.get('attributes').get('location')]):
results[region_map[product.get('attributes').get('location')]] = set()
results[region_map[product.get('attributes').get('location')]].add(
product.get('attributes').get('instanceType')
)
# Rds Instance Size spec
product_names = product_map.get(product.get('attributes').get('engineCode'), [])
product_region = region_map.get(product.get('attributes').get('location'))
license_name = license_map.get(product.get('attributes').get('licenseModel'))
instance_type = product.get('attributes').get('instanceType')
for product_name in product_names:
if not rds_specs.get(license_name):
rds_specs[license_name] = {}
if not rds_specs.get(license_name).get(product_name):
rds_specs[license_name][product_name] = {}
if not rds_specs.get(license_name).get(product_name).get(product_region):
rds_specs[license_name][product_name][product_region] = set()
rds_specs[license_name][product_name][product_region].add(instance_type)
for license_name, license_values in rds_specs.items():
for product_name, product_values in license_values.items():
for product_region, instance_types in product_values.items():
rds_specs[license_name][product_name][product_region] = list(sorted(instance_types))
LOGGER.info('Updating RDS Spec files')
filename = 'src/cfnlint/data/AdditionalSpecs/RdsProperties.json'
with open(filename, 'w+') as f:
json.dump(rds_specs, f, indent=2, sort_keys=True, separators=(',', ': '))
return results
def get_results(service, product_families):
results = {}
for page in get_paginator(service):
for price_item in page.get('PriceList', []):
products = json.loads(price_item)
product = products.get('product', {})
if product:
if product.get('productFamily') in product_families:
if not results.get(region_map[product.get('attributes').get('location')]):
results[region_map[product.get('attributes').get('location')]] = set()
results[region_map[product.get('attributes').get('location')]].add(
product.get('attributes').get('instanceType')
)
return results
def main():
""" main function """
configure_logging()
outputs = {}
for region in region_map.values():
outputs[region] = []
outputs = update_outputs('Ec2InstanceType', get_results('AmazonEC2', ['Compute Instance', 'Compute Instance (bare metal)']), outputs)
outputs = update_outputs('AWS::AmazonMQ::Broker.HostInstanceType', get_mq_pricing(), outputs)
outputs = update_outputs('RdsInstanceType', get_rds_pricing(), outputs)
outputs = update_outputs('RedshiftInstanceType', get_results('AmazonRedshift', ['Compute Instance']), outputs)
outputs = update_outputs('DAXInstanceType', get_dax_pricing(), outputs)
outputs = update_outputs('DocumentDBInstanceClass', get_results('AmazonDocDB', ['Database Instance']), outputs)
outputs = update_outputs('NeptuneInstanceClass', get_results('AmazonNeptune', ['Database Instance']), outputs)
outputs = update_outputs('ElastiCacheInstanceType', get_results('AmazonElastiCache', ['Cache Instance']), outputs)
outputs = update_outputs('ElasticsearchInstanceType', get_results('AmazonES', ['Elastic Search Instance']), outputs)
outputs = update_outputs('EMRInstanceType', get_results('ElasticMapReduce', ['Elastic Map Reduce Instance']), outputs)
outputs = update_outputs('BlockchainInstanceType', get_results('AmazonManagedBlockchain', ['Blockchain Instance']), outputs)
outputs = update_outputs('GameLiftInstanceType', get_results('AmazonGameLift', ['GameLift EC2 Instance']), outputs)
outputs = update_outputs('AppStreamInstanceType', get_results('AmazonAppStream', ['Streaming Instance']), outputs)
LOGGER.info('Updating spec files')
for region, patches in outputs.items():
filename = 'src/cfnlint/data/ExtendedSpecs/%s/05_pricing_property_values.json' % region
with open(filename, 'w+') as f:
json.dump(patches, f, indent=2, sort_keys=True, separators=(',', ': '))
if __name__ == '__main__':
try:
main()
except (ValueError, TypeError):
LOGGER.error(ValueError)