bigqueryconfiguration.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. Location string `json:"location"`
  17. QueryProjectID string `json:"queryProjectID"`
  18. Authorizer Authorizer `json:"authorizer"`
  19. }
  20. func (bqc *BigQueryConfiguration) Validate() error {
  21. if bqc.Authorizer == nil {
  22. return fmt.Errorf("BigQueryConfig: missing configurer")
  23. }
  24. err := bqc.Authorizer.Validate()
  25. if err != nil {
  26. return fmt.Errorf("BigQueryConfig: issue with GCP Authorizer: %s", err.Error())
  27. }
  28. if bqc.ProjectID == "" {
  29. return fmt.Errorf("BigQueryConfig: missing ProjectID")
  30. }
  31. if bqc.Dataset == "" {
  32. return fmt.Errorf("BigQueryConfig: missing Dataset")
  33. }
  34. if bqc.Table == "" {
  35. return fmt.Errorf("BigQueryConfig: missing Table")
  36. }
  37. return nil
  38. }
  39. func (bqc *BigQueryConfiguration) Equals(config cloud.Config) bool {
  40. if config == nil {
  41. return false
  42. }
  43. thatConfig, ok := config.(*BigQueryConfiguration)
  44. if !ok {
  45. return false
  46. }
  47. if bqc.Authorizer != nil {
  48. if !bqc.Authorizer.Equals(thatConfig.Authorizer) {
  49. return false
  50. }
  51. } else {
  52. if thatConfig.Authorizer != nil {
  53. return false
  54. }
  55. }
  56. if bqc.ProjectID != thatConfig.ProjectID {
  57. return false
  58. }
  59. if bqc.Dataset != thatConfig.Dataset {
  60. return false
  61. }
  62. if bqc.Table != thatConfig.Table {
  63. return false
  64. }
  65. if bqc.Location != thatConfig.Location {
  66. return false
  67. }
  68. bqcEffective := bqc.QueryProjectID
  69. if bqcEffective == "" {
  70. bqcEffective = bqc.ProjectID
  71. }
  72. thatEffective := thatConfig.QueryProjectID
  73. if thatEffective == "" {
  74. thatEffective = thatConfig.ProjectID
  75. }
  76. if bqcEffective != thatEffective {
  77. return false
  78. }
  79. return true
  80. }
  81. func (bqc *BigQueryConfiguration) Sanitize() cloud.Config {
  82. return &BigQueryConfiguration{
  83. ProjectID: bqc.ProjectID,
  84. Dataset: bqc.Dataset,
  85. Table: bqc.Table,
  86. Location: bqc.Location,
  87. QueryProjectID: bqc.QueryProjectID,
  88. Authorizer: bqc.Authorizer.Sanitize().(Authorizer),
  89. }
  90. }
  91. // Key uses the Usage Project Id as the Provider Key for GCP
  92. func (bqc *BigQueryConfiguration) Key() string {
  93. return fmt.Sprintf("%s/%s", bqc.ProjectID, bqc.GetBillingDataDataset())
  94. }
  95. func (bqc *BigQueryConfiguration) Provider() string {
  96. return opencost.GCPProvider
  97. }
  98. func (bqc *BigQueryConfiguration) GetBillingDataDataset() string {
  99. return fmt.Sprintf("%s.%s", bqc.Dataset, bqc.Table)
  100. }
  101. func (bqc *BigQueryConfiguration) GetBigQueryClient(ctx context.Context) (*bigquery.Client, error) {
  102. clientOpts, err := bqc.Authorizer.CreateGCPClientOptions()
  103. if err != nil {
  104. return nil, err
  105. }
  106. queryProjectID := bqc.QueryProjectID
  107. if queryProjectID == "" {
  108. queryProjectID = bqc.ProjectID
  109. }
  110. client, err := bigquery.NewClient(ctx, queryProjectID, clientOpts...)
  111. if err != nil {
  112. return nil, err
  113. }
  114. client.Location = bqc.Location
  115. return client, nil
  116. }
  117. // UnmarshalJSON assumes data is save as an BigQueryConfigurationDTO
  118. func (bqc *BigQueryConfiguration) UnmarshalJSON(b []byte) error {
  119. var f interface{}
  120. err := json.Unmarshal(b, &f)
  121. if err != nil {
  122. return err
  123. }
  124. fmap := f.(map[string]interface{})
  125. projectID, err := cloud.GetInterfaceValue[string](fmap, "projectID")
  126. if err != nil {
  127. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  128. }
  129. bqc.ProjectID = projectID
  130. dataset, err := cloud.GetInterfaceValue[string](fmap, "dataset")
  131. if err != nil {
  132. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  133. }
  134. bqc.Dataset = dataset
  135. table, err := cloud.GetInterfaceValue[string](fmap, "table")
  136. if err != nil {
  137. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  138. }
  139. bqc.Table = table
  140. if _, ok := fmap["location"]; ok {
  141. location, err := cloud.GetInterfaceValue[string](fmap, "location")
  142. if err != nil {
  143. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  144. }
  145. bqc.Location = location
  146. }
  147. if _, ok := fmap["queryProjectID"]; ok {
  148. queryProjectID, err := cloud.GetInterfaceValue[string](fmap, "queryProjectID")
  149. if err != nil {
  150. return fmt.Errorf("BigQueryConfiguration: UnmarshalJSON: %w", err)
  151. }
  152. bqc.QueryProjectID = queryProjectID
  153. }
  154. authAny, ok := fmap["authorizer"]
  155. if !ok {
  156. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
  157. }
  158. authorizer, err := cloud.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  159. if err != nil {
  160. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  161. }
  162. bqc.Authorizer = authorizer
  163. return nil
  164. }
  165. func ConvertBigQueryConfigToConfig(bqc BigQueryConfig) cloud.KeyedConfig {
  166. if bqc.IsEmpty() {
  167. return nil
  168. }
  169. BillingDataDataset := strings.Split(bqc.BillingDataDataset, ".")
  170. dataset := BillingDataDataset[0]
  171. var table string
  172. if len(BillingDataDataset) > 1 {
  173. table = BillingDataDataset[1]
  174. }
  175. bigQueryConfiguration := &BigQueryConfiguration{
  176. ProjectID: bqc.ProjectID,
  177. Dataset: dataset,
  178. Table: table,
  179. Authorizer: &WorkloadIdentity{}, // Default to WorkloadIdentity
  180. }
  181. if len(bqc.Key) != 0 {
  182. bigQueryConfiguration.Authorizer = &ServiceAccountKey{
  183. Key: bqc.Key,
  184. }
  185. }
  186. return bigQueryConfiguration
  187. }