Class: MCollective::RPC::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/rpc/request.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, ddl) ⇒ Request

Returns a new instance of Request.



7
8
9
10
11
12
13
14
15
16
# File 'lib/mcollective/rpc/request.rb', line 7

def initialize(msg, ddl)
  @time = msg[:msgtime]
  @action = msg[:body][:action] || msg[:body]["action"]
  @data = msg[:body][:data] || msg[:body]["data"]
  @sender = msg[:senderid]
  @agent = msg[:body][:agent] || msg[:body]["agent"]
  @uniqid = msg[:requestid]
  @caller = msg[:callerid] || "unknown"
  @ddl = ddl
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def action
  @action
end

#agentObject

Returns the value of attribute agent.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def agent
  @agent
end

#callerObject

Returns the value of attribute caller.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def caller
  @caller
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def data
  @data
end

#ddlObject

Returns the value of attribute ddl.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def ddl
  @ddl
end

#senderObject

Returns the value of attribute sender.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def sender
  @sender
end

#timeObject

Returns the value of attribute time.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def time
  @time
end

#uniqidObject

Returns the value of attribute uniqid.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def uniqid
  @uniqid
end

Instance Method Details

#[](key) ⇒ Object

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



58
59
60
61
# File 'lib/mcollective/rpc/request.rb', line 58

def [](key)
  return nil unless @data.is_a?(Hash)
  return @data[compatible_key(key)]
end

#compatible_key(key) ⇒ Object

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mcollective/rpc/request.rb', line 25

def compatible_key(key)
  return key if data.include?(key)

  if ddl
    input = ddl.action_interface(action)[:input]

    # if :package is requested and the DDL also declares "package" we cant tell it to fetch
    # "package", hence the check against the input here
    return key.to_s if key.is_a?(Symbol) && !input.include?(key.to_s) && data.include?(key.to_s)
  end

  key
end

#fetch(key, default) ⇒ Object



63
64
65
66
# File 'lib/mcollective/rpc/request.rb', line 63

def fetch(key, default)
  return nil unless @data.is_a?(Hash)
  return @data.fetch(compatible_key(key), default)
end

#include?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/mcollective/rpc/request.rb', line 41

def include?(key)
  return false unless @data.is_a?(Hash)

  @data.include?(compatible_key(key))
end

#should_respond?Boolean

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

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/mcollective/rpc/request.rb', line 49

def should_respond?
  return false unless @data.is_a?(Hash)
  return @data[:process_results] if @data.include?(:process_results)
  return @data["process_results"] if @data.include?("process_results")

  true
end

#to_hashObject



68
69
70
71
72
# File 'lib/mcollective/rpc/request.rb', line 68

def to_hash
  {:agent => @agent,
   :action => @action,
   :data => @data}
end

#to_jsonObject



79
80
81
82
83
# File 'lib/mcollective/rpc/request.rb', line 79

def to_json
  to_hash.merge!({:sender   => @sender,
                  :callerid => @callerid,
                  :uniqid   => @uniqid}).to_json
end

#validate!Object

Validate the request against the DDL



75
76
77
# File 'lib/mcollective/rpc/request.rb', line 75

def validate!
  @ddl.validate_rpc_request(@action, @data)
end