buildPriceEntries converts an api.PricingItem into a sorted slice of PriceEntry. OnDemand is always first; the rest are sorted by model name, then rate.
(item api.PricingItem)
| 398 | // buildPriceEntries converts an api.PricingItem into a sorted slice of PriceEntry. |
| 399 | // OnDemand is always first; the rest are sorted by model name, then rate. |
| 400 | func buildPriceEntries(item api.PricingItem) ([]resources.PriceEntry, decimal.Decimal) { |
| 401 | var entries []resources.PriceEntry |
| 402 | var onDemandRate decimal.Decimal |
| 403 | |
| 404 | for _, p := range item.Prices { |
| 405 | model := "" |
| 406 | if p.PricingModel != nil { |
| 407 | model = *p.PricingModel |
| 408 | } |
| 409 | purchaseOption := "" |
| 410 | if p.PurchaseOption != nil { |
| 411 | purchaseOption = *p.PurchaseOption |
| 412 | } |
| 413 | term := "" |
| 414 | if p.Year != nil { |
| 415 | term = p.Year.String() |
| 416 | } |
| 417 | upfront := "" |
| 418 | if p.UpfrontFee != nil { |
| 419 | upfront = p.UpfrontFee.String() |
| 420 | } |
| 421 | unit := "" |
| 422 | if p.Unit != nil { |
| 423 | unit = *p.Unit |
| 424 | } |
| 425 | |
| 426 | rate := decimal.Zero |
| 427 | if len(p.Rates) > 0 && p.Rates[0].Price != nil { |
| 428 | if d, err := decimal.NewFromString(p.Rates[0].Price.String()); err == nil { |
| 429 | rate = d |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | isCurrent := strings.EqualFold(model, "OnDemand") |
| 434 | if isCurrent && rate.GreaterThan(onDemandRate) { |
| 435 | onDemandRate = rate |
| 436 | } |
| 437 | |
| 438 | isUsage := !isHourlyUnit(unit) |
| 439 | |
| 440 | // Build rate tiers for volume-based pricing (more than one rate). |
| 441 | var tiers []resources.RateTier |
| 442 | if len(p.Rates) > 1 { |
| 443 | for _, r := range p.Rates { |
| 444 | tier := resources.RateTier{} |
| 445 | if r.Price != nil { |
| 446 | tier.Price = r.Price.String() |
| 447 | } |
| 448 | if r.StartRange != nil { |
| 449 | tier.StartRange = r.StartRange.String() |
| 450 | } |
| 451 | if r.EndRange != nil { |
| 452 | tier.EndRange = r.EndRange.String() |
| 453 | } |
| 454 | tiers = append(tiers, tier) |
| 455 | } |
| 456 | } |
| 457 |
no test coverage detected