statuses.go 3.8 KB

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