2
0

bigqueryconfiguration.go 4.2 KB

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