Class: VagrantPlugins::Openstack::KeystoneClient

Inherits:
Object
  • Object
show all
Includes:
Singleton, HttpUtils::RequestLogger
Defined in:
lib/vagrant-openstack-provider/client/keystone.rb

Instance Method Summary collapse

Methods included from HttpUtils::RequestLogger

#log_request, #log_response

Constructor Details

#initializeKeystoneClient

Returns a new instance of KeystoneClient.



13
14
15
16
# File 'lib/vagrant-openstack-provider/client/keystone.rb', line 13

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

Instance Method Details

#authenticate(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-openstack-provider/client/keystone.rb', line 18

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