This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
rds-price-list.rb
312 lines (284 loc) · 14.4 KB
/
rds-price-list.rb
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
module AwsPricing
class RdsPriceList < PriceList
def initialize
super
InstanceType.populate_lookups
get_rds_on_demand_instance_pricing
get_rds_reserved_instance_pricing2
get_rds_reserved_instance_pricing
end
protected
# NOTE if you add to DB_TYPE, make sure to update self.get_database_name
# in amazon-pricing/lib/amazon-pricing/definitions/database-type.rb
@@DB_TYPE = [:mysql, :postgresql, :oracle, :sqlserver, :aurora, :mariadb]
@@RES_TYPES = [:light, :medium, :heavy]
@@OD_DB_DEPLOY_TYPE = {
:mysql => {:mysql=>["standard","multiAZ"]},
:postgresql => {:postgresql=>["standard","multiAZ"]},
:oracle => {
# AWS: Oracle - Enterprise Edition, Standard Edition Two, Standard Edition, Standard Edition One.
# License Included Charges supported: Standard Edition One, Standard Edition Two
# BYOL Charges supported: do not vary by edition for BYOL Amazon RDS pricing.
# NB: since we explicitly copy Oracle BYOL RdsInstanceType.update_pricing_new, repeated BYOL is redundant here;
# only '#{db}' is used to build the URL, so repeated '#{db_type}' is like copying
:oracle_se =>["byol-standard", "byol-multiAZ"],
#:oracle_ee =>["byol-standard", "byol-multiAZ"], # byol is same, so copied
:oracle_se1=>["li-standard", "li-multiAZ"], # byol is same, so copied
:oracle_se2=>["se2-li-standard", "se2-li-multiAZ"] # byol is same, so copied
},
:sqlserver => {
:sqlserver_ex=>["li-ex"],
:sqlserver_web=>["li-web"],
:sqlserver_se=>["li-se", "li-se-multiAZ", "byol", "byol-multiAZ"],
:sqlserver_ee=>["byol", "byol-multiAZ", "li-ee", 'li-ee-multiAZ']
},
:aurora => { :aurora => ["multiAZ"] },
:mariadb => { :mariadb => ["standard", "multiAZ"] }
}
@@RESERVED_DB_DEPLOY_TYPE = {
:oracle=> {:oracle_se1=>["li","byol"], :oracle_se=>["byol"], :oracle_ee=>["byol"]},
:sqlserver=> {:sqlserver_ex=>["li-ex"], :sqlserver_web=>["li-web"], :sqlserver_se=>["li-se","byol"], :sqlserver_ee=>["byol"]}
}
# the following cli will extract all the URLs referenced on the AWS pricing page that fetch_reserved_rds_instance_pricing2 uses:
# curl https://aws.amazon.com/rds/pricing/ 2>/dev/null | grep 'model' |grep reserved-instances
# NB: the URL is built using '#{db_str}-#{deploy_type}', so repeats for 'db' below are required
@@RESERVED_DB_DEPLOY_TYPE2 = {
:mysql => {:mysql=>["standard","multiAZ"]},
:postgresql => {:postgresql=>["standard","multiAZ"]},
:oracle => {
# AWS: Oracle - Enterprise Edition, Standard Edition Two, Standard Edition, Standard Edition One.
# License Included Charges supported: Standard Edition One, Standard Edition Two
# BYOL Charges supported: do not vary by edition for BYOL Amazon RDS pricing.
:oracle_se =>["byol-standard", "byol-multiAZ"],
# oracle_ee is not here (nor collected), see RESERVED_DB_WITH_SAME_PRICING2 below
# :oracle_se =>["byol-standard", "byol-multiAZ"], # again, note only byol (so not needed)
:oracle_se1=>["license-included-standard", "license-included-multiAZ"],
:oracle_se2=>["license-included-standard", "license-included-multiAZ"]
},
:sqlserver => {
:sqlserver_ex =>["license-included-standard"],
:sqlserver_web=>["license-included-standard", "license-included-multiAZ"],
:sqlserver_se =>["byol-standard", "byol-multiAZ", "license-included-standard", "license-included-multiAZ"],
:sqlserver_ee =>["byol-standard", "byol-multiAZ", "license-included-standard", "license-included-multiAZ"]},
:aurora => {:aurora => ["standard", "multiAZ"]},
:mariadb => {:mariadb => ["standard", "multiAZ"]}
}
@@RESERVED_DB_WITH_SAME_PRICING2 = {
:mysql => [:mysql],
:postgresql => [:postgresql],
:oracle_se2 => [:oracle_se2],
:oracle_se1 => [:oracle_se1],
:oracle_se => [:oracle_se, :oracle_ee],
:sqlserver_ex => [:sqlserver_ex],
:sqlserver_web=> [:sqlserver_web],
:sqlserver_se => [:sqlserver_se],
:sqlserver_ee => [:sqlserver_ee],
:aurora => [:aurora],
:mariadb => [:mariadb]
}
# old RI pricing was broken out by utilization levels: light, medium & heavy.
# this data is not available for new offerings
@@NO_LEGACY_RI_PRICING_AVAILABLE = [:aurora, :mariadb]
def is_multi_az?(type)
return true if type.upcase.match("MULTI-AZ")
false
end
def is_byol?(type)
return true if type.match("byol")
false
end
def get_rds_on_demand_instance_pricing
@@DB_TYPE.each do |db|
@@OD_DB_DEPLOY_TYPE[db].each {|db_type, db_instances|
db_instances.each do |dp_type|
#
# to find out the byol type
is_byol = is_byol? dp_type
# We believe Amazon made a mistake by hosting aurora's prices on a url that follows the multi-az pattern.
# Therefore we'll construct the URL as multi-az, but still treat the prices as for single-az.
is_multi_az = dp_type.upcase.include?("MULTIAZ") && db != :aurora
dp_type = dp_type.gsub('-multiAZ', '') if db == :sqlserver
if [:mysql, :postgresql, :oracle, :aurora, :mariadb].include? db
fetch_on_demand_rds_instance_pricing(RDS_BASE_URL+"#{db}/pricing-#{dp_type}-deployments.min.js",:ondemand, db_type, is_byol, is_multi_az)
elsif db == :sqlserver
if is_multi_az
fetch_on_demand_rds_instance_pricing(RDS_BASE_URL+"#{db}/sqlserver-#{dp_type}-ondemand-maz.min.js",:ondemand, db_type, is_byol, is_multi_az)
else
fetch_on_demand_rds_instance_pricing(RDS_BASE_URL+"#{db}/sqlserver-#{dp_type}-ondemand.min.js",:ondemand, db_type, is_byol, is_multi_az)
end
end
# Now repeat for legacy instances
if [:mysql, :postgresql, :oracle].include? db
fetch_on_demand_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/pricing-#{dp_type}-deployments.min.js",:ondemand, db_type, is_byol, is_multi_az)
elsif db == :sqlserver
next if dp_type == 'li-ee' || dp_type == 'li-ee-multiAZ'
if is_multi_az
fetch_on_demand_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/sqlserver-#{dp_type}-ondemand-maz.min.js",:ondemand, db_type, is_byol, is_multi_az)
else
fetch_on_demand_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/sqlserver-#{dp_type}-ondemand.min.js",:ondemand, db_type, is_byol, is_multi_az)
end
end
end
}
end
end
def get_rds_reserved_instance_pricing2
@@DB_TYPE.each do |db_name|
@@RESERVED_DB_DEPLOY_TYPE2[db_name].each do |db, deploy_types|
deploy_types.each do |deploy_type|
is_byol = is_byol? deploy_type
# We believe Amazon made a mistake by hosting aurora's prices on a url that follows the multi-az pattern.
# Therefore we'll construct the URL as multi-az, but still treat the prices as for single-az.
is_multi_az = deploy_type.upcase.include?("MULTIAZ") && db != :aurora
# map sqlserver to aws strings (notice '-' between sql-server)
case db
when :sqlserver_se
db_str = 'sql-server-se'
when :sqlserver_ee
db_str = 'sql-server-ee'
when :sqlserver_web
db_str = 'sql-server-web'
when :sqlserver_ex
db_str = 'sql-server-express'
else
db_str = db.to_s.gsub(/_/, '-')
end
# nb: the intersection of @@RESERVED_DB_DEPLOY_TYPE2 and @@RESERVED_DB_WITH_SAME_PRICING2 should
# not overlap, since they have different pricings
dbs_with_same_pricing = @@RESERVED_DB_WITH_SAME_PRICING2[db]
fetch_reserved_rds_instance_pricing2(RDS_BASE_URL+"reserved-instances/#{db_str}-#{deploy_type}.min.js", dbs_with_same_pricing, is_multi_az, is_byol)
end
end
end
end
def fetch_reserved_rds_instance_pricing2(url, dbs, is_multi_az, is_byol)
#logger.debug "[#{__method__}] fetched #{url}"
res = PriceList.fetch_url(url)
res['config']['regions'].each do |reg|
region_name = reg['region']
region = get_region(region_name)
if region.nil?
$stderr.puts "[fetch_reserved_rds_instance_pricing2] WARNING: unable to find region #{region_name}"
next
end
reg['instanceTypes'].each do |type|
api_name = type["type"]
instance_type = region.get_rds_instance_type(api_name)
if instance_type.nil?
$stderr.puts "[fetch_reserved_rds_instance_pricing2] WARNING: new reserved instances not found for #{api_name} in #{region_name}"
next
end
type["terms"].each do |term|
term["purchaseOptions"].each do |option|
case option["purchaseOption"]
when "noUpfront"
reservation_type = :noupfront
when "allUpfront"
reservation_type = :allupfront
when "partialUpfront"
reservation_type = :partialupfront
end
duration = term["term"]
prices = option["valueColumns"]
dbs.each do |db|
instance_type.update_pricing_new(db, reservation_type, prices, duration, is_multi_az, is_byol)
end
end
end
end
end
end
def get_rds_reserved_instance_pricing
@@DB_TYPE.each do |db|
next if @@NO_LEGACY_RI_PRICING_AVAILABLE.include? db
if [:mysql, :postgresql].include? db
@@RES_TYPES.each do |res_type|
if db == :postgresql and res_type == :heavy
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/pricing-#{res_type}-utilization-reserved-instances.min.js", res_type, db, false)
elsif db == :mysql
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/pricing-#{res_type}-utilization-reserved-instances.min.js", res_type, db, false)
end
# Now repeat for legacy instances
if db == :postgresql and res_type == :heavy
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/pricing-#{res_type}-utilization-reserved-instances.min.js", res_type, db, false)
elsif db == :mysql
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/pricing-#{res_type}-utilization-reserved-instances.min.js", res_type, db, false)
end
end
else
@@RESERVED_DB_DEPLOY_TYPE[db].each {|db_type, db_instance|
@@RES_TYPES.each do |res_type|
db_instance.each do |dp_type|
is_byol = is_byol? dp_type
if db == :oracle
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/pricing-#{dp_type}-#{res_type}-utilization-reserved-instances.min.js", res_type, db_type, is_byol)
elsif db == :sqlserver
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/sqlserver-#{dp_type}-#{res_type}-ri.min.js", res_type, db_type, is_byol)
end
# Now repeat for legacy instances
if db == :oracle
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/pricing-#{dp_type}-#{res_type}-utilization-reserved-instances.min.js", res_type, db_type, is_byol)
elsif db == :sqlserver
fetch_reserved_rds_instance_pricing(RDS_BASE_URL+"#{db}/previous-generation/sqlserver-#{dp_type}-#{res_type}-ri.min.js", res_type, db_type, is_byol)
end
end
end
}
end
end
end
def fetch_on_demand_rds_instance_pricing(url, type_of_rds_instance, db_type, is_byol, is_multi_az = false)
#logger.debug "[#{__method__}] fetched #{url}"
res = PriceList.fetch_url(url)
res['config']['regions'].each do |reg|
region_name = reg['region']
region = get_region(region_name)
if region.nil?
$stderr.puts "[fetch_on_demand_rds_instance_pricing] WARNING: unable to find region #{region_name}"
next
end
reg['types'].each do |type|
type['tiers'].each do |tier|
begin
#
# this is special case URL, it is oracle - multiAZ type of deployment but it doesn't have mutliAZ attributes in json.
#if url == "http://aws.amazon.com/rds/pricing/oracle/pricing-li-multiAZ-deployments.min.js"
# is_multi_az = true
#else
# is_multi_az = is_multi_az? type["name"]
#end
api_name, name = RdsInstanceType.get_name(type["name"], tier["name"], type_of_rds_instance != :ondemand)
instance_type = region.add_or_update_rds_instance_type(api_name, name)
instance_type.update_pricing(db_type, type_of_rds_instance, tier, is_multi_az, is_byol)
rescue UnknownTypeError
$stderr.puts "[fetch_on_demand_rds_instance_pricing] WARNING: encountered #{$!.message}"
end
end
end
end
end
def fetch_reserved_rds_instance_pricing(url, type_of_rds_instance, db_type, is_byol)
#logger.debug "[#{__method__}] fetched #{url}"
res = PriceList.fetch_url(url)
res['config']['regions'].each do |reg|
region_name = reg['region']
region = get_region(region_name)
reg['instanceTypes'].each do |type|
type['tiers'].each do |tier|
begin
is_multi_az = is_multi_az? type["type"]
api_name, name = RdsInstanceType.get_name(type["type"], tier["size"], true)
instance_type = region.add_or_update_rds_instance_type(api_name, name)
instance_type.update_pricing(db_type, type_of_rds_instance, tier, is_multi_az, is_byol)
rescue UnknownTypeError
$stderr.puts "[fetch_reserved_rds_instance_pricing] WARNING: encountered #{$!.message}"
end
end
end
end
rescue => ex
$sterr.puts "Failed to fetch: #{url}"
raise
end
end
end