class Google::Cloud::Env::ComputeMetadata::Overrides
A set of overrides for metadata access. This is used in {ComputeMetadata#overrides=} and {ComputeMetadata#with_overrides}. Generally, you should create and populate an overrides object, then set it using one of those methods.
An empty overrides object that contains no data is interpreted as a metadata server that does not respond and raises MetadataServerNotResponding
. Otherwise, the overrides specifies what responses are returned for specified queries, and any query not explicitly set will result in a 404.
Public Class Methods
Create an empty overrides object.
# File lib/google/cloud/env/compute_metadata.rb, line 160 def initialize clear end
Public Instance Methods
Add an override to the object, providing just a body string.
@param path [String] The key path (e.g. ‘project/project-id`) @param string [String] The response string to return. @param query [Hash{String => String}] Any additional query
parameters for the request.
@return [self] for chaining
# File lib/google/cloud/env/compute_metadata.rb, line 189 def add path, string, query: nil, headers: nil headers = (headers || {}).merge FLAVOR_HEADER response = Response.new 200, string, headers add_response path, response, query: query end
Add an override for the ping request.
@return [self] for chaining
# File lib/google/cloud/env/compute_metadata.rb, line 200 def add_ping add nil, "computeMetadata/\n" end
Add an override to the object, providing a full response.
@param path [String] The key path (e.g. ‘project/project-id`) @param response [Response] The response object to return. @param query [Hash{String => String}] Any additional query
parameters for the request.
@return [self] for chaining
# File lib/google/cloud/env/compute_metadata.rb, line 174 def add_response path, response, query: nil @data[[path, query || {}]] = response self end
Clear all data from these overrides
@return [self] for chaining
# File lib/google/cloud/env/compute_metadata.rb, line 209 def clear @data = {} self end
Returns true if there is at least one override present
@return [true, false]
# File lib/google/cloud/env/compute_metadata.rb, line 233 def empty? @data.empty? end
Look up a response from the override data.
@param path [String] The key path (e.g. ‘project/project-id`) @param query [Hash{String => String}] Any additional query
parameters for the request.
@return [String] The response @return [nil] if there is no data for the given query
# File lib/google/cloud/env/compute_metadata.rb, line 224 def lookup path, query: nil @data[[path, query || {}]] end