Skip to content

feat(pubsub): rewrite v2 schema samples #5340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: pubsub-v2-samples-trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pubsub/schemas/commit_avro_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
"io"
"os"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

// commitAvroSchema commits a new Avro schema revision to an existing schema.
Expand All @@ -30,7 +31,7 @@ func commitAvroSchema(w io.Writer, projectID, schemaID, avscFile string) error {
// schemaID := "my-schema-id"
// avscFile = "path/to/an/avro/schema/file(.avsc)/formatted/in/json"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
Expand All @@ -42,14 +43,18 @@ func commitAvroSchema(w io.Writer, projectID, schemaID, avscFile string) error {
return fmt.Errorf("error reading from file: %s", avscFile)
}

config := pubsub.SchemaConfig{
schema := &pubsubpb.Schema{
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
Type: pubsub.SchemaAvro,
Type: pubsubpb.Schema_AVRO,
Definition: string(avscSource),
}
s, err := client.CommitSchema(ctx, schemaID, config)
req := &pubsubpb.CommitSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
Schema: schema,
}
s, err := client.CommitSchema(ctx, req)
if err != nil {
return fmt.Errorf("CommitSchema: %w", err)
return fmt.Errorf("error calling CommitSchema: %w", err)
}
fmt.Fprintf(w, "Committed a schema using an Avro schema: %#v\n", s)
return nil
Expand Down
16 changes: 11 additions & 5 deletions pubsub/schemas/commit_proto_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
"io"
"os"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

// commitProtoSchema commits a new proto schema revision to an existing schema.
Expand All @@ -30,7 +31,7 @@ func commitProtoSchema(w io.Writer, projectID, schemaID, protoFile string) error
// schemaID := "my-schema"
// protoFile = "path/to/a/proto/schema/file(.proto)/formatted/in/protocol/buffers"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
Expand All @@ -42,12 +43,17 @@ func commitProtoSchema(w io.Writer, projectID, schemaID, protoFile string) error
return fmt.Errorf("error reading from file: %s", protoFile)
}

config := pubsub.SchemaConfig{
schema := &pubsubpb.Schema{
// TODO(hongalex): check if name is necessary here
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
Type: pubsub.SchemaProtocolBuffer,
Type: pubsubpb.Schema_PROTOCOL_BUFFER,
Definition: string(protoSource),
}
s, err := client.CommitSchema(ctx, schemaID, config)
req := &pubsubpb.CommitSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
Schema: schema,
}
s, err := client.CommitSchema(ctx, req)
if err != nil {
return fmt.Errorf("CommitSchema: %w", err)
}
Expand Down
19 changes: 12 additions & 7 deletions pubsub/schemas/create_avro_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
"io"
"os"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

// createAvroSchema creates a schema resource from a JSON-formatted Avro schema file.
Expand All @@ -30,7 +31,7 @@ func createAvroSchema(w io.Writer, projectID, schemaID, avscFile string) error {
// schemaID := "my-schema"
// avscFile = "path/to/an/avro/schema/file(.avsc)/formatted/in/json"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
Expand All @@ -41,13 +42,17 @@ func createAvroSchema(w io.Writer, projectID, schemaID, avscFile string) error {
return fmt.Errorf("error reading from file: %s", avscFile)
}

config := pubsub.SchemaConfig{
Type: pubsub.SchemaAvro,
Definition: string(avscSource),
req := &pubsubpb.CreateSchemaRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
Schema: &pubsubpb.Schema{
Type: pubsubpb.Schema_AVRO,
Definition: string(avscSource),
},
SchemaId: schemaID,
}
s, err := client.CreateSchema(ctx, schemaID, config)
s, err := client.CreateSchema(ctx, req)
if err != nil {
return fmt.Errorf("CreateSchema: %w", err)
return fmt.Errorf("error calling CreateSchema: %w", err)
}
fmt.Fprintf(w, "Schema created: %#v\n", s)
return nil
Expand Down
19 changes: 12 additions & 7 deletions pubsub/schemas/create_proto_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
"io"
"os"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

// createProtoSchema creates a schema resource from a schema proto file.
Expand All @@ -30,7 +31,7 @@ func createProtoSchema(w io.Writer, projectID, schemaID, protoFile string) error
// schemaID := "my-schema"
// protoFile = "path/to/a/proto/schema/file(.proto)/formatted/in/protocol/buffers"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
Expand All @@ -41,13 +42,17 @@ func createProtoSchema(w io.Writer, projectID, schemaID, protoFile string) error
return fmt.Errorf("error reading from file: %s", protoFile)
}

config := pubsub.SchemaConfig{
Type: pubsub.SchemaProtocolBuffer,
Definition: string(protoSource),
req := &pubsubpb.CreateSchemaRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
Schema: &pubsubpb.Schema{
Type: pubsubpb.Schema_PROTOCOL_BUFFER,
Definition: string(protoSource),
},
SchemaId: schemaID,
}
s, err := client.CreateSchema(ctx, schemaID, config)
s, err := client.CreateSchema(ctx, req)
if err != nil {
return fmt.Errorf("CreateSchema: %w", err)
return fmt.Errorf("error calling CreateSchema: %w", err)
}
fmt.Fprintf(w, "Schema created: %#v\n", s)
return nil
Expand Down
12 changes: 7 additions & 5 deletions pubsub/schemas/create_topic_with_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"fmt"
"io"

"cloud.google.com/go/pubsub"
"cloud.google.com/go/pubsub/v2"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func createTopicWithSchema(w io.Writer, projectID, topicID, schemaID string, encoding pubsub.SchemaEncoding) error {
func createTopicWithSchema(w io.Writer, projectID, topicID, schemaID string, encoding pubsubpb.Encoding) error {
// projectID := "my-project-id"
// topicID := "my-topic"
// schemaID := "my-schema-id"
Expand All @@ -34,13 +35,14 @@ func createTopicWithSchema(w io.Writer, projectID, topicID, schemaID string, enc
return fmt.Errorf("pubsub.NewClient: %w", err)
}

tc := &pubsub.TopicConfig{
SchemaSettings: &pubsub.SchemaSettings{
topic := &pubsubpb.Topic{
Name: fmt.Sprintf("projects/%s/topics/%s", projectID, topicID),
SchemaSettings: &pubsubpb.SchemaSettings{
Schema: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
Encoding: encoding,
},
}
t, err := client.CreateTopicWithConfig(ctx, topicID, tc)
t, err := client.TopicAdminClient.CreateTopic(ctx, topic)
if err != nil {
return fmt.Errorf("CreateTopicWithConfig: %w", err)
}
Expand Down
20 changes: 11 additions & 9 deletions pubsub/schemas/create_topic_with_schema_revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,33 @@ import (
"fmt"
"io"

"cloud.google.com/go/pubsub"
"cloud.google.com/go/pubsub/v2"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func createTopicWithSchemaRevisions(w io.Writer, projectID, topicID, schemaID, firstRevisionID, lastRevisionID string, encoding pubsub.SchemaEncoding) error {
func createTopicWithSchemaRevisions(w io.Writer, projectID, topicID, schemaID, firstRevisionID, lastRevisionID string) error {
// projectID := "my-project-id"
// topicID := "my-topic"
// schemaID := "my-schema-id"
// firstRevisionID := "my-revision-id"
// lastRevisionID := "my-revision-id"
// encoding := pubsub.EncodingJSON
ctx := context.Background()
client, err := pubsub.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("pubsub.NewClient: %w", err)
}

tc := &pubsub.TopicConfig{
SchemaSettings: &pubsub.SchemaSettings{
topic := &pubsubpb.Topic{
Name: fmt.Sprintf("projects/%s/topics/%s", projectID, topicID),
SchemaSettings: &pubsubpb.SchemaSettings{
Schema: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
FirstRevisionID: firstRevisionID,
LastRevisionID: lastRevisionID,
Encoding: encoding,
FirstRevisionId: firstRevisionID,
LastRevisionId: lastRevisionID,
// Alternative encoding is pubsubpb.Encoding_JSON
Encoding: pubsubpb.Encoding_BINARY,
},
}
t, err := client.CreateTopicWithConfig(ctx, topicID, tc)
t, err := client.TopicAdminClient.CreateTopic(ctx, topic)
if err != nil {
return fmt.Errorf("CreateTopicWithConfig: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions pubsub/schemas/delete_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ import (
"fmt"
"io"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func deleteSchema(w io.Writer, projectID, schemaID string) error {
// projectID := "my-project-id"
// schemaID := "my-schema"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
defer client.Close()

if err := client.DeleteSchema(ctx, schemaID); err != nil {
req := &pubsubpb.DeleteSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
}
if err := client.DeleteSchema(ctx, req); err != nil {
return fmt.Errorf("client.DeleteSchema: %w", err)
}
fmt.Fprintf(w, "Deleted schema: %s", schemaID)
Expand Down
12 changes: 8 additions & 4 deletions pubsub/schemas/delete_schema_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ import (
"fmt"
"io"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func deleteSchemaRevision(w io.Writer, projectID, schemaID, revisionID string) error {
// projectID := "my-project-id"
// schemaID := "my-schema-id"
// revisionID := "my-revision-id"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
defer client.Close()

if _, err := client.DeleteSchemaRevision(ctx, schemaID, revisionID); err != nil {
return fmt.Errorf("client.DeleteSchema revision: %w", err)
req := &pubsubpb.DeleteSchemaRevisionRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s@%s", projectID, schemaID, revisionID),
}
if _, err := client.DeleteSchemaRevision(ctx, req); err != nil {
return fmt.Errorf("client.DeleteSchemaRevision: %w", err)
}
fmt.Fprintf(w, "Deleted a schema revision: %s@%s", schemaID, revisionID)
return nil
Expand Down
15 changes: 10 additions & 5 deletions pubsub/schemas/get_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,30 @@ import (
"fmt"
"io"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func getSchema(w io.Writer, projectID, schemaID string) error {
// projectID := "my-project-id"
// schemaID := "my-schema"
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
defer client.Close()

// Retrieve the full schema view. If you don't want to retrieve the
// definition, pass in pubsub.SchemaViewBasic which retrieves
// definition, pass in pubsubpb.SchemaView_BASIC which retrieves
// just the name and type of the schema.
s, err := client.Schema(ctx, schemaID, pubsub.SchemaViewFull)
req := &pubsubpb.GetSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
View: pubsubpb.SchemaView_FULL,
}
s, err := client.GetSchema(ctx, req)
if err != nil {
return fmt.Errorf("client.Schema: %w", err)
return fmt.Errorf("client.GetSchema: %w", err)
}
fmt.Fprintf(w, "Got schema: %#v\n", s)
return nil
Expand Down
15 changes: 10 additions & 5 deletions pubsub/schemas/get_schema_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ import (
"fmt"
"io"

"cloud.google.com/go/pubsub"
pubsub "cloud.google.com/go/pubsub/v2/apiv1"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func getSchemaRevision(w io.Writer, projectID, schemaID string) error {
// projectID := "my-project-id"
// schemaID := "my-schema[@my-schema-revision]"
// schemaID := my-schema@c7cfa2a8 // with revision
ctx := context.Background()
client, err := pubsub.NewSchemaClient(ctx, projectID)
client, err := pubsub.NewSchemaClient(ctx)
if err != nil {
return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
}
defer client.Close()

s, err := client.Schema(ctx, schemaID, pubsub.SchemaViewFull)
req := &pubsubpb.GetSchemaRequest{
Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
View: pubsubpb.SchemaView_FULL,
}
s, err := client.GetSchema(ctx, req)
if err != nil {
return fmt.Errorf("client.Schema revision: %w", err)
return fmt.Errorf("client.GetSchema revision: %w", err)
}
fmt.Fprintf(w, "Got schema revision: %#v\n", s)
return nil
Expand Down
Loading