<commit_before>require 'active_support/inflector'
require 'util/miq-exception'

module OpenstackHandle
  class Handle
    attr_accessor :username, :password, :address, :port, :connection_options
    attr_writer   :default_tenant_name

    SERVICE_FALL_BACK = {
      "Network"  => "Compute",
      "Image"    => "Compute",
      "Volume"   => "Compute",
      "Storage"  => nil,
      "Metering" => nil
    }

    SERVICE_NAME_MAP = {
      "Compute"       => :nova,
      "Network"       => :neutron,
      "Image"         => :glance,
      "Volume"        => :cinder,
      "Storage"       => :swift,
      "Metering"      => :ceilometer,
      "Baremetal"     => :baremetal,
      "Orchestration" => :orchestration,
      "Planning"      => :planning
    }

    # Tries both non-SSL and SSL connections to Openstack
    def self.try_connection
      # attempt to connect with SSL
      yield "https", {:ssl_verify_peer => false}
    rescue Excon::Errors::SocketError => err
      # TODO recognizing something in exception message is not very reliable. But somebody would need to go to excon gem
      # and do proper exceptions like Excon::Errors::SocketError::UnknownProtocolSSL,
      # Excon::Errors::SocketError::BadCertificate, etc. all of them inheriting from Excon::Errors::SocketError
      raise unless (err.message.include?("end of file reached (EOFError)") ||
                    err.message.include?("unknown protocol (OpenSSL::SSL::SSLError)"))
      # attempt the same connection without SSL
      yield "http", {}
    end

    def self.raw_connect_try_ssl(username, password, address, port, service = "Compute", opts = nil)
      try_connection do |scheme, connection_options|
        auth_url = auth_url(address, port, scheme)
        opts[:connection_options] = (opts[:connection_options] || {}).merge(connection_options)
        raw_connect(username, password, auth_url, service, opts)
      end
    end

    def self.raw_connect(username, password, auth_url, service = "Compute", extra_opts = nil)
      opts = {
        :provider           => 'OpenStack',
        :openstack_auth_url => auth_url,
        :openstack_username => username,
        :openstack_api_key  => password,
      }
      opts.merge!(extra_opts) if extra_opts

      # Workaround for a bug in Fog
      # https://github.com/fog/fog/issues/3112
      # Ensure the that if the Storage service is not available, it will not
      # throw an error trying to build an connection error message.
      opts[:openstack_service_type] = ["object-store"] if service == "Storage"

      if service == "Planning"
        # Special behaviour for Planning service Tuskar, since it is OpenStack specific service, there is no
        # Fog::Planning module, only Fog::OpenStack::Planning
        Fog::Openstack.const_get(service).new(opts)
      else
        Fog.const_get(service).new(opts)
      end
    rescue ArgumentError, Fog::Errors::NotFound => err
      raise MiqException::ServiceNotAvailable if err.message.include?("Could not find service")
      raise MiqException::ServiceNotAvailable if err.message =~ /\w+ has no \w+ service/
      raise
    end

    def self.auth_url(address, port = 5000, scheme = "http")
      url(address, port, scheme)
    end

    def self.url(address, port = 5000, scheme = "http", path = "/v2.0/tokens")<commit_msg>Revert "Handle Fog network service not available error."<commit_after>require 'active_support/inflector'
require 'util/miq-exception'

module OpenstackHandle
  class Handle
    attr_accessor :username, :password, :address, :port, :connection_options
    attr_writer   :default_tenant_name

    SERVICE_FALL_BACK = {
      "Network"  => "Compute",
      "Image"    => "Compute",
      "Volume"   => "Compute",
      "Storage"  => nil,
      "Metering" => nil
    }

    SERVICE_NAME_MAP = {
      "Compute"       => :nova,
      "Network"       => :neutron,
      "Image"         => :glance,
      "Volume"        => :cinder,
      "Storage"       => :swift,
      "Metering"      => :ceilometer,
      "Baremetal"     => :baremetal,
      "Orchestration" => :orchestration,
      "Planning"      => :planning
    }

    # Tries both non-SSL and SSL connections to Openstack
    def self.try_connection
      # attempt to connect with SSL
      yield "https", {:ssl_verify_peer => false}
    rescue Excon::Errors::SocketError => err
      # TODO recognizing something in exception message is not very reliable. But somebody would need to go to excon gem
      # and do proper exceptions like Excon::Errors::SocketError::UnknownProtocolSSL,
      # Excon::Errors::SocketError::BadCertificate, etc. all of them inheriting from Excon::Errors::SocketError
      raise unless (err.message.include?("end of file reached (EOFError)") ||
                    err.message.include?("unknown protocol (OpenSSL::SSL::SSLError)"))
      # attempt the same connection without SSL
      yield "http", {}
    end

    def self.raw_connect_try_ssl(username, password, address, port, service = "Compute", opts = nil)
      try_connection do |scheme, connection_options|
        auth_url = auth_url(address, port, scheme)
        opts[:connection_options] = (opts[:connection_options] || {}).merge(connection_options)
        raw_connect(username, password, auth_url, service, opts)
      end
    end

    def self.raw_connect(username, password, auth_url, service = "Compute", extra_opts = nil)
      opts = {
        :provider           => 'OpenStack',
        :openstack_auth_url => auth_url,
        :openstack_username => username,
        :openstack_api_key  => password,
      }
      opts.merge!(extra_opts) if extra_opts

      # Workaround for a bug in Fog
      # https://github.com/fog/fog/issues/3112
      # Ensure the that if the Storage service is not available, it will not
      # throw an error trying to build an connection error message.
      opts[:openstack_service_type] = ["object-store"] if service == "Storage"

      if service == "Planning"
        # Special behaviour for Planning service Tuskar, since it is OpenStack specific service, there is no
        # Fog::Planning module, only Fog::OpenStack::Planning
        Fog::Openstack.const_get(service).new(opts)
      else
        Fog.const_get(service).new(opts)
      end
    rescue Fog::Errors::NotFound => err
      raise MiqException::ServiceNotAvailable if err.message.include?("Could not find service")
      raise
    end

    def self.auth_url(address, port = 5000, scheme = "http")
      url(address, port, scheme)
    end

    def self.url(address, port = 5000, scheme = "http", path = "/v2.0/tokens")
{'fraction_non_alphanumeric': 0.11127933383800151, 'fraction_numerical': 0.004239212717638153, 'mean_word_length': 2.869947275922671, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6947099', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1913, 'n_words': 532}