-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
66 lines (55 loc) · 1.38 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
package gcpsecretfetch
import (
secretmanager "cloud.google.com/go/secretmanager/apiv1"
"context"
"github.com/pkg/errors"
"github.com/spf13/viper"
"google.golang.org/api/option"
)
const defaultConcurrency = 50
type secretClient struct {
gcpClient *secretmanager.Client
project string
ctx context.Context
v *viper.Viper
concurrency int
disablePrior bool
}
type ConfigOption func(client *secretClient)
func WithViper(v *viper.Viper) ConfigOption {
return func(client *secretClient) {
client.v = v
}
}
func WithConcurrency(n int) ConfigOption {
return func(client *secretClient) {
client.concurrency = n
}
}
func WithDisablePrior() ConfigOption {
return func(client *secretClient) {
client.disablePrior = true
}
}
func newClient(project string, opts []ConfigOption) (*secretClient, error) {
ctx := context.Background()
gcpClient, err := secretmanager.NewClient(ctx, option.WithScopes("https://www.googleapis.com/auth/cloud-platform"))
if err != nil {
return nil, errors.Wrap(err, "could not create secretmanager client")
}
svc := &secretClient{
gcpClient: gcpClient,
project: project,
ctx: ctx,
concurrency: defaultConcurrency,
}
// Loop through each option
if opts != nil {
for _, opt := range opts {
// Call the option giving the instantiated
// *House as the argument
opt(svc)
}
}
return svc, nil
}