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

new() click to toggle source

Create an empty overrides object.

# File lib/google/cloud/env/compute_metadata.rb, line 160
def initialize
  clear
end

Public Instance Methods

add(path, string, query: nil, headers: nil) click to toggle source

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_ping() click to toggle source

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_response(path, response, query: nil) click to toggle source

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() click to toggle source

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
empty?() click to toggle source

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
lookup(path, query: nil) click to toggle source

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