class Yao::Token

Attributes

auth_info[RW]
endpoints[RW]
expire_at[RW]
expires[RW]
issued_at[RW]
to_s[RW]
token[RW]

Public Class Methods

issue(cli, auth_info) click to toggle source
# File lib/yao/token.rb, line 6
def self.issue(cli, auth_info)
  t = new(auth_info)
  t.refresh(cli)
  t
end
new(auth_info, token_data=nil) click to toggle source
# File lib/yao/token.rb, line 12
def initialize(auth_info, token_data=nil)
  @auth_info = auth_info

  @endpoints = {}
end

Public Instance Methods

expired?() click to toggle source
# File lib/yao/token.rb, line 28
def expired?
  return true unless self.expire_at
  Time.now >= self.expire_at
end
refresh(cli) click to toggle source
# File lib/yao/token.rb, line 33
def refresh(cli)
  @endpoints.clear

  res = cli.post("#{Yao.config.auth_url}/tokens") do |req|
    req.body = auth_info.to_json
    req.headers['Content-Type'] = 'application/json'
  end
  body = res.body["access"]

  register(body["token"])
  register_endpoints(body["serviceCatalog"])
  self
end
register(token_data) click to toggle source
# File lib/yao/token.rb, line 21
def register(token_data)
  @token = token_data["id"]
  @issued_at = Time.parse(token_data["issued_at"]).localtime
  @expire_at = Time.parse(token_data["expires"]).localtime
  Yao.current_tenant_id token_data["tenant"]["id"]
end
register_endpoints(_endpoints) click to toggle source
# File lib/yao/token.rb, line 47
def register_endpoints(_endpoints)
  return unless _endpoints

  _endpoints.each do |endpoint_data|
    type = endpoint_data["type"]
    region_name = Yao.config.region_name ? Yao.config.region_name : 'RegionOne'
    endpoint = endpoint_data["endpoints"].find { |ep| ep.has_value?(region_name) }
    urls = {}
    if endpoint
      urls[:public_url] = endpoint["publicURL"] if endpoint["publicURL"]
      urls[:admin_url]  = endpoint["adminURL"]  if endpoint["adminURL"]
    end
    @endpoints[type] = urls
  end

  Yao.default_client.register_endpoints(@endpoints, token: self)
end