generator.go 3.5 KB

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