-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigtable.go
47 lines (36 loc) · 1.14 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
package main
import (
"context"
"os"
"strings"
"cloud.google.com/go/bigtable"
"google.golang.org/api/option"
)
const emulatorHostDefault = "BIGTABLE_EMULATOR_HOST"
const emulatorDefaultHostValue = "localhost:8086"
func newBigTableClient(project, instance string, opts ...option.ClientOption) (*bigtable.Client, error) {
ctx := context.Background()
optionalTestEnv(project, instance)
client, err := bigtable.NewClient(ctx, project, instance, opts...)
if err != nil {
return nil, err
}
return client, nil
}
func newBigTableAdminClient(project, instance string, opts ...option.ClientOption) (*bigtable.AdminClient, error) {
ctx := context.Background()
optionalTestEnv(project, instance)
client, err := bigtable.NewAdminClient(ctx, project, instance, opts...)
if err != nil {
return nil, err
}
return client, nil
}
func optionalTestEnv(project, instance string) {
if isTestEnv(project, instance) && (os.Getenv(emulatorHostDefault) == "") {
os.Setenv(emulatorHostDefault, emulatorDefaultHostValue)
}
}
func isTestEnv(project, instance string) bool {
return (strings.HasPrefix(project, "dev") || strings.HasPrefix(instance, "dev"))
}