Class: Yao::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/yao/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_info, token_data = nil) ⇒ Token

Returns a new instance of Token.



11
12
13
14
15
# File 'lib/yao/token.rb', line 11

def initialize(auth_info, token_data=nil)
  @auth_info = auth_info

  @endpoints = {}
end

Instance Attribute Details

#auth_infoObject

Returns the value of attribute auth_info.



16
17
18
# File 'lib/yao/token.rb', line 16

def auth_info
  @auth_info
end

#endpointsObject

Returns the value of attribute endpoints.



16
17
18
# File 'lib/yao/token.rb', line 16

def endpoints
  @endpoints
end

#expire_atObject Also known as: expires

Returns the value of attribute expire_at.



16
17
18
# File 'lib/yao/token.rb', line 16

def expire_at
  @expire_at
end

#issued_atObject

Returns the value of attribute issued_at.



16
17
18
# File 'lib/yao/token.rb', line 16

def issued_at
  @issued_at
end

#tokenObject Also known as: to_s

Returns the value of attribute token.



16
17
18
# File 'lib/yao/token.rb', line 16

def token
  @token
end

Class Method Details

.issue(cli, auth_info) ⇒ Object



5
6
7
8
9
# File 'lib/yao/token.rb', line 5

def self.issue(cli, auth_info)
  t = new(auth_info)
  t.refresh(cli)
  t
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/yao/token.rb', line 27

def expired?
  return true unless self.expire_at
  Time.now >= self.expire_at
end

#refresh(cli) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yao/token.rb', line 32

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) ⇒ Object



20
21
22
23
24
25
# File 'lib/yao/token.rb', line 20

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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yao/token.rb', line 46

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