statuses.go 4.0 KB

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