-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
schema.go
341 lines (300 loc) · 10.6 KB
/
schema.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
// Copyright 2021 Google LLC
//
// 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 pubsub
import (
"context"
"fmt"
"time"
"google.golang.org/api/option"
vkit "cloud.google.com/go/pubsub/apiv1"
pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
)
// SchemaClient is a Pub/Sub schema client scoped to a single project.
type SchemaClient struct {
sc *vkit.SchemaClient
projectID string
}
// Close closes the schema client and frees up resources.
func (s *SchemaClient) Close() error {
return s.sc.Close()
}
// NewSchemaClient creates a new Pub/Sub Schema client.
func NewSchemaClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*SchemaClient, error) {
sc, err := vkit.NewSchemaClient(ctx, opts...)
if err != nil {
return nil, err
}
return &SchemaClient{sc: sc, projectID: projectID}, nil
}
// SchemaConfig is a reference to a PubSub schema.
type SchemaConfig struct {
// Name of the schema.
// Format is `projects/{project}/schemas/{schema}`
Name string
// The type of the schema definition.
Type SchemaType
// The definition of the schema. This should contain a string representing
// the full definition of the schema that is a valid schema definition of
// the type specified in `type`.
Definition string
// RevisionID is the revision ID of the schema.
// This field is output only.
RevisionID string
// RevisionCreateTime is the timestamp that the revision was created.
// This field is output only.
RevisionCreateTime time.Time
}
// SchemaType is the possible schema definition types.
type SchemaType pb.Schema_Type
const (
// SchemaTypeUnspecified is the unused default value.
SchemaTypeUnspecified SchemaType = 0
// SchemaProtocolBuffer is a protobuf schema definition.
SchemaProtocolBuffer SchemaType = 1
// SchemaAvro is an Avro schema definition.
SchemaAvro SchemaType = 2
)
// SchemaView is a view of Schema object fields to be returned
// by GetSchema and ListSchemas.
type SchemaView pb.SchemaView
const (
// SchemaViewUnspecified is the default/unset value.
SchemaViewUnspecified SchemaView = 0
// SchemaViewBasic includes the name and type of the schema, but not the definition.
SchemaViewBasic SchemaView = 1
// SchemaViewFull includes all Schema object fields.
SchemaViewFull SchemaView = 2
)
// SchemaSettings are settings for validating messages
// published against a schema.
type SchemaSettings struct {
// The name of the schema that messages published should be
// validated against. Format is `projects/{project}/schemas/{schema}`
Schema string
// The encoding of messages validated against the schema.
Encoding SchemaEncoding
// The minimum (inclusive) revision allowed for validating messages. If empty
// or not present, allow any revision to be validated against LastRevisionID or
// any revision created before.
FirstRevisionID string
// The maximum (inclusive) revision allowed for validating messages. If empty
// or not present, allow any revision to be validated against FirstRevisionID
// or any revision created after.
LastRevisionID string
}
func schemaSettingsToProto(schema *SchemaSettings) *pb.SchemaSettings {
if schema == nil {
return nil
}
return &pb.SchemaSettings{
Schema: schema.Schema,
Encoding: pb.Encoding(schema.Encoding),
FirstRevisionId: schema.FirstRevisionID,
LastRevisionId: schema.LastRevisionID,
}
}
func protoToSchemaSettings(pbs *pb.SchemaSettings) *SchemaSettings {
if pbs == nil {
return nil
}
return &SchemaSettings{
Schema: pbs.Schema,
Encoding: SchemaEncoding(pbs.Encoding),
FirstRevisionID: pbs.FirstRevisionId,
LastRevisionID: pbs.LastRevisionId,
}
}
// SchemaEncoding is the encoding expected for messages.
type SchemaEncoding pb.Encoding
const (
// EncodingUnspecified is the default unused value.
EncodingUnspecified SchemaEncoding = 0
// EncodingJSON is the JSON encoding type for a message.
EncodingJSON SchemaEncoding = 1
// EncodingBinary is the binary encoding type for a message.
// For some schema types, binary encoding may not be available.
EncodingBinary SchemaEncoding = 2
)
func (s *SchemaConfig) toProto() *pb.Schema {
pbs := &pb.Schema{
Name: s.Name,
Type: pb.Schema_Type(s.Type),
Definition: s.Definition,
}
return pbs
}
func protoToSchemaConfig(pbs *pb.Schema) *SchemaConfig {
return &SchemaConfig{
Name: pbs.Name,
Type: SchemaType(pbs.Type),
Definition: pbs.Definition,
RevisionID: pbs.RevisionId,
RevisionCreateTime: pbs.RevisionCreateTime.AsTime(),
}
}
// CreateSchema creates a new schema with the given schemaID
// and config. Schemas cannot be updated after creation.
func (c *SchemaClient) CreateSchema(ctx context.Context, schemaID string, s SchemaConfig) (*SchemaConfig, error) {
req := &pb.CreateSchemaRequest{
Parent: fmt.Sprintf("projects/%s", c.projectID),
Schema: s.toProto(),
SchemaId: schemaID,
}
pbs, err := c.sc.CreateSchema(ctx, req)
if err != nil {
return nil, err
}
return protoToSchemaConfig(pbs), nil
}
// Schema retrieves the configuration of a schema given a schemaID and a view.
func (c *SchemaClient) Schema(ctx context.Context, schemaID string, view SchemaView) (*SchemaConfig, error) {
schemaPath := fmt.Sprintf("projects/%s/schemas/%s", c.projectID, schemaID)
req := &pb.GetSchemaRequest{
Name: schemaPath,
View: pb.SchemaView(view),
}
s, err := c.sc.GetSchema(ctx, req)
if err != nil {
return nil, err
}
return protoToSchemaConfig(s), nil
}
// Schemas returns an iterator which returns all of the schemas for the client's project.
func (c *SchemaClient) Schemas(ctx context.Context, view SchemaView) *SchemaIterator {
return &SchemaIterator{
it: c.sc.ListSchemas(ctx, &pb.ListSchemasRequest{
Parent: fmt.Sprintf("projects/%s", c.projectID),
View: pb.SchemaView(view),
}),
}
}
// SchemaIterator is a struct used to iterate over schemas.
type SchemaIterator struct {
it *vkit.SchemaIterator
err error
}
// Next returns the next schema. If there are no more schemas, iterator.Done will be returned.
func (s *SchemaIterator) Next() (*SchemaConfig, error) {
if s.err != nil {
return nil, s.err
}
pbs, err := s.it.Next()
if err != nil {
return nil, err
}
return protoToSchemaConfig(pbs), nil
}
// ListSchemaRevisions lists all schema revisions for the named schema.
func (c *SchemaClient) ListSchemaRevisions(ctx context.Context, schemaID string, view SchemaView) *SchemaIterator {
return &SchemaIterator{
it: c.sc.ListSchemaRevisions(ctx, &pb.ListSchemaRevisionsRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", c.projectID, schemaID),
View: pb.SchemaView(view),
}),
}
}
// CommitSchema commits a new schema revision to an existing schema.
func (c *SchemaClient) CommitSchema(ctx context.Context, schemaID string, s SchemaConfig) (*SchemaConfig, error) {
req := &pb.CommitSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", c.projectID, schemaID),
Schema: s.toProto(),
}
pbs, err := c.sc.CommitSchema(ctx, req)
if err != nil {
return nil, err
}
return protoToSchemaConfig(pbs), nil
}
// RollbackSchema creates a new schema revision that is a copy of the provided revision.
func (c *SchemaClient) RollbackSchema(ctx context.Context, schemaID, revisionID string) (*SchemaConfig, error) {
req := &pb.RollbackSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", c.projectID, schemaID),
RevisionId: revisionID,
}
pbs, err := c.sc.RollbackSchema(ctx, req)
if err != nil {
return nil, err
}
return protoToSchemaConfig(pbs), nil
}
// DeleteSchemaRevision deletes a specific schema revision.
func (c *SchemaClient) DeleteSchemaRevision(ctx context.Context, schemaID, revisionID string) (*SchemaConfig, error) {
schemaPath := fmt.Sprintf("projects/%s/schemas/%s@%s", c.projectID, schemaID, revisionID)
schema, err := c.sc.DeleteSchemaRevision(ctx, &pb.DeleteSchemaRevisionRequest{
Name: schemaPath,
})
if err != nil {
return nil, err
}
return protoToSchemaConfig(schema), nil
}
// DeleteSchema deletes an existing schema given a schema ID.
func (c *SchemaClient) DeleteSchema(ctx context.Context, schemaID string) error {
schemaPath := fmt.Sprintf("projects/%s/schemas/%s", c.projectID, schemaID)
return c.sc.DeleteSchema(ctx, &pb.DeleteSchemaRequest{
Name: schemaPath,
})
}
// ValidateSchemaResult is the response for the ValidateSchema method.
// Reserved for future use.
type ValidateSchemaResult struct{}
// ValidateSchema validates a schema config and returns an error if invalid.
func (c *SchemaClient) ValidateSchema(ctx context.Context, schema SchemaConfig) (*ValidateSchemaResult, error) {
req := &pb.ValidateSchemaRequest{
Parent: fmt.Sprintf("projects/%s", c.projectID),
Schema: schema.toProto(),
}
_, err := c.sc.ValidateSchema(ctx, req)
if err != nil {
return nil, err
}
return &ValidateSchemaResult{}, nil
}
// ValidateMessageResult is the response for the ValidateMessage method.
// Reserved for future use.
type ValidateMessageResult struct{}
// ValidateMessageWithConfig validates a message against an schema specified
// by a schema config.
func (c *SchemaClient) ValidateMessageWithConfig(ctx context.Context, msg []byte, encoding SchemaEncoding, config SchemaConfig) (*ValidateMessageResult, error) {
req := &pb.ValidateMessageRequest{
Parent: fmt.Sprintf("projects/%s", c.projectID),
SchemaSpec: &pb.ValidateMessageRequest_Schema{
Schema: config.toProto(),
},
Message: msg,
Encoding: pb.Encoding(encoding),
}
_, err := c.sc.ValidateMessage(ctx, req)
if err != nil {
return nil, err
}
return &ValidateMessageResult{}, nil
}
// ValidateMessageWithID validates a message against an schema specified
// by the schema ID of an existing schema.
func (c *SchemaClient) ValidateMessageWithID(ctx context.Context, msg []byte, encoding SchemaEncoding, schemaID string) (*ValidateMessageResult, error) {
req := &pb.ValidateMessageRequest{
Parent: fmt.Sprintf("projects/%s", c.projectID),
SchemaSpec: &pb.ValidateMessageRequest_Name{
Name: fmt.Sprintf("projects/%s/schemas/%s", c.projectID, schemaID),
},
Message: msg,
Encoding: pb.Encoding(encoding),
}
_, err := c.sc.ValidateMessage(ctx, req)
if err != nil {
return nil, err
}
return &ValidateMessageResult{}, nil
}