diff --git a/google-cloud-run-client/lib/google/cloud/run.rb b/google-cloud-run-client/lib/google/cloud/run.rb index fb43d8f5c763..d516f7342b1e 100644 --- a/google-cloud-run-client/lib/google/cloud/run.rb +++ b/google-cloud-run-client/lib/google/cloud/run.rb @@ -185,6 +185,76 @@ def self.executions_available? version: :v2, transport: :grpc false end + ## + # Create a new client object for Instances. + # + # By default, this returns an instance of + # [Google::Cloud::Run::V2::Instances::Client](https://cloud.google.com/ruby/docs/reference/google-cloud-run-v2/latest/Google-Cloud-Run-V2-Instances-Client) + # for a gRPC client for version V2 of the API. + # However, you can specify a different API version by passing it in the + # `version` parameter. If the Instances 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 Instances service. + # You can determine whether the method will succeed by calling + # {Google::Cloud::Run.instances_available?}. + # + # ## About Instances + # + # The Cloud Run Instances API allows you to manage Cloud Run Instances. + # + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v2`. + # @param transport [:grpc, :rest] The transport to use. Defaults to `:grpc`. + # @return [::Object] A client object for the specified version. + # + def self.instances version: :v2, transport: :grpc, &block + require "google/cloud/run/#{version.to_s.downcase}" + + package_name = Google::Cloud::Run + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + service_module = Google::Cloud::Run.const_get(package_name).const_get(:Instances) + service_module = service_module.const_get(:Rest) if transport == :rest + service_module.const_get(:Client).new(&block) + end + + ## + # Determines whether the Instances service is supported by the current client. + # If true, you can retrieve a client object by calling {Google::Cloud::Run.instances}. + # If false, that method will raise an exception. This could happen if the given + # API version does not exist or does not support the Instances service, + # or if the versioned client gem needs an update to support the Instances service. + # + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v2`. + # @param transport [:grpc, :rest] The transport to use. Defaults to `:grpc`. + # @return [boolean] Whether the service is available. + # + def self.instances_available? version: :v2, transport: :grpc + require "google/cloud/run/#{version.to_s.downcase}" + package_name = Google::Cloud::Run + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + return false unless package_name + service_module = Google::Cloud::Run.const_get package_name + return false unless service_module.const_defined? :Instances + service_module = service_module.const_get :Instances + 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 + ## # Create a new client object for Jobs. # diff --git a/google-cloud-run-client/test/google/cloud/run/client_test.rb b/google-cloud-run-client/test/google/cloud/run/client_test.rb index c2abb973be68..586bd3a6422c 100644 --- a/google-cloud-run-client/test/google/cloud/run/client_test.rb +++ b/google-cloud-run-client/test/google/cloud/run/client_test.rb @@ -83,6 +83,27 @@ def test_executions_rest end end + def test_instances_grpc + skip unless Google::Cloud::Run.instances_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::Run.instances transport: :grpc do |config| + config.credentials = grpc_channel + end + assert_kind_of Google::Cloud::Run::V2::Instances::Client, client + end + end + + def test_instances_rest + skip unless Google::Cloud::Run.instances_available? transport: :rest + Gapic::Rest::ClientStub.stub :new, DummyStub.new do + client = Google::Cloud::Run.instances transport: :rest do |config| + config.credentials = :dummy_credentials + end + assert_kind_of Google::Cloud::Run::V2::Instances::Rest::Client, client + end + end + def test_jobs_grpc skip unless Google::Cloud::Run.jobs_available? transport: :grpc Gapic::ServiceStub.stub :new, DummyStub.new do