storageconfiguration.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package azure
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/pkg/cloud/config"
  5. "github.com/opencost/opencost/pkg/util/json"
  6. )
  7. type StorageConfiguration struct {
  8. SubscriptionID string `json:"subscriptionID"`
  9. Account string `json:"account"`
  10. Container string `json:"container"`
  11. Path string `json:"path"`
  12. Cloud string `json:"cloud"`
  13. Authorizer Authorizer `json:"authorizer"`
  14. }
  15. // Check ensures that all required fields are set, and throws an error if they are not
  16. func (sc *StorageConfiguration) Validate() error {
  17. if sc.Authorizer == nil {
  18. return fmt.Errorf("StorageConfiguration: missing authorizer")
  19. }
  20. err := sc.Authorizer.Validate()
  21. if err != nil {
  22. return err
  23. }
  24. if sc.SubscriptionID == "" {
  25. return fmt.Errorf("StorageConfiguration: missing Subcription ID")
  26. }
  27. if sc.Account == "" {
  28. return fmt.Errorf("StorageConfiguration: missing Account")
  29. }
  30. if sc.Container == "" {
  31. return fmt.Errorf("StorageConfiguration: missing Container")
  32. }
  33. return nil
  34. }
  35. func (sc *StorageConfiguration) Equals(config config.Config) bool {
  36. if config == nil {
  37. return false
  38. }
  39. thatConfig, ok := config.(*StorageConfiguration)
  40. if !ok {
  41. return false
  42. }
  43. if sc.Authorizer != nil {
  44. if !sc.Authorizer.Equals(thatConfig.Authorizer) {
  45. return false
  46. }
  47. } else {
  48. if thatConfig.Authorizer != nil {
  49. return false
  50. }
  51. }
  52. if sc.SubscriptionID != thatConfig.SubscriptionID {
  53. return false
  54. }
  55. if sc.Account != thatConfig.Account {
  56. return false
  57. }
  58. if sc.Container != thatConfig.Container {
  59. return false
  60. }
  61. if sc.Path != thatConfig.Path {
  62. return false
  63. }
  64. if sc.Cloud != thatConfig.Cloud {
  65. return false
  66. }
  67. return true
  68. }
  69. func (sc *StorageConfiguration) Sanitize() config.Config {
  70. return &StorageConfiguration{
  71. SubscriptionID: sc.SubscriptionID,
  72. Account: sc.Account,
  73. Container: sc.Container,
  74. Path: sc.Path,
  75. Cloud: sc.Cloud,
  76. Authorizer: sc.Authorizer.Sanitize().(Authorizer),
  77. }
  78. }
  79. func (sc *StorageConfiguration) Key() string {
  80. key := fmt.Sprintf("%s/%s", sc.SubscriptionID, sc.Container)
  81. // append container path to key if it exists
  82. if sc.Path != "" {
  83. key = fmt.Sprintf("%s/%s", key, sc.Path)
  84. }
  85. return key
  86. }
  87. func (sc *StorageConfiguration) UnmarshalJSON(b []byte) error {
  88. var f interface{}
  89. err := json.Unmarshal(b, &f)
  90. if err != nil {
  91. return err
  92. }
  93. fmap := f.(map[string]interface{})
  94. subscriptionID, err := config.GetInterfaceValue[string](fmap, "subscriptionID")
  95. if err != nil {
  96. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  97. }
  98. sc.SubscriptionID = subscriptionID
  99. account, err := config.GetInterfaceValue[string](fmap, "account")
  100. if err != nil {
  101. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  102. }
  103. sc.Account = account
  104. container, err := config.GetInterfaceValue[string](fmap, "container")
  105. if err != nil {
  106. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  107. }
  108. sc.Container = container
  109. path, err := config.GetInterfaceValue[string](fmap, "path")
  110. if err != nil {
  111. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  112. }
  113. sc.Path = path
  114. cloud, err := config.GetInterfaceValue[string](fmap, "cloud")
  115. if err != nil {
  116. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  117. }
  118. sc.Cloud = cloud
  119. authAny, ok := fmap["authorizer"]
  120. if !ok {
  121. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
  122. }
  123. authorizer, err := config.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  124. if err != nil {
  125. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  126. }
  127. sc.Authorizer = authorizer
  128. return nil
  129. }
  130. func ConvertAzureStorageConfigToConfig(asc AzureStorageConfig) config.KeyedConfig {
  131. if asc.IsEmpty() {
  132. return nil
  133. }
  134. var authorizer Authorizer
  135. authorizer = &AccessKey{
  136. AccessKey: asc.AccessKey,
  137. Account: asc.AccountName,
  138. }
  139. return &StorageConfiguration{
  140. SubscriptionID: asc.SubscriptionId,
  141. Account: asc.AccountName,
  142. Container: asc.ContainerName,
  143. Path: asc.ContainerPath,
  144. Cloud: asc.AzureCloud,
  145. Authorizer: authorizer,
  146. }
  147. }