bigqueryconfiguration.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if bqc.ExcludePartitionTime != thatConfig.ExcludePartitionTime {
  80. return false
  81. }
  82. return true
  83. }
  84. func (bqc *BigQueryConfiguration) Sanitize() cloud.Config {
  85. return &BigQueryConfiguration{
  86. ProjectID: bqc.ProjectID,
  87. Dataset: bqc.Dataset,
  88. Table: bqc.Table,
  89. Location: bqc.Location,
  90. ExcludePartitionTime: bqc.ExcludePartitionTime,
  91. QueryProjectID: bqc.QueryProjectID,
  92. Authorizer: bqc.Authorizer.Sanitize().(Authorizer),
  93. }
  94. }
  95. // Key uses the Usage Project Id as the Provider Key for GCP
  96. func (bqc *BigQueryConfiguration) Key() string {
  97. return fmt.Sprintf("%s/%s", bqc.ProjectID, bqc.GetBillingDataDataset())
  98. }
  99. func (bqc *BigQueryConfiguration) Provider() string {
  100. return opencost.GCPProvider
  101. }
  102. func (bqc *BigQueryConfiguration) GetBillingDataDataset() string {
  103. return fmt.Sprintf("%s.%s", bqc.Dataset, bqc.Table)
  104. }
  105. func (bqc *BigQueryConfiguration) GetBigQueryClient(ctx context.Context) (*bigquery.Client, error) {
  106. clientOpts, err := bqc.Authorizer.CreateGCPClientOptions()
  107. if err != nil {
  108. return nil, err
  109. }
  110. queryProjectID := bqc.QueryProjectID
  111. if queryProjectID == "" {
  112. queryProjectID = bqc.ProjectID
  113. }
  114. client, err := bigquery.NewClient(ctx, queryProjectID, clientOpts...)
  115. if err != nil {
  116. return nil, err
  117. }
  118. client.Location = bqc.Location
  119. return client, nil
  120. }
  121. // UnmarshalJSON assumes data is save as an BigQueryConfigurationDTO
  122. func (bqc *BigQueryConfiguration) UnmarshalJSON(b []byte) error {
  123. var f interface{}
  124. err := json.Unmarshal(b, &f)
  125. if err != nil {
  126. return err
  127. }
  128. fmap := f.(map[string]interface{})
  129. projectID, err := cloud.GetInterfaceValue[string](fmap, "projectID")
  130. if err != nil {
  131. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  132. }
  133. bqc.ProjectID = projectID
  134. dataset, err := cloud.GetInterfaceValue[string](fmap, "dataset")
  135. if err != nil {
  136. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  137. }
  138. bqc.Dataset = dataset
  139. table, err := cloud.GetInterfaceValue[string](fmap, "table")
  140. if err != nil {
  141. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  142. }
  143. bqc.Table = table
  144. if _, ok := fmap["location"]; ok {
  145. location, err := cloud.GetInterfaceValue[string](fmap, "location")
  146. if err != nil {
  147. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  148. }
  149. bqc.Location = location
  150. }
  151. if _, ok := fmap["excludePartitionTime"]; ok {
  152. excludePartitionTime, err := cloud.GetInterfaceValue[bool](fmap, "excludePartitionTime")
  153. if err != nil {
  154. return fmt.Errorf("BigQueryConfiguration: FromInterface: %s", err.Error())
  155. }
  156. bqc.ExcludePartitionTime = excludePartitionTime
  157. }
  158. if _, ok := fmap["queryProjectID"]; ok {
  159. queryProjectID, err := cloud.GetInterfaceValue[string](fmap, "queryProjectID")
  160. if err != nil {
  161. return fmt.Errorf("BigQueryConfiguration: UnmarshalJSON: %w", err)
  162. }
  163. bqc.QueryProjectID = queryProjectID
  164. }
  165. authAny, ok := fmap["authorizer"]
  166. if !ok {
  167. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
  168. }
  169. authorizer, err := cloud.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  170. if err != nil {
  171. return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
  172. }
  173. bqc.Authorizer = authorizer
  174. return nil
  175. }
  176. func ConvertBigQueryConfigToConfig(bqc BigQueryConfig) cloud.KeyedConfig {
  177. if bqc.IsEmpty() {
  178. return nil
  179. }
  180. BillingDataDataset := strings.Split(bqc.BillingDataDataset, ".")
  181. dataset := BillingDataDataset[0]
  182. var table string
  183. if len(BillingDataDataset) > 1 {
  184. table = BillingDataDataset[1]
  185. }
  186. bigQueryConfiguration := &BigQueryConfiguration{
  187. ProjectID: bqc.ProjectID,
  188. Dataset: dataset,
  189. Table: table,
  190. Authorizer: &WorkloadIdentity{}, // Default to WorkloadIdentity
  191. }
  192. if len(bqc.Key) != 0 {
  193. bigQueryConfiguration.Authorizer = &ServiceAccountKey{
  194. Key: bqc.Key,
  195. }
  196. }
  197. return bigQueryConfiguration
  198. }