Class: Latitude::Metadata::Session
- Inherits:
-
Object
- Object
- Latitude::Metadata::Session
- Defined in:
- lib/latitude/metadata/session.rb
Constant Summary collapse
- BASE_URL =
"http://169.254.169.254/metadata/v1"- TOKEN_REFRESH_RATIO =
0.9- JSON_PATHS =
%w[tags network storage ssh_keys vendor].freeze
- UNAVAILABLE_EXCEPTIONS =
[ Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout, SocketError, ].freeze
Instance Method Summary collapse
- #all ⇒ Object
- #fetch(path) ⇒ Object
-
#initialize(ttl: DEFAULT_TTL_SECONDS, open_timeout: 2, read_timeout: 3) ⇒ Session
constructor
A new instance of Session.
- #refresh_token! ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize(ttl: DEFAULT_TTL_SECONDS, open_timeout: 2, read_timeout: 3) ⇒ Session
Returns a new instance of Session.
20 21 22 23 24 25 26 27 |
# File 'lib/latitude/metadata/session.rb', line 20 def initialize(ttl: DEFAULT_TTL_SECONDS, open_timeout: 2, read_timeout: 3) @ttl = Integer(ttl) @open_timeout = open_timeout @read_timeout = read_timeout @mutex = Mutex.new @token = nil @token_expires_at = nil end |
Instance Method Details
#all ⇒ Object
37 38 39 40 41 42 |
# File 'lib/latitude/metadata/session.rb', line 37 def all body = get("metadata.json") wrap(JSON.parse(body)) rescue JSON::ParserError => e raise RequestError.new("Invalid JSON from metadata service: #{e.}") end |
#fetch(path) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/latitude/metadata/session.rb', line 44 def fetch(path) path = path.to_s.sub(%r{\A/}, "") body = get(path) if JSON_PATHS.include?(path) || path == "metadata.json" begin wrap(JSON.parse(body)) rescue JSON::ParserError body end else body end end |
#refresh_token! ⇒ Object
33 34 35 |
# File 'lib/latitude/metadata/session.rb', line 33 def refresh_token! @mutex.synchronize { fetch_new_token } end |
#token ⇒ Object
29 30 31 |
# File 'lib/latitude/metadata/session.rb', line 29 def token @mutex.synchronize { refresh_token_if_needed } end |