class MCollective::RPC::Reply

Simple class to manage compliant replies to MCollective::RPC

Attributes

data[RW]
statuscode[RW]
statusmsg[RW]

Public Class Methods

new(action, ddl) click to toggle source
   # File lib/mcollective/rpc/reply.rb
 7 def initialize(action, ddl)
 8   @data = {}
 9   @statuscode = 0
10   @statusmsg = "OK"
11   @ddl = ddl
12   @action = action
13 
14   begin
15     initialize_data
16   rescue Exception => e
17     Log.warn("Could not pre-populate reply data from the DDL: %s: %s" % [e.class, e.to_s ])
18   end
19 end

Public Instance Methods

[](key) click to toggle source

Read from the data hash

   # File lib/mcollective/rpc/reply.rb
70 def [](key)
71   @data[key]
72 end
[]=(key, val) click to toggle source

Write to the data hash

   # File lib/mcollective/rpc/reply.rb
65 def []=(key, val)
66   @data[key] = val
67 end
fail(msg, code=1) click to toggle source

Helper to fill in statusmsg and code on failure

   # File lib/mcollective/rpc/reply.rb
36 def fail(msg, code=1)
37   @statusmsg = msg
38   @statuscode = code
39 end
fail!(msg, code=1) click to toggle source

Helper that fills in statusmsg and code but also raises an appropriate error

   # File lib/mcollective/rpc/reply.rb
42 def fail!(msg, code=1)
43   @statusmsg = msg
44   @statuscode = code
45 
46   case code
47     when 1
48       raise RPCAborted, msg
49 
50     when 2
51       raise UnknownRPCAction, msg
52 
53     when 3
54       raise MissingRPCData, msg
55 
56     when 4
57       raise InvalidRPCData, msg
58 
59     else
60       raise UnknownRPCError, msg
61   end
62 end
fetch(key, default) click to toggle source
   # File lib/mcollective/rpc/reply.rb
74 def fetch(key, default)
75   @data.fetch(key, default)
76 end
initialize_data() click to toggle source
   # File lib/mcollective/rpc/reply.rb
21 def initialize_data
22   unless @ddl.actions.include?(@action)
23     raise "No action '%s' defined for agent '%s' in the DDL" % [@action, @ddl.pluginname]
24   end
25 
26   interface = @ddl.action_interface(@action)
27 
28   interface[:output].keys.each do |output|
29     # must deep clone this data to avoid accidental updates of the DDL in cases where the
30     # default is for example a string and someone does << on it
31     @data[output] = Marshal.load(Marshal.dump(interface[:output][output].fetch(:default, nil)))
32   end
33 end
to_hash() click to toggle source

Returns a compliant Hash of the reply that should be sent over the middleware

   # File lib/mcollective/rpc/reply.rb
80 def to_hash
81   return {:statuscode => @statuscode,
82           :statusmsg => @statusmsg,
83           :data => @data}
84 end