X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;ds=sidebyside;f=lib%2Fmcollective%2Frpc%2Freply.rb;fp=lib%2Fmcollective%2Frpc%2Freply.rb;h=574be55ef6c202a3dba6dcd9a13d22a478cfa334;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/lib/mcollective/rpc/reply.rb b/lib/mcollective/rpc/reply.rb new file mode 100644 index 0000000..574be55 --- /dev/null +++ b/lib/mcollective/rpc/reply.rb @@ -0,0 +1,85 @@ +module MCollective + module RPC + # Simple class to manage compliant replies to MCollective::RPC + class Reply + attr_accessor :statuscode, :statusmsg, :data + + def initialize(action, ddl) + @data = {} + @statuscode = 0 + @statusmsg = "OK" + @ddl = ddl + @action = action + + begin + initialize_data + rescue Exception => e + Log.warn("Could not pre-populate reply data from the DDL: %s: %s" % [e.class, e.to_s ]) + end + end + + def initialize_data + unless @ddl.actions.include?(@action) + raise "No action '%s' defined for agent '%s' in the DDL" % [@action, @ddl.pluginname] + end + + interface = @ddl.action_interface(@action) + + interface[:output].keys.each do |output| + @data[output] = interface[:output][output][:default] + end + end + + # Helper to fill in statusmsg and code on failure + def fail(msg, code=1) + @statusmsg = msg + @statuscode = code + end + + # Helper that fills in statusmsg and code but also raises an appropriate error + def fail!(msg, code=1) + @statusmsg = msg + @statuscode = code + + case code + when 1 + raise RPCAborted, msg + + when 2 + raise UnknownRPCAction, msg + + when 3 + raise MissingRPCData, msg + + when 4 + raise InvalidRPCData, msg + + else + raise UnknownRPCError, msg + end + end + + # Write to the data hash + def []=(key, val) + @data[key] = val + end + + # Read from the data hash + def [](key) + @data[key] + end + + def fetch(key, default) + @data.fetch(key, default) + end + + # Returns a compliant Hash of the reply that should be sent + # over the middleware + def to_hash + return {:statuscode => @statuscode, + :statusmsg => @statusmsg, + :data => @data} + end + end + end +end