generator.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package public
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/core/pkg/log"
  5. "github.com/opencost/opencost/core/pkg/model/shared"
  6. "github.com/opencost/opencost/core/pkg/pricing"
  7. "github.com/opencost/opencost/core/pkg/unit"
  8. "github.com/opencost/opencost/modules/pricing/public/aws"
  9. "github.com/opencost/opencost/modules/pricing/public/azure"
  10. )
  11. // GenerateAWSPricing fetches AWS pricing data in the specified currency
  12. func GenerateAWSPricing(currency unit.Currency) (*pricing.PricingSet, error) {
  13. log.Infof("Generating AWS pricing for currency: %s", currency)
  14. source := aws.NewAWSPricingSource(aws.AWSPricingSourceConfig{
  15. CurrencyCode: string(currency),
  16. })
  17. pricingSet, err := source.GetPricing()
  18. if err != nil {
  19. return nil, fmt.Errorf("failed to get AWS pricing: %w", err)
  20. }
  21. // Sort to ensure deterministic output for checksums
  22. pricingSet.Sort()
  23. log.Infof("Generated %d AWS node pricing entries", len(pricingSet.NodePricing))
  24. return pricingSet, nil
  25. }
  26. // GenerateAzurePricing fetches Azure pricing data in the specified currency
  27. func GenerateAzurePricing(currency unit.Currency) (*pricing.PricingSet, error) {
  28. log.Infof("Generating Azure pricing for currency: %s", currency)
  29. source := azure.NewAzurePricingSource(azure.AzurePricingSourceConfig{
  30. CurrencyCode: string(currency),
  31. })
  32. pricingSet, err := source.GetPricing()
  33. if err != nil {
  34. return nil, fmt.Errorf("failed to get Azure pricing: %w", err)
  35. }
  36. // Sort to ensure deterministic output for checksums
  37. pricingSet.Sort()
  38. log.Infof("Generated %d Azure node pricing entries", len(pricingSet.NodePricing))
  39. return pricingSet, nil
  40. }
  41. // GenerateAllProvidersPricing fetches pricing data for all supported providers
  42. // and combines them into a single PricingSet
  43. func GenerateAllProvidersPricing(currency unit.Currency) (*pricing.PricingSet, error) {
  44. log.Infof("Generating pricing for all providers in currency: %s", currency)
  45. // Create a combined pricing set
  46. combinedSet := &pricing.PricingSet{
  47. NodePricing: []*pricing.NodePricing{},
  48. PersistentVolumePricing: []*pricing.PersistentVolumePricing{},
  49. }
  50. // Fetch AWS pricing
  51. awsSet, err := GenerateAWSPricing(currency)
  52. if err != nil {
  53. log.Warnf("Failed to get AWS pricing: %v", err)
  54. } else {
  55. combinedSet.NodePricing = append(combinedSet.NodePricing, awsSet.NodePricing...)
  56. combinedSet.PersistentVolumePricing = append(combinedSet.PersistentVolumePricing, awsSet.PersistentVolumePricing...)
  57. log.Infof("Added %d AWS node pricing entries", len(awsSet.NodePricing))
  58. }
  59. // Fetch Azure pricing
  60. azureSet, err := GenerateAzurePricing(currency)
  61. if err != nil {
  62. log.Warnf("Failed to get Azure pricing: %v", err)
  63. } else {
  64. combinedSet.NodePricing = append(combinedSet.NodePricing, azureSet.NodePricing...)
  65. combinedSet.PersistentVolumePricing = append(combinedSet.PersistentVolumePricing, azureSet.PersistentVolumePricing...)
  66. log.Infof("Added %d Azure node pricing entries", len(azureSet.NodePricing))
  67. }
  68. // Sort the combined set to ensure deterministic output
  69. combinedSet.Sort()
  70. log.Infof("Generated combined pricing set with %d total node entries and %d volume entries",
  71. len(combinedSet.NodePricing), len(combinedSet.PersistentVolumePricing))
  72. return combinedSet, nil
  73. }
  74. // GeneratePricingForProvider fetches pricing data for a specific provider
  75. // in the specified currency
  76. func GeneratePricingForProvider(provider shared.Provider, currency unit.Currency) (*pricing.PricingSet, error) {
  77. switch provider {
  78. case shared.ProviderEmpty:
  79. return GenerateAllProvidersPricing(currency)
  80. case shared.ProviderAWS:
  81. return GenerateAWSPricing(currency)
  82. case shared.ProviderAzure:
  83. return GenerateAzurePricing(currency)
  84. case shared.ProviderGCP:
  85. return nil, fmt.Errorf("not implemented")
  86. // return GenerateGCPPricing(currency)
  87. default:
  88. return nil, fmt.Errorf("unsupported provider: %s", provider)
  89. }
  90. }