lib/google/cloud/env.rb in google-cloud-env-1.3.3 vs lib/google/cloud/env.rb in google-cloud-env-1.4.0
- old
+ new
@@ -80,10 +80,13 @@
# instance of the information from `Google::Cloud.env`. This constructor
# is provided to allow customization of the timeout/retry settings, as
# well as mocking for testing.
#
# @param [Hash] env Mock environment variables.
+ # @param [String] host The hostname or IP address of the metadata server.
+ # Optional. If not specified, uses the `GCE_METADATA_HOST`,
+ # environment variable or falls back to `169.254.167.254`.
# @param [Hash,false] metadata_cache The metadata cache. You may pass
# a prepopuated cache, an empty cache (the default) or `false` to
# disable the cache completely.
# @param [Numeric] open_timeout Timeout for opening http connections.
# Defaults to 0.1.
@@ -100,11 +103,11 @@
# seconds. Defaults to 0.5.
# @param [Faraday::Connection] connection Faraday connection to use.
# If specified, overrides the `request_timeout` and `open_timeout`
# settings.
#
- def initialize env: nil, connection: nil, metadata_cache: nil,
+ def initialize env: nil, host: nil, connection: nil, metadata_cache: nil,
open_timeout: 0.1, request_timeout: 1.0,
retry_count: 2, retry_interval: 0.1,
retry_backoff_factor: 1.5, retry_max_interval: 0.5
@disable_metadata_cache = metadata_cache == false
@metadata_cache = metadata_cache || {}
@@ -112,10 +115,12 @@
@retry_count = retry_count
@retry_interval = retry_interval
@retry_backoff_factor = retry_backoff_factor
@retry_max_interval = retry_max_interval
request_opts = { timeout: request_timeout, open_timeout: open_timeout }
- @connection = connection || ::Faraday.new(url: METADATA_HOST, request: request_opts)
+ host ||= @env["GCE_METADATA_HOST"] || METADATA_HOST
+ host = "http://#{host}" unless host.start_with? "http://"
+ @connection = connection || ::Faraday.new(url: host, request: request_opts)
end
##
# Determine whether the application is running on a Knative-based
# hosting platform, such as Cloud Run or Cloud Functions.