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
|
# File 'lib/webhookdb/replicator/aws_pricing_v1.rb', line 244
def each
@arns.each do |price_list_arn|
url_req = @replicator.with_pricing_client do |client|
client.build_request(
:get_price_list_file_url,
{file_format: "json", price_list_arn:},
)
end
url_resp = url_req.send_request({})
tmp = Tempfile.new("awspricing", binmode: true)
Webhookdb::Http.get(url_resp.url, logger: @replicator.logger, stream_body: true,
timeout: Webhookdb::AWS.http_timeout,) do |fragment|
tmp.write(fragment)
end
tmp.flush
tmp.seek(0)
flines = File.foreach(tmp.path)
meta_str = +""
until (mline = flines.next) =~ /^ {2}"products"/
meta_str << mline
end
meta_str << '"":""}'
meta = Oj.load(meta_str)
meta_str.clear
publication_date = Time.parse(meta.fetch("publicationDate"))
service_code = meta.fetch("offerCode")
version = meta.fetch("version")
products_str = +"{"
until (pline = flines.next) =~ /^ {2}}/
products_str << pline
end
products_str << "}"
products = Oj.load(products_str)
products_str.clear
_terms = flines.next
term_type = nil
until (line = flines.next) == "}" start_of_term_type = line =~ /^ {4}"/
if start_of_term_type
term_type = line[/^ {4}"([A-Za-z0-9]+)"/, 1]
next
end
start_of_product_and_term_map = line =~ /^ {6}"/
next unless start_of_product_and_term_map
product_sku = line[/^ {6}"([A-Za-z0-9]+)"/, 1]
term_map_str = +"{"
until (tline = flines.next) =~ /^ {6}}/
term_map_str << tline
end
term_map_str << "}"
term_map = Oj.load(term_map_str)
term_map_str.clear
product = products.fetch(product_sku)
product_family = product.fetch("productFamily", nil)
product_attributes = product.fetch("attributes", {})
product_group = product_attributes.fetch("group", nil)
product_location = product_attributes.fetch("location", nil)
product_region = Webhookdb::AWS::LOCATIONS_TO_REGIONS.fetch(product_location, product_location)
product_operation = product_attributes.fetch("operation", nil)
product_usagetype = product_attributes.fetch("usagetype", nil)
term_map.each do |term_code, term|
term["priceDimensions"].each do |rate_code, rate|
price_per_unit_currency, price_per_unit_amount = rate.fetch("pricePerUnit").first
rate = {
product_sku:,
product_family:,
product_attributes:,
product_group:,
product_location:,
product_region:,
product_operation:,
product_usagetype:,
publication_date:,
service_code:,
version:,
term_type:,
term_code:,
offer_term_code: term.fetch("offerTermCode"),
effective_date: Time.parse(term.fetch("effectiveDate")),
term_attributes: term.fetch("termAttributes", {}),
rate_code:,
applies_to: (applies_to = rate.fetch("appliesTo")) ? Sequel.pg_array(applies_to) : nil,
begin_range: self.parse_range(rate.fetch("beginRange", nil)),
description: rate.fetch("description"),
end_range: self.parse_range(rate.fetch("endRange", nil)),
unit: rate.fetch("unit", nil),
price_per_unit_raw: rate.fetch("pricePerUnit"),
price_per_unit_amount:,
price_per_unit_currency:,
}.stringify_keys
yield rate
end
end
end
tmp.unlink
end
end
|