class MCollective::RPC::Request

Simple class to manage compliant requests for MCollective::RPC agents

Attributes

action[RW]
agent[RW]
caller[RW]
data[RW]
ddl[RW]
sender[RW]
time[RW]
uniqid[RW]

Public Class Methods

new(msg, ddl) click to toggle source
   # File lib/mcollective/rpc/request.rb
 7 def initialize(msg, ddl)
 8   @time = msg[:msgtime]
 9   @action = msg[:body][:action] || msg[:body]["action"]
10   @data = msg[:body][:data] || msg[:body]["data"]
11   @sender = msg[:senderid]
12   @agent = msg[:body][:agent] || msg[:body]["agent"]
13   @uniqid = msg[:requestid]
14   @caller = msg[:callerid] || "unknown"
15   @ddl = ddl
16 end

Public Instance Methods

[](key) click to toggle source

If data is a hash, gives easy access to its members, else returns nil

   # File lib/mcollective/rpc/request.rb
58 def [](key)
59   return nil unless @data.is_a?(Hash)
60   return @data[compatible_key(key)]
61 end
compatible_key(key) click to toggle source

In a scenario where a request came from a JSON pure medium like a REST service or other language client DDL::AgentDDL#validate_rpc_request will check “package” against the intput :package should the input “package” not also be known

Thus once the request is built it will also have “package” and not :package data, so we need to fetch the correct key out of the hash.

   # File lib/mcollective/rpc/request.rb
25 def compatible_key(key)
26   return key if data.include?(key)
27 
28   if ddl
29     input = ddl.action_interface(action)[:input]
30 
31     # if :package is requested and the DDL also declares "package" we cant tell it to fetch
32     # "package", hence the check against the input here
33     return key.to_s if key.is_a?(Symbol) && !input.include?(key.to_s) && data.include?(key.to_s)
34   end
35 
36   key
37 end
fetch(key, default) click to toggle source
   # File lib/mcollective/rpc/request.rb
63 def fetch(key, default)
64   return nil unless @data.is_a?(Hash)
65   return @data.fetch(compatible_key(key), default)
66 end
include?(key) click to toggle source

If data is a hash, quick helper to get access to it's include? method else returns false

   # File lib/mcollective/rpc/request.rb
41 def include?(key)
42   return false unless @data.is_a?(Hash)
43 
44   @data.include?(compatible_key(key))
45 end
should_respond?() click to toggle source

If no :process_results is specified always respond else respond based on the supplied property

   # File lib/mcollective/rpc/request.rb
49 def should_respond?
50   return false unless @data.is_a?(Hash)
51   return @data[:process_results] if @data.include?(:process_results)
52   return @data["process_results"] if @data.include?("process_results")
53 
54   true
55 end
to_hash() click to toggle source
   # File lib/mcollective/rpc/request.rb
68 def to_hash
69   {:agent => @agent,
70    :action => @action,
71    :data => @data}
72 end
to_json() click to toggle source
   # File lib/mcollective/rpc/request.rb
79 def to_json
80   to_hash.merge!({:sender   => @sender,
81                   :callerid => @callerid,
82                   :uniqid   => @uniqid}).to_json
83 end
validate!() click to toggle source

Validate the request against the DDL

   # File lib/mcollective/rpc/request.rb
75 def validate!
76   @ddl.validate_rpc_request(@action, @data)
77 end