bigqueryquerier.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package gcp
  2. import (
  3. "context"
  4. "cloud.google.com/go/bigquery"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. )
  7. type BigQueryQuerier struct {
  8. BigQueryConfiguration
  9. ConnectionStatus cloud.ConnectionStatus
  10. }
  11. func (bqq *BigQueryQuerier) GetStatus() cloud.ConnectionStatus {
  12. // initialize status if it has not done so; this can happen if the integration is inactive
  13. if bqq.ConnectionStatus.String() == "" {
  14. bqq.ConnectionStatus = cloud.InitialStatus
  15. }
  16. return bqq.ConnectionStatus
  17. }
  18. func (bqq *BigQueryQuerier) Equals(config cloud.Config) bool {
  19. thatConfig, ok := config.(*BigQueryQuerier)
  20. if !ok {
  21. return false
  22. }
  23. return bqq.BigQueryConfiguration.Equals(&thatConfig.BigQueryConfiguration)
  24. }
  25. func (bqq *BigQueryQuerier) Query(ctx context.Context, queryStr string) (*bigquery.RowIterator, error) {
  26. err := bqq.Validate()
  27. if err != nil {
  28. bqq.ConnectionStatus = cloud.InvalidConfiguration
  29. return nil, err
  30. }
  31. client, err := bqq.GetBigQueryClient(ctx)
  32. if err != nil {
  33. bqq.ConnectionStatus = cloud.FailedConnection
  34. return nil, err
  35. }
  36. query := client.Query(queryStr)
  37. iter, err := query.Read(ctx)
  38. // If result is empty and connection status is not already successful update status to missing data
  39. if iter == nil && bqq.ConnectionStatus != cloud.SuccessfulConnection {
  40. bqq.ConnectionStatus = cloud.MissingData
  41. return iter, nil
  42. }
  43. bqq.ConnectionStatus = cloud.SuccessfulConnection
  44. return iter, nil
  45. }