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.



153
154
155
# File 'lib/google/cloud/env/compute_metadata.rb', line 153

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



182
183
184
185
186
# File 'lib/google/cloud/env/compute_metadata.rb', line 182

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



193
194
195
# File 'lib/google/cloud/env/compute_metadata.rb', line 193

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



167
168
169
170
# File 'lib/google/cloud/env/compute_metadata.rb', line 167

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

#clearself

Clear all data from these overrides

Returns:

  • (self)

    for chaining



202
203
204
205
# File 'lib/google/cloud/env/compute_metadata.rb', line 202

def clear
  @data = {}
  self
end

#empty?true, false

Returns true if there is at least one override present

Returns:

  • (true, false)


226
227
228
# File 'lib/google/cloud/env/compute_metadata.rb', line 226

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



217
218
219
# File 'lib/google/cloud/env/compute_metadata.rb', line 217

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