-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
130 lines (105 loc) · 3.89 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package bqutil
import (
"context"
"os"
"time"
"cloud.google.com/go/bigquery"
"github.com/pkg/errors"
"github.com/stackrox/infra/config"
"github.com/stackrox/infra/pkg/logging"
"google.golang.org/api/option"
)
const (
bigqueryInsertTimeout = 10 * time.Second
)
// BigQueryClient is a BigQuery client that operates on `IngestionRecord`s.
type BigQueryClient interface {
InsertClusterCreationRecord(ctx context.Context, clusterID, workflowName, flavor, actor string) error
InsertClusterDeletionRecord(ctx context.Context, clusterID, workflowName string) error
}
var (
log = logging.CreateProductionLogger()
_ BigQueryClient = (*enabledClient)(nil)
_ BigQueryClient = (*disabledClient)(nil)
)
type enabledClient struct {
environment string
creationInserter *bigquery.Inserter
deletionInserter *bigquery.Inserter
}
type disabledClient struct{}
func (*disabledClient) InsertClusterCreationRecord(_ context.Context, _, _, _, _ string) error {
return nil
}
func (*disabledClient) InsertClusterDeletionRecord(_ context.Context, _, _ string) error {
return nil
}
type clusterCreationRecord struct {
Environment string
ClusterID string
WorkflowName string
Flavor string
Actor string
CreationTimestamp time.Time
}
type clusterDeletionRecord struct {
Environment string
ClusterID string
WorkflowName string
DeletionTimestamp time.Time
}
// NewClient returns a new BigQuery client
func NewClient(cfg *config.BigQueryConfig) (BigQueryClient, error) {
if os.Getenv("TEST_MODE") == "true" {
log.Log(logging.INFO, "disabling BigQuery integration because we are in TEST_MODE")
return &disabledClient{}, nil
}
// If the config was missing a BigQuery configuration, disable the integration
// altogether.
if cfg == nil {
log.Log(logging.INFO, "disabling BigQuery integration due to missing configuration")
return &disabledClient{}, nil
}
if cfg.CredentialsFile == "" || cfg.Environment == "" || cfg.Project == "" || cfg.Dataset == "" || cfg.CreationTable == "" || cfg.DeletionTable == "" {
return nil, errors.Errorf("malformed BigQuery config: all of credentialsFile, environment, project, dataset, tables must be defined")
}
client, err := bigquery.NewClient(context.Background(), cfg.Project, option.WithCredentialsFile(cfg.CredentialsFile))
if err != nil {
return nil, errors.Wrap(err, "creating BigQuery client")
}
creationInserter := client.Dataset(cfg.Dataset).Table(cfg.CreationTable).Inserter()
deletionInserter := client.Dataset(cfg.Dataset).Table(cfg.DeletionTable).Inserter()
bigQueryClient := &enabledClient{
environment: cfg.Environment,
creationInserter: creationInserter,
deletionInserter: deletionInserter,
}
log.Log(logging.INFO, "enabled BigQuery integration")
return bigQueryClient, nil
}
// InsertClusterCreationRecord inserts a new cluster creation record into BigQuery.
func (c *enabledClient) InsertClusterCreationRecord(ctx context.Context, clusterID, workflowName, flavor, actor string) error {
subCtx, cancel := context.WithTimeout(ctx, bigqueryInsertTimeout)
defer cancel()
clusterCreationRecord := &clusterCreationRecord{
Environment: c.environment,
ClusterID: clusterID,
WorkflowName: workflowName,
Flavor: flavor,
Actor: actor,
CreationTimestamp: time.Now(),
}
return c.creationInserter.Put(subCtx, clusterCreationRecord)
}
// InsertClusterDeletionRecord inserts a new cluster deletion record into BigQuery.
func (c *enabledClient) InsertClusterDeletionRecord(ctx context.Context, clusterID, workflowName string) error {
subCtx, cancel := context.WithTimeout(ctx, bigqueryInsertTimeout)
defer cancel()
clusterDeletionRecord := &clusterDeletionRecord{
Environment: c.environment,
ClusterID: clusterID,
WorkflowName: workflowName,
DeletionTimestamp: time.Now(),
}
return c.deletionInserter.Put(subCtx, clusterDeletionRecord)
}