Class MCollective::RPC::Reply
In: lib/mcollective/rpc/reply.rb
Parent: Object

Simple class to manage compliant replies to MCollective::RPC

Methods

[]   []=   fail   fail!   initialize_data   new   to_hash  

Attributes

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

Public Class methods

[Source]

    # File lib/mcollective/rpc/reply.rb, line 7
 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

Read from the data hash

[Source]

    # File lib/mcollective/rpc/reply.rb, line 68
68:       def [](key)
69:         @data[key]
70:       end

Write to the data hash

[Source]

    # File lib/mcollective/rpc/reply.rb, line 63
63:       def []=(key, val)
64:         @data[key] = val
65:       end

Helper to fill in statusmsg and code on failure

[Source]

    # File lib/mcollective/rpc/reply.rb, line 34
34:       def fail(msg, code=1)
35:         @statusmsg = msg
36:         @statuscode = code
37:       end

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

[Source]

    # File lib/mcollective/rpc/reply.rb, line 40
40:       def fail!(msg, code=1)
41:         @statusmsg = msg
42:         @statuscode = code
43: 
44:         case code
45:           when 1
46:             raise RPCAborted, msg
47: 
48:           when 2
49:             raise UnknownRPCAction, msg
50: 
51:           when 3
52:             raise MissingRPCData, msg
53: 
54:           when 4
55:             raise InvalidRPCData, msg
56: 
57:           else
58:             raise UnknownRPCError, msg
59:         end
60:       end

[Source]

    # File lib/mcollective/rpc/reply.rb, line 21
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:           @data[output] = interface[:output][output][:default]
30:         end
31:       end

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

[Source]

    # File lib/mcollective/rpc/reply.rb, line 74
74:       def to_hash
75:         return {:statuscode => @statuscode,
76:                 :statusmsg => @statusmsg,
77:                 :data => @data}
78:       end

[Validate]