diff --git a/google-cloud-pubsub/lib/google/cloud/pubsub/schema.rb b/google-cloud-pubsub/lib/google/cloud/pubsub/schema.rb index 19a55c2b92d9..2e890adc7da8 100644 --- a/google-cloud-pubsub/lib/google/cloud/pubsub/schema.rb +++ b/google-cloud-pubsub/lib/google/cloud/pubsub/schema.rb @@ -197,6 +197,29 @@ def exists? @exists = false end + ## + # Lists all schema revisions for the named schema. + # + # @param view [Symbol, String, nil] The set of fields to return in the response. Possible values: + # * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`. + # * `FULL` - Include all Schema object fields. + # + # @return [Google::Cloud::PubSub::V1::ListSchemaRevisionsResponse] + # + # @example + # require "google/cloud/pubsub" + # + # pubsub = Google::Cloud::PubSub.new + # + # schema = pubsub.schema "my-schema" + # schema.list_revisions + # + def list_revisions options = {}, view: nil + service.list_schema_revisions name: name, + view: view || @view, + options: options + end + ## # Determines whether the schema object was created without retrieving the # resource representation from the Pub/Sub service. diff --git a/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb b/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb index d38b6a9dd2e5..5d2c3b3b3931 100644 --- a/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb +++ b/google-cloud-pubsub/lib/google/cloud/pubsub/service.rb @@ -341,6 +341,21 @@ def list_schemas view, options = {} paged_enum.response end + ## + # Lists all schema revisions for the named schema. + # @param name [String] The name of the schema to list revisions for. + # @param view [String, Symbol, nil] Possible values: + # * `BASIC` - Include the name and type of the schema, but not the definition. + # * `FULL` - Include all Schema object fields. + # + def list_schema_revisions name, view, options = {} + schema_view = Google::Cloud::PubSub::V1::SchemaView.const_get view.to_s.upcase + schemas.list_schema_revisions name: name, + view: schema_view, + page_size: options[:max], + page_token: options[:token] + end + ## # Creates a schema in the current (or given) project. def create_schema schema_id, type, definition, options = {} diff --git a/google-cloud-pubsub/samples/acceptance/schemas_test.rb b/google-cloud-pubsub/samples/acceptance/schemas_test.rb index 1771caa2f654..cab4f3133595 100644 --- a/google-cloud-pubsub/samples/acceptance/schemas_test.rb +++ b/google-cloud-pubsub/samples/acceptance/schemas_test.rb @@ -13,16 +13,17 @@ # 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_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_schema_revisions" +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 @@ -38,7 +39,8 @@ @schema.delete if @schema end - it "supports pubsub_create_schema, pubsub_get_schema, pubsub_list_schemas, pubsub_delete_schema" do + it "supports pubsub_create_schema, pubsub_get_schema, pubsub_List_schema_revisions, " \ + "pubsub_list_schemas, pubsub_delete_schema" do # create_avro_schema assert_output "Schema projects/#{pubsub.project}/schemas/#{schema_id} created.\n" do create_avro_schema schema_id: schema_id, avsc_file: avsc_file @@ -52,6 +54,12 @@ get_schema schema_id: schema_id end + # pubsub_list_schema_revisions + out, _err = capture_io do + list_schema_revisions + end + assert_includes out, "Listed revisions of schema" + # pubsub_list_schemas out, _err = capture_io do list_schemas diff --git a/google-cloud-pubsub/samples/pubsub_list_schema_revisions.rb b/google-cloud-pubsub/samples/pubsub_list_schema_revisions.rb new file mode 100644 index 000000000000..4ba636969c44 --- /dev/null +++ b/google-cloud-pubsub/samples/pubsub_list_schema_revisions.rb @@ -0,0 +1,32 @@ +# 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 list_schema_revisions + # [START pubsub_list_schema_revisions] + # schema_id = "your-schema-id" + + pubsub = Google::Cloud::Pubsub.new + + schema = pubsub.schema schema_id + + response = schema.list_revisions + + puts "Listed revisions of schema #{schema_id}" + response.schemas.each do |revision| + puts revision.revision_id + end + # [END pubsub_list_schema_revisions] +end