Module: Bosh::Director::AgentMessageConverter

Defined in:
lib/bosh/director/agent_message_converter.rb

Class Method Summary collapse

Class Method Details

.convert_old_message_to_new(msg) ⇒ Hash

This converts the old agent response format to the new. This can be taken out once the agents never reply with the old message format. The old message format is just to pass the return object as JSON. That means it could be any type – array, hash, string, int, etc. The new format is: “value”=><task_return_object>,

"agent_task_id"=>123

This is a class method to make testing easier.

Parameters:

  • msg (Object)

    The agent message to convert to the new format.

Returns:

  • (Hash)

    The message in the new message format.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bosh/director/agent_message_converter.rb', line 13

def self.convert_old_message_to_new(msg)
  if msg && msg.is_a?(Hash)
    if !msg.has_key?('value')
      # There's no value but there is state and agent_task_id.
      # Just leave it.
      if msg.has_key?('state') && msg.has_key?('agent_task_id')
        return msg
      end
      # There's no value and either one or both of state/agent_task_id don't
      # exist.  Assume that the whole message response was meant to be the
      # value.
      return { 'value' => msg,
               'state' => msg['state'] || 'done',
               'agent_task_id' => msg['agent_task_id'] || nil }
    elsif !msg.has_key?('state')
      # The message has a value section but no "state".  Mark state as
      # done.
      return { 'value' => msg['value'],
               'state' => 'done',
               'agent_task_id' => msg['agent_task_id'] || nil }
    else
      # The message was good from the start!
      return msg
    end
  end
  # If the message was anything other than a hash (float, int, string,
  # array, etc.) then we want to just make that be the "value".
  { 'state' => 'done', 'value' => msg, 'agent_task_id' => nil }
end