bigqueryconfiguration.go 4.0 KB

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