-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.go
235 lines (197 loc) · 5.26 KB
/
module.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
225
226
227
228
229
230
231
232
233
234
235
package gcp
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"cloud.google.com/go/pubsub"
"github.com/grafana/sobek"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"google.golang.org/api/sheets/v4"
)
func init() {
modules.Register("k6/x/gcp", New())
}
type (
// RootModule is the global module instance that will create module
// instances for each VU.
RootModule struct{}
// ModuleInstance represents an instance of the JS module.
ModuleInstance struct {
// vu provides methods for accessing internal k6 objects for a VU
vu modules.VU
}
Gcp struct {
// vu modules.VU
emulatorHost string
keyByte []byte
scope []string
projectId string
// Client
sheet *sheets.Service
pubsub *pubsub.Client
}
GcpConfig struct {
// All gcloud emulator has to set XXX_EMULATOR_HOST environment variable
EmulatorHost string
Key ServiceAccountKey
Scope []string
ProjectId string
}
Option func(*Gcp) error
ServiceAccountKey struct {
AuthProviderX509CertUrl string `json:"auth_provider_x509_cert_url"`
AuthURL string `json:"auth_uri"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
ClientX509CertUrl string `json:"client_x509_cert_url"`
PrivateKey string `json:"private_key"`
PrivateKeyID string `json:"private_key_id"`
ProjectID string `json:"project_id"`
TokenURL string `json:"token_uri"`
Type string `json:"type"`
UniverseDomain string `json:"universe_domain"`
}
)
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
gcpConstructorDefaultScope = []string{"https://www.googleapis.com/auth/cloud-platform"}
)
func New() *RootModule {
return &RootModule{}
}
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{
vu: vu,
}
}
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
"Gcp": mi.newGcp,
},
}
}
func (mi *ModuleInstance) newGcp(c sobek.ConstructorCall) *sobek.Object {
rt := mi.vu.Runtime()
const envKey = "GOOGLE_SERVICE_ACCOUNT_KEY"
var options GcpConfig
err := rt.ExportTo(c.Argument(0), &options)
if err != nil {
common.Throw(rt,
fmt.Errorf("gcp constructor fails to read options: %w", err))
}
g, err := newGcpConstructor(
withGcpEmulatorHost(options.EmulatorHost),
withGcpConstructorKey(options.Key, envKey),
withGcpConstructorScope(options.Scope),
withGcpConstructorProjectId(options.ProjectId),
)
if err != nil {
common.Throw(rt, fmt.Errorf("cannot initialize gcp constructor <%w>", err))
}
return rt.ToValue(g).ToObject(rt)
}
func convertToByte(key interface{}) ([]byte, error) {
b, err := json.Marshal(key)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal key <%w>", err)
}
return b, nil
}
// The function creates a new instance of the Gcp struct with specified options.
func newGcpConstructor(opts ...Option) (Gcp, error) {
g := Gcp{
scope: gcpConstructorDefaultScope,
}
for _, opt := range opts {
if err := opt(&g); err != nil {
return Gcp{}, fmt.Errorf("gcp constructor fails to read options %w", err)
}
}
return g, nil
}
func withGcpConstructorKey(key ServiceAccountKey, env string) func(*Gcp) error {
return func(g *Gcp) error {
if !isStructEmpty(key) {
b, err := convertToByte(key)
if err != nil {
return err
}
g.keyByte = b
return nil
}
if envString := os.Getenv(env); envString != "" {
s := &ServiceAccountKey{}
err := json.Unmarshal([]byte(envString), &s)
if err != nil {
return fmt.Errorf("cannot unmarshal environment variable %v <%w>", env, err)
}
b, err := convertToByte(s)
if err != nil {
return err
}
g.keyByte = b
return nil
}
// Emulators doesn't need service account
if g.emulatorHost != "" {
return nil
}
return fmt.Errorf("service account key not found. Please use %s or input 'key' parameter", env)
}
}
func withGcpConstructorScope(scope []string) func(*Gcp) error {
return func(g *Gcp) error {
if len(scope) != 0 {
g.scope = scope
}
return nil
}
}
func withGcpConstructorProjectId(projectId string) func(*Gcp) error {
return func(g *Gcp) error {
if projectId != "" {
g.projectId = projectId
} else {
s := &ServiceAccountKey{}
err := json.Unmarshal(g.keyByte, s)
if err != nil {
log.Fatalf("unable to unmarshal byte <%v>", err)
}
g.projectId = s.ProjectID
}
return nil
}
}
func withGcpEmulatorHost(host string) func(*Gcp) error {
return func(g *Gcp) error {
if host != "" {
g.emulatorHost = host
}
return nil
}
}
func isStructEmpty(object interface{}) bool {
// check normal definitions of empty
if object == nil {
return true
} else if object == "" {
return true
} else if object == false {
return true
}
// see if it's a struct
if reflect.ValueOf(object).Kind() == reflect.Struct {
// and create an empty copy of the struct object to compare against
empty := reflect.New(reflect.TypeOf(object)).Elem().Interface()
if reflect.DeepEqual(object, empty) {
return true
}
}
return false
}