<commit_before>require_relative './core'
require_relative './identity/authenticator'

module Fog
  module Identity
    class OpenStackCommon < Fog::Service

      requires :openstack_auth_url
      recognizes :openstack_auth_token, :openstack_management_url, :persistent,
                  :openstack_service_type, :openstack_service_name, :openstack_tenant,
                  :openstack_api_key, :openstack_username, :openstack_current_user_id,
                  :openstack_endpoint_type,
                  :current_user, :current_tenant

      model_path 'fog/openstackcommon/models/identity'
      model       :tenant
      collection  :tenants
      model       :user
      collection  :users
      model       :role
      collection  :roles
      model       :ec2_credential
      collection  :ec2_credentials

      request_path 'fog/openstackcommon/requests/identity'

      # Administrative API Operations ----------------------------

      ## Token Operations
      # request :generate_token                 # cannot find
      request :validate_token
      request :check_token
      request :list_endpoints_for_token         # HEAD in request, GET in doc

      ## User Operations
      request :get_user_by_name
      request :get_user_by_id
      request :list_user_global_roles

      ## Tenant Operations
      request :list_tenants
      request :get_tenants_by_name              # differs
      request :get_tenants_by_id
      request :list_roles_for_user_on_tenant

      # Openstack Identity Service Extensions --------------------

      ## User Operations
      # request :list_users                     # Plain 'ol list users missing
      request :create_user
      request :update_user
      request :delete_user
      # request :enable_user                    # missing
      # request :list_user_global_roles         # close to :list_user_global_roles
      # request :add_global_role_to_user        # missing
      # request :delete_global_role_from_user   # missing
      # request :add_user_credentials           # missing
      # request :list_credentials               # missing
      # request :update_user_credentials        # missing
      # request :delete_user_credentials        # missing
      # request :get_user_credentials           # missing

      ## Tenant Operations
      request :create_tenant
      request :update_tenant
      request :delete_tenant
      request :list_users                       # missing limits
      request :get_tenant
      request :delete_user_role                 # DUP -> :remove_user_from_tenant
      request :create_user_role                 # DUP -> :add_user_to_tenant

      ## Role Operations
      request :get_role_by_name                 # missing
      request :create_role                      # differs
      request :get_role
      request :delete_role
      request :list_roles                       # not in the docs

      ## Service Operations
      # request :list_services
      # request :add_service
      # request :get_service
      # request :delete_service


      # OS-KSEC2 Admin Extension ----------------------------------

      ## User Operations
      request :create_ec2_credential            # differs
      request :list_ec2_credentials             # differs
      # request :update_credentials             # missing
      request :delete_ec2_credential            # differs
      request :get_ec2_credential               # differs




      request :set_tenant




      # minimal requirement
      class Mock
      end

      class Real
        attr_reader :current_user, :current_tenant, :unscoped_token
        attr_reader :auth_token

        def initialize(options={})
          # puts "===== Fog::Identity::OpenStackCommon -> initialize ====="
          apply_options(options)
          authenticate
          connection_url = "#{@scheme}://#{@host}:#{@port}"
          @connection = Fog::Core::Connection.new(connection_url, @persistent, @connection_options)
        end

        def reload
          # puts "===== Fog::Identity::OpenStackCommon -> reload ====="
          @connection.reset
        end

        def request(params)
          # puts "===== Fog::Identity::OpenStackCommon -> request ====="
          retried = false
          begin
            response = @connection.request(params.merge({
              :headers  => {
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
                'X-Auth-Token' => @auth_token
              }.merge!(params[:headers] || {}),
              :path     => "#{@path}#{params[:path]}"#,
            }))
          rescue Excon::Errors::Unauthorized => error
            raise if retried
            retried = true

            @openstack_must_reauthenticate = true
            authenticate
            retry
          rescue Excon::Errors::HTTPStatusError => error
            raise case error
            when Excon::Errors::NotFound
              Fog::Identity::OpenStackCommon::NotFound.slurp(error)
            else
              error
            end
          end
          unless response.body.empty?
            response.body = MultiJson.decode(response.body)
          end
          response
        end

        private

        def authenticate
          # puts "===== Fog::Identity::OpenStackCommon -> authenticate ====="
          if !@openstack_management_url || @openstack_must_reauthenticate
            case @openstack_auth_uri.path
            when /v1(\.\d+)?/
              Fog::OpenStackCommon::Authenticator.adapter = :authenticator_v1
            else
              Fog::OpenStackCommon::Authenticator.adapter = :authenticator_v2
            end

            options = init_auth_options
            credentials = Fog::OpenStackCommon::Authenticator.adapter.authenticate(options, @connection_options)
            handle_auth_results(credentials)
          else
            @auth_token = @openstack_auth_token
          end

          save_host_attributes
          credentials
        end

        def apply_options(options)

          @openstack_auth_token = options[:openstack_auth_token]
          # puts "openstack_auth_token: #{@openstack_auth_token}"

          unless @openstack_auth_token
            set_credentials(options)
            validate_credentials
          end

          @openstack_tenant   = options[:openstack_tenant]
          # puts "@openstack_tenant: #{@openstack_tenant}"

          @openstack_auth_url = options[:openstack_auth_url]
          @openstack_auth_uri = URI.parse(@openstack_auth_url)
          # puts "@openstack_auth_url: #{@openstack_auth_url}"
          # puts "@openstack_auth_uri: #{@openstack_auth_uri.to_yaml}"

          @openstack_management_url       = options[:openstack_management_url]
          # puts "@openstack_management_url: #{@openstack_management_url}"

          @openstack_must_reauthenticate  = false
          # puts "@openstack_must_reauthenticate: #{@openstack_must_reauthenticate}"

          @openstack_service_type = options[:openstack_service_type] || ['identity']
          # puts "@openstack_service_type: #{@openstack_service_type}"

          @openstack_service_name = options[:openstack_service_name]
          # puts "@openstack_service_name: #{@openstack_service_name}"

          @openstack_current_user_id = options[:openstack_current_user_id]
          # puts "@openstack_current_user_id: #{@openstack_current_user_id}"

          @openstack_endpoint_type = options[:openstack_endpoint_type] || 'adminURL'
          # puts "@openstack_endpoint_type: #{@openstack_endpoint_type}"

          @current_user = options[:current_user]
          # puts "@current_user: #{@current_user}"

          @current_tenant = options[:current_tenant]
          # puts "@current_tenant: #{@current_tenant}"

          @connection_options = options[:connection_options] || {}
          # puts "@connection_options: #{@connection_options}"

          @persistent = options[:persistent] || false
          # puts "@persistent: #{@persistent}"
        end

        def init_auth_options
          { :openstack_api_key  => @openstack_api_key,
            :openstack_username => @openstack_username,
            :openstack_auth_token => @openstack_auth_token,
            :openstack_auth_uri => @openstack_auth_uri,
            :openstack_tenant   => @openstack_tenant,
            :openstack_service_type => @openstack_service_type,
            :openstack_service_name => @openstack_service_name,
            :openstack_endpoint_type => @openstack_endpoint_type
          }
        end

        def handle_auth_results(credentials={})
          @current_user = credentials[:user]
          @current_tenant = credentials[:tenant]
          @openstack_must_reauthenticate = false
          @auth_token = credentials[:token]
          @openstack_management_url = credentials[:server_management_url]
          @openstack_current_user_id = credentials[:current_user_id]
          @unscoped_token = credentials[:unscoped_token]
        end

        def save_host_attributes
          uri = URI.parse(@openstack_management_url)
          @host   = uri.host
          @path   = uri.path
          @path.sub!(/\/$/, '')
          @port   = uri.port
          @scheme = uri.scheme
          puts "HOST: #{@host}"
          puts "path: #{@path}"
          puts "port: #{@port}"
          puts "scheme: #{@scheme}"
        end

        def set_credentials(options={})
          @openstack_api_key  = options[:openstack_api_key]
          @openstack_username = options[:openstack_username]
        end

        def validate_credentials
          missing_credentials = Array.new
          missing_credentials << :openstack_api_key  unless @openstack_api_key
          missing_credentials << :openstack_username unless @openstack_username
          if !missing_credentials.empty?
            raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}"
          end
        end

      end

    end<commit_msg>rename base path; adjust docs<commit_after>require_relative './core'
require_relative './identity/authenticator'

module Fog
  module Identity
    class OpenStackCommon < Fog::Service

      requires :openstack_auth_url
      recognizes :openstack_auth_token, :openstack_management_url, :persistent,
                  :openstack_service_type, :openstack_service_name, :openstack_tenant,
                  :openstack_api_key, :openstack_username, :openstack_current_user_id,
                  :openstack_endpoint_type,
                  :current_user, :current_tenant

      model_path 'fog/openstackcommon/models/identity'
      model       :tenant
      collection  :tenants
      model       :user
      collection  :users
      model       :role
      collection  :roles
      model       :ec2_credential
      collection  :ec2_credentials

      request_path 'fog/openstackcommon/requests/identity'

      # Administrative API Operations ----------------------------

      ## Token Operations

      # ToDo:
      # this might be the request_tokens method in the authenticator_v2
      # class... if so, we should extract into a request class like the
      # others.
      # request :generate_token                 # cannot find
      request :validate_token
      request :check_token
      request :list_endpoints_for_token         # HEAD in request, GET in doc

      ## User Operations
      request :get_user_by_name
      request :get_user_by_id
      request :list_user_global_roles

      ## Tenant Operations
      request :list_tenants
      request :get_tenants_by_name              # differs
      request :get_tenants_by_id
      request :list_roles_for_user_on_tenant

      # Openstack Identity Service Extensions --------------------

      ## User Operations
      # request :list_users                     # Plain 'ol list users missing
      request :create_user
      request :update_user
      request :delete_user
      # request :enable_user                    # missing
      # request :list_user_global_roles         # close to :list_user_global_roles
      # request :add_global_role_to_user        # missing
      # request :delete_global_role_from_user   # missing
      # request :add_user_credentials           # missing
      # request :list_credentials               # missing
      # request :update_user_credentials        # missing
      # request :delete_user_credentials        # missing
      # request :get_user_credentials           # missing

      ## Tenant Operations
      request :create_tenant
      request :update_tenant
      request :delete_tenant
      request :list_users                       # missing limits
      request :get_tenant
      request :delete_user_role                 # DUP -> :remove_user_from_tenant
      request :create_user_role                 # DUP -> :add_user_to_tenant

      ## Role Operations
      request :get_role_by_name                 # missing
      request :create_role                      # differs
      request :get_role
      request :delete_role
      request :list_roles                       # not in the docs

      ## Service Operations
      # request :list_services
      # request :add_service
      # request :get_service
      # request :delete_service


      # OS-KSEC2 Admin Extension ----------------------------------

      ## User Operations
      request :create_ec2_credential            # differs
      request :list_ec2_credentials             # differs
      # request :update_credentials             # missing
      request :delete_ec2_credential            # differs
      request :get_ec2_credential               # differs


      request :set_tenant                       # not in API, unsure of purpose


      # minimal requirement
      class Mock
      end

      class Real
        attr_reader :current_user, :current_tenant, :unscoped_token
        attr_reader :auth_token

        def initialize(options={})
          # puts "===== Fog::Identity::OpenStackCommon -> initialize ====="
          apply_options(options)
          authenticate
          connection_url = "#{@scheme}://#{@host}:#{@port}"
          @connection = Fog::Core::Connection.new(connection_url, @persistent, @connection_options)
        end

        def reload
          # puts "===== Fog::Identity::OpenStackCommon -> reload ====="
          @connection.reset
        end

        def request(params)
          # puts "===== Fog::Identity::OpenStackCommon -> request ====="
          retried = false
          begin
            response = @connection.request(params.merge({
              :headers  => {
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
                'X-Auth-Token' => @auth_token
              }.merge!(params[:headers] || {}),
              :path     => "#{@base_path}#{params[:path]}"#,
            }))
          rescue Excon::Errors::Unauthorized => error
            raise if retried
            retried = true

            @openstack_must_reauthenticate = true
            authenticate
            retry
          rescue Excon::Errors::HTTPStatusError => error
            raise case error
            when Excon::Errors::NotFound
              Fog::Identity::OpenStackCommon::NotFound.slurp(error)
            else
              error
            end
          end
          unless response.body.empty?
            response.body = MultiJson.decode(response.body)
          end
          response
        end

        private

        def authenticate
          # puts "===== Fog::Identity::OpenStackCommon -> authenticate ====="
          if !@openstack_management_url || @openstack_must_reauthenticate
            case @openstack_auth_uri.path
            when /v1(\.\d+)?/
              Fog::OpenStackCommon::Authenticator.adapter = :authenticator_v1
            else
              Fog::OpenStackCommon::Authenticator.adapter = :authenticator_v2
            end

            options = init_auth_options
            credentials = Fog::OpenStackCommon::Authenticator.adapter.authenticate(options, @connection_options)
            handle_auth_results(credentials)
          else
            @auth_token = @openstack_auth_token
          end

          save_host_attributes
          credentials
        end

        def apply_options(options)

          @openstack_auth_token = options[:openstack_auth_token]
          # puts "openstack_auth_token: #{@openstack_auth_token}"

          unless @openstack_auth_token
            set_credentials(options)
            validate_credentials
          end

          @openstack_tenant   = options[:openstack_tenant]
          # puts "@openstack_tenant: #{@openstack_tenant}"

          @openstack_auth_url = options[:openstack_auth_url]
          @openstack_auth_uri = URI.parse(@openstack_auth_url)
          # puts "@openstack_auth_url: #{@openstack_auth_url}"
          # puts "@openstack_auth_uri: #{@openstack_auth_uri.to_yaml}"

          @openstack_management_url       = options[:openstack_management_url]
          # puts "@openstack_management_url: #{@openstack_management_url}"

          @openstack_must_reauthenticate  = false
          # puts "@openstack_must_reauthenticate: #{@openstack_must_reauthenticate}"

          @openstack_service_type = options[:openstack_service_type] || ['identity']
          # puts "@openstack_service_type: #{@openstack_service_type}"

          @openstack_service_name = options[:openstack_service_name]
          # puts "@openstack_service_name: #{@openstack_service_name}"

          @openstack_current_user_id = options[:openstack_current_user_id]
          # puts "@openstack_current_user_id: #{@openstack_current_user_id}"

          @openstack_endpoint_type = options[:openstack_endpoint_type] || 'adminURL'
          # puts "@openstack_endpoint_type: #{@openstack_endpoint_type}"

          @current_user = options[:current_user]
          # puts "@current_user: #{@current_user}"

          @current_tenant = options[:current_tenant]
          # puts "@current_tenant: #{@current_tenant}"

          @connection_options = options[:connection_options] || {}
          # puts "@connection_options: #{@connection_options}"

          @persistent = options[:persistent] || false
          # puts "@persistent: #{@persistent}"
        end

        def init_auth_options
          { :openstack_api_key  => @openstack_api_key,
            :openstack_username => @openstack_username,
            :openstack_auth_token => @openstack_auth_token,
            :openstack_auth_uri => @openstack_auth_uri,
            :openstack_tenant   => @openstack_tenant,
            :openstack_service_type => @openstack_service_type,
            :openstack_service_name => @openstack_service_name,
            :openstack_endpoint_type => @openstack_endpoint_type
          }
        end

        def handle_auth_results(credentials={})
          @current_user = credentials[:user]
          @current_tenant = credentials[:tenant]
          @openstack_must_reauthenticate = false
          @auth_token = credentials[:token]
          @openstack_management_url = credentials[:server_management_url]
          @openstack_current_user_id = credentials[:current_user_id]
          @unscoped_token = credentials[:unscoped_token]
        end

        def save_host_attributes
          uri = URI.parse(@openstack_management_url)
          @host   = uri.host
          @base_path   = uri.path
          @base_path.sub!(/\/$/, '')
          @port   = uri.port
          @scheme = uri.scheme
          puts "HOST: #{@host}"
          puts "path: #{@base_path}"
          puts "port: #{@port}"
          puts "scheme: #{@scheme}"
        end

        def set_credentials(options={})
          @openstack_api_key  = options[:openstack_api_key]
          @openstack_username = options[:openstack_username]
        end

        def validate_credentials
          missing_credentials = Array.new
          missing_credentials << :openstack_api_key  unless @openstack_api_key
          missing_credentials << :openstack_username unless @openstack_username
          if !missing_credentials.empty?
            raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}"
          end
        end

      end

    end
{'fraction_non_alphanumeric': 0.09177792009458594, 'fraction_numerical': 0.0010345337208729495, 'mean_word_length': 2.1125421649800673, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, 'https://': 0, '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': '4993306', 'n_tokens_mistral': 5451, 'n_tokens_neox': 5325, 'n_words': 1030}