diff --git a/google-cloud-backupdr/lib/google/cloud/backupdr.rb b/google-cloud-backupdr/lib/google/cloud/backupdr.rb index cc5df431bd65..e09f50f7ebe3 100644 --- a/google-cloud-backupdr/lib/google/cloud/backupdr.rb +++ b/google-cloud-backupdr/lib/google/cloud/backupdr.rb @@ -115,6 +115,76 @@ def self.backupdr_available? version: :v1, transport: :grpc false end + ## + # Create a new client object for BackupDrProtectionSummary. + # + # By default, this returns an instance of + # [Google::Cloud::BackupDR::V1::BackupDrProtectionSummary::Client](https://cloud.google.com/ruby/docs/reference/google-cloud-backupdr-v1/latest/Google-Cloud-BackupDR-V1-BackupDrProtectionSummary-Client) + # for a gRPC client for version V1 of the API. + # However, you can specify a different API version by passing it in the + # `version` parameter. If the BackupDrProtectionSummary service is + # supported by that API version, and the corresponding gem is available, the + # appropriate versioned client will be returned. + # You can also specify a different transport by passing `:rest` or `:grpc` in + # the `transport` parameter. + # + # Raises an exception if the currently installed versioned client gem for the + # given API version does not support the given transport of the BackupDrProtectionSummary service. + # You can determine whether the method will succeed by calling + # {Google::Cloud::BackupDR.backup_dr_protection_summary_available?}. + # + # ## About BackupDrProtectionSummary + # + # The Protection Summary service. + # + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v1`. + # @param transport [:grpc, :rest] The transport to use. Defaults to `:grpc`. + # @return [::Object] A client object for the specified version. + # + def self.backup_dr_protection_summary version: :v1, transport: :grpc, &block + require "google/cloud/backupdr/#{version.to_s.downcase}" + + package_name = Google::Cloud::BackupDR + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + service_module = Google::Cloud::BackupDR.const_get(package_name).const_get(:BackupDrProtectionSummary) + service_module = service_module.const_get(:Rest) if transport == :rest + service_module.const_get(:Client).new(&block) + end + + ## + # Determines whether the BackupDrProtectionSummary service is supported by the current client. + # If true, you can retrieve a client object by calling {Google::Cloud::BackupDR.backup_dr_protection_summary}. + # If false, that method will raise an exception. This could happen if the given + # API version does not exist or does not support the BackupDrProtectionSummary service, + # or if the versioned client gem needs an update to support the BackupDrProtectionSummary service. + # + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v1`. + # @param transport [:grpc, :rest] The transport to use. Defaults to `:grpc`. + # @return [boolean] Whether the service is available. + # + def self.backup_dr_protection_summary_available? version: :v1, transport: :grpc + require "google/cloud/backupdr/#{version.to_s.downcase}" + package_name = Google::Cloud::BackupDR + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + return false unless package_name + service_module = Google::Cloud::BackupDR.const_get package_name + return false unless service_module.const_defined? :BackupDrProtectionSummary + service_module = service_module.const_get :BackupDrProtectionSummary + if transport == :rest + return false unless service_module.const_defined? :Rest + service_module = service_module.const_get :Rest + end + service_module.const_defined? :Client + rescue ::LoadError + false + end + ## # Configure the google-cloud-backupdr library. # diff --git a/google-cloud-backupdr/test/google/cloud/backupdr/client_test.rb b/google-cloud-backupdr/test/google/cloud/backupdr/client_test.rb index c731a1b9e0dd..12df7de42258 100644 --- a/google-cloud-backupdr/test/google/cloud/backupdr/client_test.rb +++ b/google-cloud-backupdr/test/google/cloud/backupdr/client_test.rb @@ -61,4 +61,25 @@ def test_backupdr_rest assert_kind_of Google::Cloud::BackupDR::V1::BackupDR::Rest::Client, client end end + + def test_backup_dr_protection_summary_grpc + skip unless Google::Cloud::BackupDR.backup_dr_protection_summary_available? transport: :grpc + Gapic::ServiceStub.stub :new, DummyStub.new do + grpc_channel = GRPC::Core::Channel.new "localhost:8888", nil, :this_channel_is_insecure + client = Google::Cloud::BackupDR.backup_dr_protection_summary transport: :grpc do |config| + config.credentials = grpc_channel + end + assert_kind_of Google::Cloud::BackupDR::V1::BackupDrProtectionSummary::Client, client + end + end + + def test_backup_dr_protection_summary_rest + skip unless Google::Cloud::BackupDR.backup_dr_protection_summary_available? transport: :rest + Gapic::Rest::ClientStub.stub :new, DummyStub.new do + client = Google::Cloud::BackupDR.backup_dr_protection_summary transport: :rest do |config| + config.credentials = :dummy_credentials + end + assert_kind_of Google::Cloud::BackupDR::V1::BackupDrProtectionSummary::Rest::Client, client + end + end end