Skip to content

Commit

Permalink
chore(pubsub): Add commit_proto_schema to samples
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation committed Feb 27, 2025
1 parent 8b174a8 commit 4bb6ef0
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 12 deletions.
43 changes: 43 additions & 0 deletions google-cloud-pubsub/lib/google/cloud/pubsub/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ def definition
@grpc.definition if @grpc.definition && !@grpc.definition.empty?
end

##
# The revision ID of the schema.
#
# @return [String] The revision id.
#
def revision_id
return nil if reference?
@grpc.revision_id if @grpc.revision_id && !@grpc.revision_id.empty?
end

##
# Validates a message against a schema.
#
Expand Down Expand Up @@ -197,6 +207,39 @@ def exists?
@exists = false
end

##
# Commits a new schema revision to an existing schema.
#
# @param definition [String] 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`. See
# https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas#Schema
# for details.
# @param type [String, Symbol] The type of the schema. Possible values are
# case-insensitive and include:
#
# * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
# * `AVRO` - An Avro schema definition.
#
# @return [Google::Cloud::PubSub::Schema]
#
# @example
# require "google/cloud/pubsub"
#
# pubsub = Google::Cloud::PubSub.new
#
# schema = pubsub.schema "my-schema"
#
# definition = File.read /path/to/file
#
# revision_schema = schema.commit definition, type: :protocol_buffer
#
def commit definition, type
type = type.to_s.upcase
grpc = service.commit_schema name, definition, type
Schema.from_grpc grpc, service, view: @view
end

##
# Determines whether the schema object was created without retrieving the
# resource representation from the Pub/Sub service.
Expand Down
25 changes: 25 additions & 0 deletions google-cloud-pubsub/lib/google/cloud/pubsub/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,31 @@ def delete_schema schema_name
schemas.delete_schema name: schema_path(schema_name)
end

##
# Commits a new schema revision to an existing schema.
#
# @param name [String] The name of the schema to revision.
# @param definition [String] 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`. See
# https://cloud.google.com/pubsub/docs/schemas for details.
# @param type [String, Symbol] The type of the schema. Required. Possible
# values are case-insensitive and include:
#
# * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
# * `AVRO` - An Avro schema definition.
#
# @return [Google::Cloud::PubSub::V1::Schema]
#
def commit_schema name, definition, type
schema = Google::Cloud::PubSub::V1::Schema.new(
name: name,
definition: definition,
type: type
)
schemas.commit_schema name: name, schema: schema
end

##
# Validate the definition string intended for a schema.
def validate_schema type, definition, options = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package utilities;

message StateProto {
string name = 1;
string post_abbr = 2;
string capital_city = 3;
}
41 changes: 29 additions & 12 deletions google-cloud-pubsub/samples/acceptance/schemas_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@
# limitations under the License.

require_relative "helper"
require_relative "../pubsub_create_avro_schema.rb"
require_relative "../pubsub_create_topic_with_schema.rb"
require_relative "../pubsub_create_proto_schema.rb"
require_relative "../pubsub_delete_schema.rb"
require_relative "../pubsub_get_schema.rb"
require_relative "../pubsub_list_schemas.rb"
require_relative "../pubsub_publish_avro_records.rb"
require_relative "../pubsub_subscribe_avro_records.rb"
require_relative "../pubsub_publish_proto_messages.rb"
require_relative "../pubsub_subscribe_proto_messages.rb"
require_relative "../pubsub_commit_proto_schema"
require_relative "../pubsub_create_avro_schema"
require_relative "../pubsub_create_topic_with_schema"
require_relative "../pubsub_create_proto_schema"
require_relative "../pubsub_delete_schema"
require_relative "../pubsub_get_schema"
require_relative "../pubsub_list_schemas"
require_relative "../pubsub_publish_avro_records"
require_relative "../pubsub_subscribe_avro_records"
require_relative "../pubsub_publish_proto_messages"
require_relative "../pubsub_subscribe_proto_messages"


describe "schemas" do
let(:pubsub) { Google::Cloud::Pubsub.new }
let(:schema_id) { random_schema_id }
let(:topic_id) { random_topic_id }
let(:subscription_id) { random_subscription_id }
let(:avsc_file) { File.expand_path("data/us-states.avsc", __dir__) }
let(:avsc_file) { File.expand_path "data/us-states.avsc", __dir__ }

after do
@subscription.delete if @subscription
Expand Down Expand Up @@ -146,7 +147,7 @@

describe "PROTOCOL_BUFFER" do
require_relative "../utilities/us-states_pb"
let(:proto_file) { File.expand_path("data/us-states.proto", __dir__) }
let(:proto_file) { File.expand_path "data/us-states.proto", __dir__ }
let(:proto_definition) { File.read proto_file }

it "supports pubsub_create_topic_with_schema, pubsub_publish_proto_messages with binary encoding" do
Expand Down Expand Up @@ -218,5 +219,21 @@
end
end
end

it "supports pubsub_commit_proto_schema" do
@schema = pubsub.create_schema schema_id, :protocol_buffer, proto_definition
definition_file = File.expand_path "data/us-states-revision.proto", __dir__
definition = File.read definition_file

revision_schema = @schema.commit definition, :protocol_buffer

# pubsub_commit_proto_schema
expect_with_retry "pubsub_commit_proto_schema" do
assert_output "Schema projects/#{pubsub.project}/schemas/#{schema_id} commited with " \
"revision #{revision_schema.revision_id}.\n" do
commit_proto_schema schema_id: schema_id, proto_file: definition
end
end
end
end
end
31 changes: 31 additions & 0 deletions google-cloud-pubsub/samples/pubsub_commit_proto_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2025 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.

require "google/cloud/pubsub"

def commit_proto_schema schema_id:, proto_file:
# [START pubsub_commit_proto_schema]
# schema_id = "your-schema-id"
# proto_file = "path/to/a/proto/file/(.proto)/formatted/in/protocol/buffers"

pubsub = Google::Cloud::Pubsub.new
schema = pubsub.schema schema_id

definition = File.read proto_file

result = schema.commit definition, :protocol_buffer

puts "Schema #{result.name} commited with revision #{result.revision_id}."
# [END pubsub_commit_proto_schema]
end

0 comments on commit 4bb6ef0

Please # to comment.