-
Notifications
You must be signed in to change notification settings - Fork 68
/
client.go
372 lines (323 loc) · 9.74 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resultstore
import (
"context"
"crypto/x509"
"fmt"
"strings"
"github.com/google/uuid"
resultstore "google.golang.org/genproto/googleapis/devtools/resultstore/v2"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/metadata"
)
// Connect returns a secure gRPC connection.
//
// Authenticates as the service account if specified otherwise the default user.
func Connect(ctx context.Context, serviceAccountPath string) (*grpc.ClientConn, error) {
pool, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("system cert pool: %v", err)
}
creds := credentials.NewClientTLSFromCert(pool, "")
const scope = "https://www.googleapis.com/auth/cloud-platform"
var perRPC credentials.PerRPCCredentials
if serviceAccountPath != "" {
perRPC, err = oauth.NewServiceAccountFromFile(serviceAccountPath, scope)
} else {
perRPC, err = oauth.NewApplicationDefault(ctx, scope)
}
if err != nil {
return nil, fmt.Errorf("create oauth: %v", err)
}
conn, err := grpc.Dial(
"resultstore.googleapis.com:443",
grpc.WithTransportCredentials(creds),
grpc.WithPerRPCCredentials(perRPC),
)
if err != nil {
return nil, fmt.Errorf("dial: %v", err)
}
return conn, nil
}
// Secret represents a secret authorization uuid to protect invocations.
type Secret string
// UUID represents a universally "unique" identifier.
func UUID() string {
return uuid.New().String()
}
// NewSecret returns a new, unique identifier.
func NewSecret() Secret {
return Secret(UUID())
}
// Client provides ResultStore CRUD methods.
type Client struct {
up resultstore.ResultStoreUploadClient
down resultstore.ResultStoreDownloadClient
ctx context.Context
token string
}
// NewClient uses the specified gRPC connection to connect to ResultStore.
func NewClient(conn *grpc.ClientConn) *Client {
return &Client{
up: resultstore.NewResultStoreUploadClient(conn),
down: resultstore.NewResultStoreDownloadClient(conn),
ctx: context.Background(),
}
}
// WithContext uses the specified context for all RPCs.
func (c *Client) WithContext(ctx context.Context) *Client {
c.ctx = ctx
return c
}
// WithSecret applies the specified secret to all requests.
func (c *Client) WithSecret(authorizationToken Secret) *Client {
c.token = string(authorizationToken)
return c
}
// Access resources
// Invocations provides Invocation CRUD methods.
func (c Client) Invocations() Invocations {
return Invocations{
Client: c,
}
}
// Configurations provides CRUD methods for an invocation's configurations.
func (c Client) Configurations(invocationName string) Configurations {
return Configurations{
Client: c,
inv: invocationName,
}
}
// Targets provides CRUD methods for an invocations's targets.
func (c Client) Targets(invocationName string) Targets {
return Targets{
Client: c,
inv: invocationName,
}
}
// ConfiguredTargets provides CRUD methods for a target's configured targets.
func (c Client) ConfiguredTargets(targetName, configID string) ConfiguredTargets {
return ConfiguredTargets{
Client: c,
target: targetName,
config: configID,
}
}
// Actions provides CRUD methods for a configured target.
func (c Client) Actions(configuredTargetName string) Actions {
return Actions{
Client: c,
configuredTarget: configuredTargetName,
}
}
// Resources
// Invocations client.
type Invocations struct {
Client
}
// Targets client.
type Targets struct {
Client
inv string
}
// Configurations client.
type Configurations struct {
Client
inv string
}
// ConfiguredTargets client.
type ConfiguredTargets struct {
Client
target string
config string
}
// Actions client.
type Actions struct {
Client
configuredTarget string
}
// Mask methods
// fieldMask is required by gRPC for GET methods.
func fieldMask(ctx context.Context, fields ...string) context.Context {
return metadata.AppendToOutgoingContext(ctx, "X-Goog-FieldMask", strings.Join(fields, ","))
}
// listMask adds the required next_page_token for list requests, as well as any other methods.
func listMask(ctx context.Context, fields ...string) context.Context {
return fieldMask(ctx, append(fields, "next_page_token")...)
}
// Target methods
// Create a new target with the specified id (target basename), returing the fully qualified path.
func (t Targets) Create(id string, target Target) (string, error) {
tgt, err := t.up.CreateTarget(t.ctx, &resultstore.CreateTargetRequest{
Parent: t.inv,
TargetId: id,
Target: target.To(),
AuthorizationToken: t.token,
})
if err != nil {
return "", err
}
return tgt.Name, nil
}
// List requested fields in targets, does not currently handle paging.
func (t Targets) List(fields ...string) ([]Target, error) {
resp, err := t.down.ListTargets(listMask(t.ctx, fields...), &resultstore.ListTargetsRequest{
Parent: t.inv,
})
if err != nil {
return nil, err
}
var targets []Target
for _, r := range resp.Targets {
targets = append(targets, fromTarget(r))
}
return targets, nil
}
// Configuration methods
const (
// Default is the expected single-configuration id.
Default = "default"
)
// Create a new configuration using the specified basename, returning the fully qualified path.
func (c Configurations) Create(id string) (string, error) {
config, err := c.up.CreateConfiguration(c.ctx, &resultstore.CreateConfigurationRequest{
Parent: c.inv,
ConfigId: id,
AuthorizationToken: c.token,
// Configuration is useless
})
if err != nil {
return "", err
}
return config.Name, nil
}
// ConfiguredTarget methods
// Create a new configured target, returning the fully qualified path.
func (ct ConfiguredTargets) Create(act Action) (string, error) {
resp, err := ct.up.CreateConfiguredTarget(ct.ctx, &resultstore.CreateConfiguredTargetRequest{
Parent: ct.target,
ConfigId: ct.config,
AuthorizationToken: ct.token,
ConfiguredTarget: &resultstore.ConfiguredTarget{
StatusAttributes: status(act.Status, act.Description),
},
})
if err != nil {
return "", err
}
return resp.Name, nil
}
// Action methods
// Create a test action under the specified ID, returning the fully-qualified path.
//
// Technically there are also build actions, but these do not show up in the ResultStore UI.
func (a Actions) Create(id string, test Test) (string, error) {
resp, err := a.up.CreateAction(a.ctx, &resultstore.CreateActionRequest{
Parent: a.configuredTarget,
ActionId: id,
AuthorizationToken: a.token,
Action: test.To(),
})
if err != nil {
return "", err
}
return resp.Name, nil
}
// TestFields represent all fields this client cares about.
var TestFields = [...]string{
"actions.name",
"actions.test_action",
"actions.description",
"actions.timing",
}
// List tests in this configured target.
func (a Actions) List(fields ...string) ([]Test, error) {
if len(fields) == 0 {
fields = TestFields[:]
}
resp, err := a.down.ListActions(listMask(a.ctx, fields...), &resultstore.ListActionsRequest{
Parent: a.configuredTarget,
})
if err != nil {
return nil, err
}
var ret []Test
for _, r := range resp.Actions {
ret = append(ret, fromTest(r))
}
return ret, nil
}
// Invocation methods
// Create a new invocation (project must be specified).
func (i Invocations) Create(inv Invocation) (string, error) {
resp, err := i.up.CreateInvocation(i.ctx, &resultstore.CreateInvocationRequest{
Invocation: inv.To(),
AuthorizationToken: i.token,
})
if err != nil {
return "", err
}
return resp.Name, nil
}
// Update a pre-existing invocation at name.
func (i Invocations) Update(inv Invocation, fields ...string) error {
_, err := i.up.UpdateInvocation(i.ctx, &resultstore.UpdateInvocationRequest{
Invocation: inv.To(),
UpdateMask: &field_mask.FieldMask{
Paths: fields,
},
AuthorizationToken: i.token,
})
return err
}
// Finish an invocation, preventing further updates.
// TODO(fejta): consider renaming this to Finalize()
func (i Invocations) Finish(name string) error {
_, err := i.up.FinalizeInvocation(i.ctx, &resultstore.FinalizeInvocationRequest{
Name: name,
AuthorizationToken: i.token,
})
return err
}
// Get an existing invocation at name.
func (i Invocations) Get(name string, fields ...string) (*Invocation, error) {
inv, err := i.down.GetInvocation(fieldMask(i.ctx, fields...), &resultstore.GetInvocationRequest{Name: name})
if err != nil {
return nil, err
}
resp := fromInvocation(inv)
return &resp, nil
}
func convertToInvocations(results *resultstore.SearchInvocationsResponse) []*Invocation {
invocations := []*Invocation{}
for _, invocation := range results.Invocations {
inv := fromInvocation(invocation)
invocations = append(invocations, &inv)
}
return invocations
}
// Search finds all the invocations that satisfies the query condition within a project.
func (i Invocations) Search(ctx context.Context, projectID string, query string, fields ...string) ([]*Invocation, error) {
results, err := i.down.SearchInvocations(fieldMask(ctx, fields...), &resultstore.SearchInvocationsRequest{
ProjectId: projectID,
Query: query,
})
if err != nil {
return nil, err
}
return convertToInvocations(results), nil
}