Sha256: 92b7352cc8858f54d723971443664ea5fec3114b63f802ef7e4f59a390058958

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 KB

Contents

require 'rest-client'

module ForemanRhCloud
  class CloudPingService
    class TokenPing
      include ForemanRhCloud::CloudAuth

      attr_accessor :logger

      def initialize(logger)
        @logger = logger
      end

      def ping
        execute_cloud_request(
          method: :get,
          url: ForemanRhCloud.base_url + "/api/inventory/v1/hosts?per_page=1",
          headers: {
            content_type: :json,
          }
        )
      rescue StandardError => ex
        ex
      end
    end

    class CertPing
      include ForemanRhCloud::CloudRequest
      include InsightsCloud::CandlepinCache

      attr_accessor :logger

      def initialize(org, logger)
        @org = org
        @logger = logger
      end

      def ping
        certs = candlepin_id_cert(@org)
        return StandardError.new('certificate missing') unless certs

        execute_cloud_request(
          method: :get,
          url: ForemanRhCloud.cert_base_url + "/api/apicast-tests/ping",
          headers: {
            content_type: :json,
          },
          ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]),
          ssl_client_key: OpenSSL::PKey::RSA.new(certs[:key])
        )
      rescue StandardError => ex
        ex
      end
    end

    def initialize(organizations, logger)
      @organizations = organizations
      @logger = logger
    end

    def ping
      token_response = TokenPing.new(@logger).ping
      {
        token_auth: {
          success: token_response.is_a?(RestClient::Response),
          error: (token_response.is_a?(Exception) ? token_response.inspect : nil),
        },
        cert_auth: Hash[
          @organizations.map do |org|
            cert_response = CertPing.new(org, @logger).ping
            [
              org,
              {
                success: cert_response.is_a?(RestClient::Response),
                error: (cert_response.is_a?(Exception) ? cert_response.inspect : nil),
              },
            ]
          end
        ],
      }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
foreman_rh_cloud-4.0.31 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-5.0.31 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-5.0.30 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-4.0.30 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-4.0.29 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-5.0.29 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-5.0.28 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-4.0.27 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-4.0.26 app/services/foreman_rh_cloud/cloud_ping_service.rb
foreman_rh_cloud-4.0.25.1 app/services/foreman_rh_cloud/cloud_ping_service.rb