statuses.go 4.3 KB

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