Skip to content

Commit

Permalink
3 possible ways for using keystone
Browse files Browse the repository at this point in the history
  • Loading branch information
thousandsofthem committed Nov 27, 2014
1 parent 557d2a3 commit ead8502
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
21 changes: 13 additions & 8 deletions lib/iron_core/client.rb
Expand Up @@ -77,15 +77,15 @@ def initialize(company, product, options = {}, default_options = {}, extra_optio

@rest = Rest::Client.new(:gem => http_gem)

keystone_required_keys_list = [:username, :password, :tenant, :server]
if !self.keystone.nil?
if self.keystone.class == Hash && (self.keystone.keys & keystone_required_keys_list).length == 4
@token_provider = IronCore::KeystoneTokenProvider.new(@rest, self.keystone)
else
missing = (keystone_required_keys_list - self.keystone.keys).map{|i| i.to_s}.join(', ')
IronCore::Logger.error 'IronCore', "Keystone keys missing: #{missing}", IronCore::Error
raise IronCore::ConfigurationError.new("Keystone keys missing: #{missing}")
if self.keystone && self.keystone.is_a?(Hash)
raise_keystone_config_error('server') if self.keystone[:server].nil?
raise_keystone_config_error('tenant') if self.keystone[:tenant].nil?
if self.keystone[:token].nil? && self.keystone[:tenant_token].nil? &&
(self.keystone[:username].nil? && self.keystone[:password].nil?)
raise_keystone_config_error('username, password or token')
end

@token_provider = IronCore::KeystoneTokenProvider.new(@rest, self.keystone)
else
@token_provider = IronCore::IronTokenProvider.new(@token)
end
Expand Down Expand Up @@ -340,5 +340,10 @@ def check_id(id, name = 'id', length = 24)
IronCore::Logger.error 'IronCore', "Expecting #{length} symbol #{name} string", IronCore::Error
end
end

def raise_keystone_config_error(missing)
IronCore::Logger.error 'IronCore', "Keystone keys missing: #{missing}", IronCore::Error
raise IronCore::ConfigurationError.new("Keystone keys missing: #{missing}")
end
end
end
25 changes: 15 additions & 10 deletions lib/iron_core/keystone_token_provider.rb
Expand Up @@ -2,24 +2,29 @@ module IronCore
class KeystoneTokenProvider
def initialize(client, options)
@rest_client = client.dup
@token = nil
@token = options[:tenant_token] # Way to bypass fetching a token from keystone api
@server = options[:server]
@tenant = options[:tenant]
@username = options[:username]
@password = options[:password]
@user_token = options[:token]
end

def token
if @token.nil? || (Time.now - @local_expirest_at > -10)
if @token.nil? || (@local_expires_at && (Time.now - @local_expires_at > -10))
payload = {
auth: {
tenantId: @tenant,
passwordCredentials: {
username: @username,
password: @password
}
}
auth: {
tenantId: @tenant,
}
}
if @username.to_s != ''
payload[:auth][:passwordCredentials] = {
username: @username,
password: @password
}
elsif @user_token.to_s != ''
payload[:auth][:token] = {id: @user_token}
end

response = post(@server + 'tokens', payload)
result = JSON.parse(response.body)
Expand All @@ -29,7 +34,7 @@ def token
expires = Time.parse(token_data['expires'] + " UTC")
duration = (expires - issued_at).to_i

@local_expirest_at = Time.now + duration
@local_expires_at = Time.now + duration
@token = token_data['id']
end

Expand Down

0 comments on commit ead8502

Please sign in to comment.