storageconfiguration.go 4.3 KB

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