integration.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package cloudcost
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. "github.com/opencost/opencost/pkg/cloud/alibaba"
  7. "github.com/opencost/opencost/pkg/cloud/aws"
  8. "github.com/opencost/opencost/pkg/cloud/azure"
  9. "github.com/opencost/opencost/pkg/cloud/gcp"
  10. "github.com/opencost/opencost/pkg/cloud/oracle"
  11. "github.com/opencost/opencost/pkg/cloud/stackit"
  12. )
  13. // CloudCostIntegration is an interface for retrieving daily granularity CloudCost data for a given range
  14. type CloudCostIntegration interface {
  15. GetCloudCost(time.Time, time.Time) (*opencost.CloudCostSetRange, error)
  16. GetStatus() cloud.ConnectionStatus
  17. RefreshStatus() cloud.ConnectionStatus
  18. }
  19. // GetIntegrationFromConfig coverts any valid KeyedConfig into the appropriate BillingIntegration if possible
  20. func GetIntegrationFromConfig(kc cloud.KeyedConfig) CloudCostIntegration {
  21. switch keyedConfig := kc.(type) {
  22. // AthenaIntegration
  23. case *aws.AthenaConfiguration:
  24. return &aws.AthenaIntegration{
  25. AthenaQuerier: aws.AthenaQuerier{
  26. AthenaConfiguration: *keyedConfig,
  27. },
  28. }
  29. case *aws.AthenaQuerier:
  30. return &aws.AthenaIntegration{
  31. AthenaQuerier: *keyedConfig,
  32. }
  33. case *aws.AthenaIntegration:
  34. return keyedConfig
  35. // BigQueryIntegration
  36. case *gcp.BigQueryConfiguration:
  37. return &gcp.BigQueryIntegration{
  38. BigQueryQuerier: gcp.BigQueryQuerier{
  39. BigQueryConfiguration: *keyedConfig,
  40. },
  41. }
  42. case *gcp.BigQueryQuerier:
  43. return &gcp.BigQueryIntegration{
  44. BigQueryQuerier: *keyedConfig,
  45. }
  46. case *gcp.BigQueryIntegration:
  47. return keyedConfig
  48. // AzureStorageIntegration
  49. case *azure.StorageConfiguration:
  50. return &azure.AzureStorageIntegration{
  51. AzureStorageBillingParser: azure.AzureStorageBillingParser{
  52. StorageConnection: azure.StorageConnection{
  53. StorageConfiguration: *keyedConfig},
  54. },
  55. }
  56. case *azure.StorageConnection:
  57. return &azure.AzureStorageIntegration{
  58. AzureStorageBillingParser: azure.AzureStorageBillingParser{
  59. StorageConnection: azure.StorageConnection{
  60. StorageConfiguration: keyedConfig.StorageConfiguration},
  61. },
  62. }
  63. case *azure.AzureStorageBillingParser:
  64. return &azure.AzureStorageIntegration{
  65. AzureStorageBillingParser: azure.AzureStorageBillingParser{
  66. StorageConnection: azure.StorageConnection{
  67. StorageConfiguration: keyedConfig.StorageConfiguration},
  68. },
  69. }
  70. case *azure.AzureStorageIntegration:
  71. return &azure.AzureStorageIntegration{
  72. AzureStorageBillingParser: azure.AzureStorageBillingParser{
  73. StorageConnection: azure.StorageConnection{
  74. StorageConfiguration: keyedConfig.StorageConfiguration},
  75. },
  76. }
  77. // S3SelectIntegration
  78. case *aws.S3Configuration:
  79. return &aws.S3SelectIntegration{
  80. S3SelectQuerier: aws.S3SelectQuerier{
  81. S3Connection: aws.S3Connection{
  82. S3Configuration: *keyedConfig,
  83. },
  84. },
  85. }
  86. case *aws.S3Connection:
  87. return &aws.S3SelectIntegration{
  88. S3SelectQuerier: aws.S3SelectQuerier{
  89. S3Connection: *keyedConfig,
  90. },
  91. }
  92. case *aws.S3SelectQuerier:
  93. return &aws.S3SelectIntegration{
  94. S3SelectQuerier: *keyedConfig,
  95. }
  96. case *aws.S3SelectIntegration:
  97. return keyedConfig
  98. // Alibaba BOA Integration
  99. case *alibaba.BOAConfiguration:
  100. return nil
  101. case *oracle.UsageApiConfiguration:
  102. return &oracle.UsageApiIntegration{
  103. UsageApiConfiguration: *keyedConfig,
  104. }
  105. case *stackit.CostConfiguration:
  106. return &stackit.CostIntegration{
  107. CostConfiguration: *keyedConfig,
  108. }
  109. default:
  110. return nil
  111. }
  112. }