statuses.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package config
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/opencost/opencost/core/pkg/util/json"
  6. "github.com/opencost/opencost/pkg/cloud"
  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/ibm"
  11. "github.com/opencost/opencost/pkg/cloud/oracle"
  12. "github.com/opencost/opencost/pkg/cloud/stackit"
  13. )
  14. const (
  15. S3ConfigType = "s3"
  16. AthenaConfigType = "athena"
  17. BigQueryConfigType = "bigquery"
  18. AzureStorageConfigType = "azurestorage"
  19. UsageApiConfigType = "usageapi"
  20. STACKITCostConfigType = "stackitcost"
  21. IBMUsageConfigType = "ibmusage"
  22. )
  23. func ConfigTypeFromConfig(config cloud.KeyedConfig) (string, error) {
  24. switch config.(type) {
  25. case *aws.S3Configuration:
  26. return S3ConfigType, nil
  27. case *aws.AthenaConfiguration:
  28. return AthenaConfigType, nil
  29. case *gcp.BigQueryConfiguration:
  30. return BigQueryConfigType, nil
  31. case *azure.StorageConfiguration:
  32. return AzureStorageConfigType, nil
  33. case *oracle.UsageApiConfiguration:
  34. return UsageApiConfigType, nil
  35. case *stackit.CostConfiguration:
  36. return STACKITCostConfigType, nil
  37. case *ibm.UsageConfiguration:
  38. return IBMUsageConfigType, nil
  39. }
  40. return "", fmt.Errorf("failed to determine config type for config with key: %s, type %T", config.Key(), config)
  41. }
  42. type Statuses map[ConfigSource]map[string]*Status
  43. func (s Statuses) Get(key string, source ConfigSource) (*Status, bool) {
  44. if _, ok := s[source]; !ok {
  45. return nil, false
  46. }
  47. status, ok := s[source][key]
  48. return status, ok
  49. }
  50. func (s Statuses) Insert(status *Status) {
  51. if _, ok := s[status.Source]; !ok {
  52. s[status.Source] = map[string]*Status{}
  53. }
  54. s[status.Source][status.Key] = status
  55. }
  56. func (s Statuses) List() []*Status {
  57. var list []*Status
  58. for _, statusesByKey := range s {
  59. for _, status := range statusesByKey {
  60. list = append(list, status)
  61. }
  62. }
  63. return list
  64. }
  65. type Status struct {
  66. Source ConfigSource `json:"source"`
  67. Key string `json:"key"`
  68. Active bool `json:"active"`
  69. Valid bool `json:"valid"`
  70. ConfigType string `json:"configType"`
  71. Config cloud.KeyedConfig `json:"config"`
  72. }
  73. func (s *Status) UnmarshalJSON(b []byte) error {
  74. var f interface{}
  75. err := json.Unmarshal(b, &f)
  76. if err != nil {
  77. return err
  78. }
  79. fmap := f.(map[string]interface{})
  80. sourceFloat, err := cloud.GetInterfaceValue[float64](fmap, "source")
  81. if err != nil {
  82. return fmt.Errorf("Status: UnmarshalJSON: %s", err.Error())
  83. }
  84. source := ConfigSource(int(sourceFloat))
  85. key, err := cloud.GetInterfaceValue[string](fmap, "key")
  86. if err != nil {
  87. return fmt.Errorf("Status: UnmarshalJSON: %s", err.Error())
  88. }
  89. active, err := cloud.GetInterfaceValue[bool](fmap, "active")
  90. if err != nil {
  91. return fmt.Errorf("Status: UnmarshalJSON: %s", err.Error())
  92. }
  93. valid, err := cloud.GetInterfaceValue[bool](fmap, "valid")
  94. if err != nil {
  95. return fmt.Errorf("Status: UnmarshalJSON: %s", err.Error())
  96. }
  97. configType, err := cloud.GetInterfaceValue[string](fmap, "configType")
  98. if err != nil {
  99. return fmt.Errorf("Status: UnmarshalJSON: %s", err.Error())
  100. }
  101. // Pick correct implementation to unmarshal into
  102. var config cloud.KeyedConfig
  103. switch strings.ToLower(configType) {
  104. case S3ConfigType:
  105. config = &aws.S3Configuration{}
  106. case AthenaConfigType:
  107. config = &aws.AthenaConfiguration{}
  108. case BigQueryConfigType:
  109. config = &gcp.BigQueryConfiguration{}
  110. case AzureStorageConfigType:
  111. config = &azure.StorageConfiguration{}
  112. case UsageApiConfigType:
  113. config = &oracle.UsageApiConfiguration{}
  114. case STACKITCostConfigType:
  115. config = &stackit.CostConfiguration{}
  116. case IBMUsageConfigType:
  117. config = &ibm.UsageConfiguration{}
  118. default:
  119. return fmt.Errorf("Status: UnmarshalJSON: config type '%s' is not recognized", configType)
  120. }
  121. configAny, err := cloud.GetInterfaceValue[any](fmap, "config")
  122. if err != nil {
  123. return fmt.Errorf("Status: UnmarshalJSON: %s", err.Error())
  124. }
  125. // convert the interface back to a []Byte so that it can be unmarshalled into the correct type
  126. fBin, err := json.Marshal(configAny)
  127. if err != nil {
  128. return fmt.Errorf("Status: UnmarshalJSON: could not marshal value %v: %w", f, err)
  129. }
  130. err = json.Unmarshal(fBin, config)
  131. if err != nil {
  132. return fmt.Errorf("Status: UnmarshalJSON: failed to unmarshal into Configuration type %T from value %v: %w", config, f, err)
  133. }
  134. // Set Values
  135. s.Source = source
  136. s.Key = key
  137. s.Active = active
  138. s.Valid = valid
  139. s.ConfigType = configType
  140. s.Config = config
  141. return nil
  142. }