-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbigtable.go
executable file
·224 lines (190 loc) · 6.16 KB
/
bigtable.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package bigtable
import (
"context"
_ "embed" // used to print the embedded assets
"encoding/base64"
"encoding/json"
"fmt"
"sync"
"cloud.google.com/go/bigtable"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/meteor/models"
v1beta2 "github.com/raystack/meteor/models/raystack/assets/v1beta2"
"github.com/raystack/meteor/plugins"
"github.com/raystack/meteor/registry"
"github.com/raystack/meteor/utils"
"github.com/raystack/salt/log"
"google.golang.org/api/option"
"google.golang.org/protobuf/types/known/anypb"
)
//go:embed README.md
var summary string
const (
service = "bigtable"
)
// Config holds the configurations for the bigtable extractor
type Config struct {
ProjectID string `json:"project_id" yaml:"project_id" mapstructure:"project_id" validate:"required"`
ServiceAccountBase64 string `mapstructure:"service_account_base64"`
serviceAccountJSON []byte
}
var info = plugins.Info{
Description: "Compressed, high-performance, data storage system.",
Summary: summary,
Tags: []string{"gcp", "extractor"},
SampleConfig: heredoc.Doc(`
project_id: google-project-id
service_account_base64: ____base64_encoded_service_account____
`),
}
// InstancesFetcher is an interface for fetching instances
type InstancesFetcher interface {
Instances(context.Context) ([]*bigtable.InstanceInfo, error)
}
var (
instanceAdminClientCreator = createInstanceAdminClient
instanceInfoGetter = getInstancesInfo
)
// Extractor used to extract bigtable metadata
type Extractor struct {
plugins.BaseExtractor
config Config
logger log.Logger
instanceNames []string
newClient NewClientFunc
newAdminClient NewAdminClientFunc
}
// InstanceAdminClient is an interface for *bigtable.InstanceAdminClient
//
//go:generate mockery --name=InstanceAdminClient -r --case underscore --with-expecter --structname InstanceAdminClient --output=./mocks
type InstanceAdminClient interface {
Instances(ctx context.Context) ([]*bigtable.InstanceInfo, error)
}
// AdminClient is an interface for *bigtable.AdminClient
//
//go:generate mockery --name=AdminClient -r --case underscore --with-expecter --structname AdminClient --output=./mocks
type AdminClient interface {
Tables(ctx context.Context) ([]string, error)
TableInfo(ctx context.Context, table string) (*bigtable.TableInfo, error)
}
type (
NewClientFunc func(ctx context.Context, cfg Config) (InstanceAdminClient, error)
NewAdminClientFunc func(ctx context.Context, instance string, config Config) (AdminClient, error)
)
func New(logger log.Logger, newClient NewClientFunc, newAdminClient NewAdminClientFunc) *Extractor {
e := &Extractor{
logger: logger,
newClient: newClient,
newAdminClient: newAdminClient,
}
e.BaseExtractor = plugins.NewBaseExtractor(info, &e.config)
e.ScopeNotRequired = true
return e
}
func (e *Extractor) Init(ctx context.Context, config plugins.Config) error {
if err := e.BaseExtractor.Init(ctx, config); err != nil {
return err
}
err := e.decodeServiceAccount()
if err != nil {
return err
}
client, err := e.newClient(ctx, e.config)
if err != nil {
return err
}
client = WithInstanceAdminClientMW(e.config.ProjectID)(client)
e.instanceNames, err = instanceInfoGetter(ctx, client)
if err != nil {
return err
}
return nil
}
// Extract checks if the extractor is configured and
// if so, then extracts the metadata and
// returns the assets.
func (e *Extractor) Extract(ctx context.Context, emit plugins.Emit) error {
return e.getTablesInfo(ctx, emit)
}
func getInstancesInfo(ctx context.Context, client InstancesFetcher) ([]string, error) {
instanceInfos, err := client.Instances(ctx)
if err != nil {
return nil, err
}
var instanceNames []string
for i := 0; i < len(instanceInfos); i++ {
instanceNames = append(instanceNames, instanceInfos[i].Name)
}
return instanceNames, nil
}
func (e *Extractor) getTablesInfo(ctx context.Context, emit plugins.Emit) error {
for _, instance := range e.instanceNames {
adminClient, err := e.newAdminClient(ctx, instance, e.config)
if err != nil {
return err
}
adminClient = WithAdminClientMW(e.config.ProjectID, instance)(adminClient)
tables, _ := adminClient.Tables(ctx)
var wg sync.WaitGroup
for _, table := range tables {
wg.Add(1)
go func(table string) {
defer wg.Done()
tableInfo, err := adminClient.TableInfo(ctx, table)
if err != nil {
return
}
familyInfoBytes, _ := json.Marshal(tableInfo.FamilyInfos)
tableMeta, err := anypb.New(&v1beta2.Table{
Attributes: utils.TryParseMapToProto(map[string]interface{}{
"column_family": string(familyInfoBytes),
}),
})
if err != nil {
e.logger.Warn("error creating Any struct", "error", err)
}
asset := v1beta2.Asset{
Urn: models.NewURN(service, e.config.ProjectID, "table", fmt.Sprintf("%s.%s", instance, table)),
Name: table,
Service: service,
Type: "table",
Data: tableMeta,
}
emit(models.NewRecord(&asset))
}(table)
}
wg.Wait()
}
return nil
}
func (c Config) clientOptions() []option.ClientOption {
if c.serviceAccountJSON == nil {
return nil
}
return []option.ClientOption{option.WithCredentialsJSON(c.serviceAccountJSON)}
}
func createInstanceAdminClient(ctx context.Context, config Config) (InstanceAdminClient, error) {
return bigtable.NewInstanceAdminClient(ctx, config.ProjectID, config.clientOptions()...)
}
func createAdminClient(ctx context.Context, instance string, config Config) (AdminClient, error) {
return bigtable.NewAdminClient(ctx, config.ProjectID, instance, config.clientOptions()...)
}
func (e *Extractor) decodeServiceAccount() error {
if e.config.ServiceAccountBase64 == "" {
return nil
}
serviceAccountJSON, err := base64.StdEncoding.DecodeString(e.config.ServiceAccountBase64)
if err != nil || len(serviceAccountJSON) == 0 {
return fmt.Errorf("decode Base64 encoded service account: %w", err)
}
e.config.serviceAccountJSON = serviceAccountJSON
return nil
}
// Register the extractor to catalog
func init() {
if err := registry.Extractors.Register("bigtable", func() plugins.Extractor {
return New(plugins.GetLog(), instanceAdminClientCreator, createAdminClient)
}); err != nil {
panic(err)
}
}