configurations.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/opencost/opencost/core/pkg/log"
  6. "github.com/opencost/opencost/pkg/cloud"
  7. "github.com/opencost/opencost/pkg/cloud/alibaba"
  8. "github.com/opencost/opencost/pkg/cloud/aws"
  9. "github.com/opencost/opencost/pkg/cloud/azure"
  10. "github.com/opencost/opencost/pkg/cloud/gcp"
  11. "github.com/opencost/opencost/pkg/cloud/ibm"
  12. "github.com/opencost/opencost/pkg/cloud/oracle"
  13. "github.com/opencost/opencost/pkg/cloud/stackit"
  14. )
  15. // MultiCloudConfig struct is used to unmarshal cloud configs for each provider out of cloud-integration file
  16. // Deprecated: v1.104 use Configurations
  17. type MultiCloudConfig struct {
  18. AzureConfigs []azure.AzureStorageConfig `json:"azure"`
  19. GCPConfigs []gcp.BigQueryConfig `json:"gcp"`
  20. AWSConfigs []aws.AwsAthenaInfo `json:"aws"`
  21. AlibabaConfigs []alibaba.AlibabaInfo `json:"alibaba"`
  22. }
  23. func (mcc MultiCloudConfig) loadConfigurations(configs *Configurations) {
  24. // Load AWS configs
  25. for _, awsConfig := range mcc.AWSConfigs {
  26. kc := aws.ConvertAwsAthenaInfoToConfig(awsConfig)
  27. err := configs.Insert(kc)
  28. if err != nil {
  29. log.Errorf("MultiCloudConfig: error converting AWS config %s", err.Error())
  30. }
  31. }
  32. // Load GCP configs
  33. for _, gcpConfig := range mcc.GCPConfigs {
  34. kc := gcp.ConvertBigQueryConfigToConfig(gcpConfig)
  35. err := configs.Insert(kc)
  36. if err != nil {
  37. log.Errorf("MultiCloudConfig: error converting GCP config %s", err.Error())
  38. }
  39. }
  40. // Load Azure configs
  41. for _, azureConfig := range mcc.AzureConfigs {
  42. kc := azure.ConvertAzureStorageConfigToConfig(azureConfig)
  43. err := configs.Insert(kc)
  44. if err != nil {
  45. log.Errorf("MultiCloudConfig: error converting Azure config %s", err.Error())
  46. }
  47. }
  48. // Load Alibaba Cloud Configs
  49. for _, aliCloudConfig := range mcc.AlibabaConfigs {
  50. kc := alibaba.ConvertAlibabaInfoToConfig(aliCloudConfig)
  51. err := configs.Insert(kc)
  52. if err != nil {
  53. log.Errorf("MultiCloudConfig: error converting Alibaba config %s", err.Error())
  54. }
  55. }
  56. }
  57. // Configurations is a general use container for all configuration types
  58. type Configurations struct {
  59. AWS *AWSConfigs `json:"aws,omitempty"`
  60. GCP *GCPConfigs `json:"gcp,omitempty"`
  61. Azure *AzureConfigs `json:"azure,omitempty"`
  62. Alibaba *AlibabaConfigs `json:"alibaba,omitempty"`
  63. OCI *OCIConfigs `json:"oci,omitempty"`
  64. STACKIT *STACKITConfigs `json:"stackit,omitempty"`
  65. IBM *IBMConfigs `json:"ibm,omitempty"`
  66. }
  67. // UnmarshalJSON custom json unmarshalling to maintain support for MultiCloudConfig format
  68. func (c *Configurations) UnmarshalJSON(bytes []byte) error {
  69. // This has been tested for backwards compatability, and it works in both config formats.
  70. // It also coincidentally works if you mix-and-match both the old format and the new
  71. // format.
  72. // Create defined type to gain access to default Unmarshalling, then convert to pointer
  73. type confUnmarshaller Configurations
  74. err := json.Unmarshal(bytes, (*confUnmarshaller)(c))
  75. // If unmarshal is successful, return
  76. if err == nil {
  77. return nil
  78. }
  79. // Attempt to unmarshal into old config object
  80. multiConfig := &MultiCloudConfig{}
  81. err = json.Unmarshal(bytes, multiConfig)
  82. if err != nil {
  83. return err
  84. }
  85. multiConfig.loadConfigurations(c)
  86. return nil
  87. }
  88. func (c *Configurations) Equals(that *Configurations) bool {
  89. if c == nil && that == nil {
  90. return true
  91. }
  92. if c == nil || that == nil {
  93. return false
  94. }
  95. if !c.AWS.Equals(that.AWS) {
  96. return false
  97. }
  98. if !c.GCP.Equals(that.GCP) {
  99. return false
  100. }
  101. if !c.Azure.Equals(that.Azure) {
  102. return false
  103. }
  104. if !c.Alibaba.Equals(that.Alibaba) {
  105. return false
  106. }
  107. if !c.OCI.Equals(that.OCI) {
  108. return false
  109. }
  110. if !c.STACKIT.Equals(that.STACKIT) {
  111. return false
  112. }
  113. if !c.IBM.Equals(that.IBM) {
  114. return false
  115. }
  116. return true
  117. }
  118. func (c *Configurations) Insert(keyedConfig cloud.Config) error {
  119. switch keyedConfig.(type) {
  120. case *aws.AthenaConfiguration:
  121. if c.AWS == nil {
  122. c.AWS = &AWSConfigs{}
  123. }
  124. c.AWS.Athena = append(c.AWS.Athena, keyedConfig.(*aws.AthenaConfiguration))
  125. case *aws.S3Configuration:
  126. if c.AWS == nil {
  127. c.AWS = &AWSConfigs{}
  128. }
  129. c.AWS.S3 = append(c.AWS.S3, keyedConfig.(*aws.S3Configuration))
  130. case *gcp.BigQueryConfiguration:
  131. if c.GCP == nil {
  132. c.GCP = &GCPConfigs{}
  133. }
  134. c.GCP.BigQuery = append(c.GCP.BigQuery, keyedConfig.(*gcp.BigQueryConfiguration))
  135. case *azure.StorageConfiguration:
  136. if c.Azure == nil {
  137. c.Azure = &AzureConfigs{}
  138. }
  139. c.Azure.Storage = append(c.Azure.Storage, keyedConfig.(*azure.StorageConfiguration))
  140. case *alibaba.BOAConfiguration:
  141. if c.Alibaba == nil {
  142. c.Alibaba = &AlibabaConfigs{}
  143. }
  144. c.Alibaba.BOA = append(c.Alibaba.BOA, keyedConfig.(*alibaba.BOAConfiguration))
  145. case *oracle.UsageApiConfiguration:
  146. if c.OCI == nil {
  147. c.OCI = &OCIConfigs{}
  148. }
  149. c.OCI.UsageAPI = append(c.OCI.UsageAPI, keyedConfig.(*oracle.UsageApiConfiguration))
  150. case *stackit.CostConfiguration:
  151. if c.STACKIT == nil {
  152. c.STACKIT = &STACKITConfigs{}
  153. }
  154. c.STACKIT.CostAPI = append(c.STACKIT.CostAPI, keyedConfig.(*stackit.CostConfiguration))
  155. case *ibm.UsageConfiguration:
  156. if c.IBM == nil {
  157. c.IBM = &IBMConfigs{}
  158. }
  159. c.IBM.UsageAPI = append(c.IBM.UsageAPI, keyedConfig.(*ibm.UsageConfiguration))
  160. default:
  161. return fmt.Errorf("Configurations: Insert: failed to insert config of type: %T", keyedConfig)
  162. }
  163. return nil
  164. }
  165. func (c *Configurations) ToSlice() []cloud.KeyedConfig {
  166. var keyedConfigs []cloud.KeyedConfig
  167. if c.AWS != nil {
  168. for _, athenaConfig := range c.AWS.Athena {
  169. keyedConfigs = append(keyedConfigs, athenaConfig)
  170. }
  171. for _, s3Config := range c.AWS.S3 {
  172. keyedConfigs = append(keyedConfigs, s3Config)
  173. }
  174. }
  175. if c.GCP != nil {
  176. for _, bigQueryConfig := range c.GCP.BigQuery {
  177. keyedConfigs = append(keyedConfigs, bigQueryConfig)
  178. }
  179. }
  180. if c.Azure != nil {
  181. for _, azureStorageConfig := range c.Azure.Storage {
  182. keyedConfigs = append(keyedConfigs, azureStorageConfig)
  183. }
  184. }
  185. if c.Alibaba != nil {
  186. for _, boaConfig := range c.Alibaba.BOA {
  187. keyedConfigs = append(keyedConfigs, boaConfig)
  188. }
  189. }
  190. if c.OCI != nil {
  191. for _, usageConfig := range c.OCI.UsageAPI {
  192. keyedConfigs = append(keyedConfigs, usageConfig)
  193. }
  194. }
  195. if c.STACKIT != nil {
  196. for _, costConfig := range c.STACKIT.CostAPI {
  197. keyedConfigs = append(keyedConfigs, costConfig)
  198. }
  199. }
  200. if c.IBM != nil {
  201. for _, usageConfig := range c.IBM.UsageAPI {
  202. keyedConfigs = append(keyedConfigs, usageConfig)
  203. }
  204. }
  205. return keyedConfigs
  206. }
  207. type AWSConfigs struct {
  208. Athena []*aws.AthenaConfiguration `json:"athena,omitempty"`
  209. S3 []*aws.S3Configuration `json:"s3,omitempty"`
  210. }
  211. func (ac *AWSConfigs) Equals(that *AWSConfigs) bool {
  212. if ac == nil && that == nil {
  213. return true
  214. }
  215. if ac == nil || that == nil {
  216. return false
  217. }
  218. // Check Athena
  219. if len(ac.Athena) != len(that.Athena) {
  220. return false
  221. }
  222. for i, thisAthena := range ac.Athena {
  223. thatAthena := that.Athena[i]
  224. if !thisAthena.Equals(thatAthena) {
  225. return false
  226. }
  227. }
  228. // Check S3
  229. if len(ac.S3) != len(that.S3) {
  230. return false
  231. }
  232. for i, thisS3 := range ac.S3 {
  233. thatS3 := that.S3[i]
  234. if !thisS3.Equals(thatS3) {
  235. return false
  236. }
  237. }
  238. return true
  239. }
  240. type GCPConfigs struct {
  241. BigQuery []*gcp.BigQueryConfiguration `json:"bigQuery,omitempty"`
  242. }
  243. func (gc *GCPConfigs) Equals(that *GCPConfigs) bool {
  244. if gc == nil && that == nil {
  245. return true
  246. }
  247. if gc == nil || that == nil {
  248. return false
  249. }
  250. // Check BigQuery
  251. if len(gc.BigQuery) != len(that.BigQuery) {
  252. return false
  253. }
  254. for i, thisBigQuery := range gc.BigQuery {
  255. thatBigQuery := that.BigQuery[i]
  256. if !thisBigQuery.Equals(thatBigQuery) {
  257. return false
  258. }
  259. }
  260. return true
  261. }
  262. type AzureConfigs struct {
  263. Storage []*azure.StorageConfiguration `json:"storage,omitempty"`
  264. }
  265. func (ac *AzureConfigs) Equals(that *AzureConfigs) bool {
  266. if ac == nil && that == nil {
  267. return true
  268. }
  269. if ac == nil || that == nil {
  270. return false
  271. }
  272. // Check Storage
  273. if len(ac.Storage) != len(that.Storage) {
  274. return false
  275. }
  276. for i, thisStorage := range ac.Storage {
  277. thatStorage := that.Storage[i]
  278. if !thisStorage.Equals(thatStorage) {
  279. return false
  280. }
  281. }
  282. return true
  283. }
  284. type AlibabaConfigs struct {
  285. BOA []*alibaba.BOAConfiguration `json:"boa,omitempty"`
  286. }
  287. func (ac *AlibabaConfigs) Equals(that *AlibabaConfigs) bool {
  288. if ac == nil && that == nil {
  289. return true
  290. }
  291. if ac == nil || that == nil {
  292. return false
  293. }
  294. // Check BOA
  295. if len(ac.BOA) != len(that.BOA) {
  296. return false
  297. }
  298. for i, thisBOA := range ac.BOA {
  299. thatBOA := that.BOA[i]
  300. if !thisBOA.Equals(thatBOA) {
  301. return false
  302. }
  303. }
  304. return true
  305. }
  306. type OCIConfigs struct {
  307. UsageAPI []*oracle.UsageApiConfiguration `json:"usageApi,omitempty"`
  308. }
  309. func (oc *OCIConfigs) Equals(that *OCIConfigs) bool {
  310. if oc == nil && that == nil {
  311. return true
  312. }
  313. if oc == nil || that == nil {
  314. return false
  315. }
  316. // Check Usage API
  317. if len(oc.UsageAPI) != len(that.UsageAPI) {
  318. return false
  319. }
  320. for i, thisUsageAPI := range oc.UsageAPI {
  321. thatUsageAPI := that.UsageAPI[i]
  322. if !thisUsageAPI.Equals(thatUsageAPI) {
  323. return false
  324. }
  325. }
  326. return true
  327. }
  328. type STACKITConfigs struct {
  329. CostAPI []*stackit.CostConfiguration `json:"costApi,omitempty"`
  330. }
  331. func (sc *STACKITConfigs) Equals(that *STACKITConfigs) bool {
  332. if sc == nil && that == nil {
  333. return true
  334. }
  335. if sc == nil || that == nil {
  336. return false
  337. }
  338. if len(sc.CostAPI) != len(that.CostAPI) {
  339. return false
  340. }
  341. for i, thisCost := range sc.CostAPI {
  342. thatCost := that.CostAPI[i]
  343. if !thisCost.Equals(thatCost) {
  344. return false
  345. }
  346. }
  347. return true
  348. }
  349. type IBMConfigs struct {
  350. UsageAPI []*ibm.UsageConfiguration `json:"usageApi,omitempty"`
  351. }
  352. func (ic *IBMConfigs) Equals(that *IBMConfigs) bool {
  353. if ic == nil && that == nil {
  354. return true
  355. }
  356. if ic == nil || that == nil {
  357. return false
  358. }
  359. if len(ic.UsageAPI) != len(that.UsageAPI) {
  360. return false
  361. }
  362. for i, thisUsage := range ic.UsageAPI {
  363. thatUsage := that.UsageAPI[i]
  364. if !thisUsage.Equals(thatUsage) {
  365. return false
  366. }
  367. }
  368. return true
  369. }