Sha256: 395b043641eb46abb225828d745584db0d4113c92becbe8395a700f9190de449

Contents?: true

Size: 1.92 KB

Versions: 10

Compression:

Stored size: 1.92 KB

Contents

require 'log4r'
require 'restclient'
require 'json'

require 'vagrant-openstack-provider/client/request_logger'

module VagrantPlugins
  module Openstack
    class KeystoneClient
      include Singleton
      include VagrantPlugins::Openstack::HttpUtils::RequestLogger

      def initialize
        @logger = Log4r::Logger.new('vagrant_openstack::keystone')
        @session = VagrantPlugins::Openstack.session
      end

      def authenticate(env)
        @logger.info('Authenticating on Keystone')
        config = env[:machine].provider_config
        @logger.info(I18n.t('vagrant_openstack.client.authentication', project: config.tenant_name, user: config.username))

        post_body =
          {
            auth:
              {
                tenantName: config.tenant_name,
                passwordCredentials:
                  {
                    username: config.username,
                    password: '****'
                  }
              }
          }

        log_request(:POST, config.openstack_auth_url, post_body.to_json)

        post_body[:auth][:passwordCredentials][:password] = config.password

        authentication = RestClient.post(config.openstack_auth_url, post_body.to_json,
                                         content_type: :json,
                                         accept: :json) do |response|
          log_response(response)
          case response.code
          when 200
            response
          when 401
            fail Errors::AuthenticationFailed
          when 404
            fail Errors::BadAuthenticationEndpoint
          else
            fail Errors::VagrantOpenstackError, message: response.to_s
          end
        end

        access = JSON.parse(authentication)['access']
        response_token = access['token']
        @session.token = response_token['id']
        @session.project_id = response_token['tenant']['id']

        access['serviceCatalog']
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vagrant-openstack-provider-0.6.0 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.5.2 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.5.1 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.5.0 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.4.1 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.4.0 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.3.4.pre lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.3.3 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.3.2 lib/vagrant-openstack-provider/client/keystone.rb
vagrant-openstack-provider-0.3.0 lib/vagrant-openstack-provider/client/keystone.rb