Class: Google::Cloud::Env::ComputeMetadata::Overrides

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/env/compute_metadata.rb

Overview

A set of overrides for metadata access. This is used in #overrides= and #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.

Instance Method Summary collapse

Constructor Details

#initializeOverrides

Create an empty overrides object.



160
161
162
# File 'lib/google/cloud/env/compute_metadata.rb', line 160

def initialize
  clear
end

Instance Method Details

#add(path, string, query: nil, headers: nil) ⇒ self

Add an override to the object, providing just a body string.

Parameters:

  • path (String)

    The key path (e.g. project/project-id)

  • string (String)

    The response string to return.

  • query (Hash{String => String}) (defaults to: nil)

    Any additional query parameters for the request.

Returns:

  • (self)

    for chaining



189
190
191
192
193
# 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_pingself

Add an override for the ping request.

Returns:

  • (self)

    for chaining



200
201
202
# File 'lib/google/cloud/env/compute_metadata.rb', line 200

def add_ping
  add nil, "computeMetadata/\n"
end

#add_response(path, response, query: nil) ⇒ self

Add an override to the object, providing a full response.

Parameters:

  • path (String)

    The key path (e.g. project/project-id)

  • response (Response)

    The response object to return.

  • query (Hash{String => String}) (defaults to: nil)

    Any additional query parameters for the request.

Returns:

  • (self)

    for chaining



174
175
176
177
# File 'lib/google/cloud/env/compute_metadata.rb', line 174

def add_response path, response, query: nil
  @data[[path, query || {}]] = response
  self
end

#clearself

Clear all data from these overrides

Returns:

  • (self)

    for chaining



209
210
211
212
# File 'lib/google/cloud/env/compute_metadata.rb', line 209

def clear
  @data = {}
  self
end

#empty?true, false

Returns true if there is at least one override present

Returns:

  • (true, false)


233
234
235
# File 'lib/google/cloud/env/compute_metadata.rb', line 233

def empty?
  @data.empty?
end

#lookup(path, query: nil) ⇒ String?

Look up a response from the override data.

Parameters:

  • path (String)

    The key path (e.g. project/project-id)

  • query (Hash{String => String}) (defaults to: nil)

    Any additional query parameters for the request.

Returns:

  • (String)

    The response

  • (nil)

    if there is no data for the given query



224
225
226
# File 'lib/google/cloud/env/compute_metadata.rb', line 224

def lookup path, query: nil
  @data[[path, query || {}]]
end