bigqueryconfiguration.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package gcp
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "cloud.google.com/go/bigquery"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/util/json"
  9. "github.com/opencost/opencost/pkg/cloud"
  10. )
  11. type BigQueryConfiguration struct {
  12. ProjectID string `json:"projectID"`
  13. Dataset string `json:"dataset"`
  14. Table string `json:"table"`
  15. Authorizer Authorizer `json:"authorizer"`
  16. }
  17. func (bqc *BigQueryConfiguration) Validate() error {
  18. if bqc.Authorizer == nil {
  19. return fmt.Errorf("BigQueryConfig: missing configurer")
  20. }
  21. err := bqc.Authorizer.Validate()
  22. if err != nil {
  23. return fmt.Errorf("BigQueryConfig: issue with GCP Authorizer: %s", err.Error())
  24. }
  25. if bqc.ProjectID == "" {
  26. return fmt.Errorf("BigQueryConfig: missing ProjectID")
  27. }
  28. if bqc.Dataset == "" {
  29. return fmt.Errorf("BigQueryConfig: missing Dataset")
  30. }
  31. if bqc.Table == "" {
  32. return fmt.Errorf("BigQueryConfig: missing Table")
  33. }
  34. return nil
  35. }
  36. func (bqc *BigQueryConfiguration) Equals(config cloud.Config) bool {
  37. if config == nil {
  38. return false
  39. }
  40. thatConfig, ok := config.(*BigQueryConfiguration)
  41. if !ok {
  42. return false
  43. }
  44. if bqc.Authorizer != nil {
  45. if !bqc.Authorizer.Equals(thatConfig.Authorizer) {
  46. return false
  47. }
  48. } else {
  49. if thatConfig.Authorizer != nil {
  50. return false
  51. }
  52. }
  53. if bqc.ProjectID != thatConfig.ProjectID {
  54. return false
  55. }
  56. if bqc.Dataset != thatConfig.Dataset {
  57. return false
  58. }
  59. if bqc.Table != thatConfig.Table {
  60. return false
  61. }
  62. return true
  63. }
  64. func (bqc *BigQueryConfiguration) Sanitize() cloud.Config {
  65. return &BigQueryConfiguration{
  66. ProjectID: bqc.ProjectID,
  67. Dataset: bqc.Dataset,
  68. Table: bqc.Table,
  69. Authorizer: bqc.Authorizer.Sanitize().(Authorizer),
  70. }
  71. }
  72. // Key uses the Usage Project Id as the Provider Key for GCP
  73. func (bqc *BigQueryConfiguration) Key() string {
  74. return fmt.Sprintf("%s/%s", bqc.ProjectID, bqc.GetBillingDataDataset())
  75. }
  76. func (bqc *BigQueryConfiguration) Provider() string {
  77. return opencost.GCPProvider
  78. }
  79. func (bqc *BigQueryConfiguration) GetBillingDataDataset() string {
  80. return fmt.Sprintf("%s.%s", bqc.Dataset, bqc.Table)
  81. }
  82. func (bqc *BigQueryConfiguration) GetBigQueryClient(ctx context.Context) (*bigquery.Client, error) {
  83. clientOpts, err := bqc.Authorizer.CreateGCPClientOptions()
  84. if err != nil {
  85. return nil, err
  86. }
  87. return bigquery.NewClient(ctx, bqc.ProjectID, clientOpts...)
  88. }
  89. // UnmarshalJSON assumes data is save as an BigQueryConfigurationDTO
  90. func (bqc *BigQueryConfiguration) UnmarshalJSON(b []byte) error {
  91. var f interface{}
  92. err := json.Unmarshal(b, &f)
  93. if err != nil {
  94. return err
  95. }
  96. fmap := f.(map[string]interface{})
  97. projectID, err := cloud.GetInterfaceValue[string](fmap, "projectID")
  98. if err != nil {
  99. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  100. }
  101. bqc.ProjectID = projectID
  102. dataset, err := cloud.GetInterfaceValue[string](fmap, "dataset")
  103. if err != nil {
  104. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  105. }
  106. bqc.Dataset = dataset
  107. table, err := cloud.GetInterfaceValue[string](fmap, "table")
  108. if err != nil {
  109. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  110. }
  111. bqc.Table = table
  112. authAny, ok := fmap["authorizer"]
  113. if !ok {
  114. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
  115. }
  116. authorizer, err := cloud.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  117. if err != nil {
  118. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  119. }
  120. bqc.Authorizer = authorizer
  121. return nil
  122. }
  123. func ConvertBigQueryConfigToConfig(bqc BigQueryConfig) cloud.KeyedConfig {
  124. if bqc.IsEmpty() {
  125. return nil
  126. }
  127. BillingDataDataset := strings.Split(bqc.BillingDataDataset, ".")
  128. dataset := BillingDataDataset[0]
  129. var table string
  130. if len(BillingDataDataset) > 1 {
  131. table = BillingDataDataset[1]
  132. }
  133. bigQueryConfiguration := &BigQueryConfiguration{
  134. ProjectID: bqc.ProjectID,
  135. Dataset: dataset,
  136. Table: table,
  137. Authorizer: &WorkloadIdentity{}, // Default to WorkloadIdentity
  138. }
  139. if len(bqc.Key) != 0 {
  140. bigQueryConfiguration.Authorizer = &ServiceAccountKey{
  141. Key: bqc.Key,
  142. }
  143. }
  144. return bigQueryConfiguration
  145. }