Class: MCollective::RPC::Reply

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

Overview

Simple class to manage compliant replies to MCollective::RPC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, ddl) ⇒ Reply

Returns a new instance of Reply.



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

def initialize(action, ddl)
  @data = {}
  @statuscode = 0
  @statusmsg = "OK"
  @ddl = ddl
  @action = action

  begin
    initialize_data
  rescue Exception => e # rubocop:disable Lint/RescueException
    Log.warn("Could not pre-populate reply data from the DDL: %s: %s" % [e.class, e.to_s])
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#statuscodeObject

Returns the value of attribute statuscode.



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

def statuscode
  @statuscode
end

#statusmsgObject

Returns the value of attribute statusmsg.



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

def statusmsg
  @statusmsg
end

Instance Method Details

#[](key) ⇒ Object

Read from the data hash



68
69
70
# File 'lib/mcollective/rpc/reply.rb', line 68

def [](key)
  @data[key]
end

#[]=(key, val) ⇒ Object

Write to the data hash



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

def []=(key, val)
  @data[key] = val
end

#fail(msg, code = 1) ⇒ Object

Helper to fill in statusmsg and code on failure



34
35
36
37
# File 'lib/mcollective/rpc/reply.rb', line 34

def fail(msg, code=1)
  @statusmsg = msg
  @statuscode = code
end

#fail!(msg, code = 1) ⇒ Object

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mcollective/rpc/reply.rb', line 40

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

#fetch(key, default) ⇒ Object



72
73
74
# File 'lib/mcollective/rpc/reply.rb', line 72

def fetch(key, default)
  @data.fetch(key, default)
end

#initialize_dataObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mcollective/rpc/reply.rb', line 21

def initialize_data
  raise "No action '%s' defined for agent '%s' in the DDL" % [@action, @ddl.pluginname] unless @ddl.actions.include?(@action)

  interface = @ddl.action_interface(@action)

  interface[:output].each_key do |output|
    # must deep clone this data to avoid accidental updates of the DDL in cases where the
    # default is for example a string and someone does << on it
    @data[output] = Marshal.load(Marshal.dump(interface[:output][output].fetch(:default, nil)))
  end
end

#to_hashObject

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



78
79
80
81
82
# File 'lib/mcollective/rpc/reply.rb', line 78

def to_hash
  {:statuscode => @statuscode,
   :statusmsg => @statusmsg,
   :data => @data}
end