Class: Yao::TokenV3

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_info, token_data = nil) ⇒ TokenV3

Returns a new instance of TokenV3.



11
12
13
14
15
# File 'lib/yao/tokenv3.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/tokenv3.rb', line 16

def auth_info
  @auth_info
end

#endpointsObject

Returns the value of attribute endpoints.



16
17
18
# File 'lib/yao/tokenv3.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/tokenv3.rb', line 16

def expire_at
  @expire_at
end

#issued_atObject

Returns the value of attribute issued_at.



16
17
18
# File 'lib/yao/tokenv3.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/tokenv3.rb', line 16

def token
  @token
end

Class Method Details

.issue(cli, auth_info) ⇒ Object



5
6
7
8
9
# File 'lib/yao/tokenv3.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)


29
30
31
32
# File 'lib/yao/tokenv3.rb', line 29

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

#refresh(cli) ⇒ Object



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

def refresh(cli)
  @endpoints.clear

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

  register(res)
  register_endpoints(res.body["token"]["catalog"])
  self
end

#register(response) ⇒ Object



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

def register(response)
  @token = response.headers["X-Subject-Token"]

  token_data = response.body["token"]
  @issued_at = Time.parse(token_data["issued_at"]).localtime
  @expire_at = Time.parse(token_data["expires_at"]).localtime
  Yao.current_tenant_id token_data["project"]["id"]
end

#register_endpoints(_endpoints) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yao/tokenv3.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'
    endpoints = endpoint_data["endpoints"].select { |ep| ep.has_value?(region_name) }
    urls = {}
    endpoints.each do |ep|
      name = "#{ep["interface"]}_url".to_sym
      urls[name] = ep["url"]
    end
    @endpoints[type] = urls
  end

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