Class: LogStash::Outputs::Keystone::Token
- Inherits:
-
Object
- Object
- LogStash::Outputs::Keystone::Token
- Includes:
- Singleton
- Defined in:
- lib/logstash/outputs/keystone/token.rb
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#keystone_client ⇒ Object
Returns the value of attribute keystone_client.
Instance Method Summary collapse
- #get_sleep_time(attempt) ⇒ Object
-
#initialize ⇒ Token
constructor
A new instance of Token.
- #request_new_token(username, user_domain_name, password, project_name, project_domain_name) ⇒ Object
- #set_keystone_client(keystone_api, insecure) ⇒ Object
- #set_token(id, expires_at) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Token
63 64 65 |
# File 'lib/logstash/outputs/keystone/token.rb', line 63 def initialize @logger = Cabin::Channel.get(LogStash) end |
Instance Attribute Details
#expires_at ⇒ Object
Returns the value of attribute expires_at.
25 26 27 |
# File 'lib/logstash/outputs/keystone/token.rb', line 25 def expires_at @expires_at end |
#id ⇒ Object
Returns the value of attribute id.
25 26 27 |
# File 'lib/logstash/outputs/keystone/token.rb', line 25 def id @id end |
#keystone_client ⇒ Object
Returns the value of attribute keystone_client.
25 26 27 |
# File 'lib/logstash/outputs/keystone/token.rb', line 25 def keystone_client @keystone_client end |
Instance Method Details
#get_sleep_time(attempt) ⇒ Object
48 49 50 51 |
# File 'lib/logstash/outputs/keystone/token.rb', line 48 def get_sleep_time(attempt) sleep_for = attempt**2 sleep_for <= 60 ? sleep_for : 60 end |
#request_new_token(username, user_domain_name, password, project_name, project_domain_name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/logstash/outputs/keystone/token.rb', line 27 def request_new_token(username, user_domain_name, password, project_name, project_domain_name) attempt = 0 begin @logger.info("Connecting to keystone, attempt no. #{attempt}") token = @keystone_client .authenticate(username, user_domain_name, password, project_name, project_domain_name) set_token(token[:token], token[:expires_at]) rescue => e attempt += 1 sleep_for = get_sleep_time(attempt) @logger.error("Sending event to keystone threw exception, "\ "will sleep for #{sleep_for} seconds.", :exceptionew => e) Stud.stoppable_sleep(sleep_for) retry end @logger.info('New token requested') @logger.debug("token=#{@id}, expire_at=#{@expires_at}") end |
#set_keystone_client(keystone_api, insecure) ⇒ Object
58 59 60 61 |
# File 'lib/logstash/outputs/keystone/token.rb', line 58 def set_keystone_client(keystone_api, insecure) @keystone_client = LogStash::Outputs::Keystone::KeystoneClient .new(keystone_api, insecure) end |
#set_token(id, expires_at) ⇒ Object
53 54 55 56 |
# File 'lib/logstash/outputs/keystone/token.rb', line 53 def set_token(id, expires_at) @id = id @expires_at = expires_at end |
#valid? ⇒ Boolean
67 68 69 70 71 72 73 74 |
# File 'lib/logstash/outputs/keystone/token.rb', line 67 def valid? token_valid = true now = DateTime.now + Rational(1, 1440) if @id.nil? || now >= @expires_at token_valid = false end token_valid end |