Class MCollective::RPC::DDL
In: lib/mcollective/rpc/ddl.rb
Parent: Object

A class that helps creating data description language files for agents. You can define meta data, actions, input and output describing the behavior of your agent.

Later you can access this information to assist with creating of user interfaces or online help

A sample DDL can be seen below, you‘d put this in your agent dir as <agent name>.ddl

   metadata :name        => "SimpleRPC Service Agent",
            :description => "Agent to manage services using the Puppet service provider",
            :author      => "R.I.Pienaar",
            :license     => "GPLv2",
            :version     => "1.1",
            :url         => "http://mcollective-plugins.googlecode.com/",
            :timeout     => 60

   action "status", :description => "Gets the status of a service" do
      display :always

      input "service",
            :prompt      => "Service Name",
            :description => "The service to get the status for",
            :type        => :string,
            :validation  => '^[a-zA-Z\-_\d]+$',
            :optional    => true,
            :maxlength   => 30

      output "status",
            :description => "The status of service",
            :display_as  => "Service Status"
  end

Methods

Attributes

meta  [R] 

Public Class methods

[Source]

    # File lib/mcollective/rpc/ddl.rb, line 39
39:             def initialize(agent)
40:                 @actions = {}
41:                 @meta = {}
42:                 @config = MCollective::Config.instance
43:                 @agent = agent
44: 
45:                 agentdir = "#{@config.libdir}/mcollective/agent"
46: 
47:                 if File.exist?("#{agentdir}/#{agent}.ddl")
48:                     instance_eval(File.read("#{agentdir}/#{agent}.ddl"))
49:                 else
50:                     raise("Can't find DDL for agent '#{agent}' in #{agentdir}/#{agent}.ddl")
51:                 end
52:             end

Public Instance methods

Creates the definition for an action, you can nest input definitions inside the action to attach inputs and validation to the actions

   action "status", :description => "Restarts a Service" do
      display :always

      input "service",
           :prompt      => "Service Action",
           :description => "The action to perform",
           :type        => :list,
           :optional    => true,
           :list        => ["start", "stop", "restart", "status"]

      output "status"
           :description => "The status of the service after the action"

   end

[Source]

    # File lib/mcollective/rpc/ddl.rb, line 80
80:             def action(name, input, &block)
81:                 raise "Action needs a :description" unless input.include?(:description)
82: 
83:                 unless @actions.include?(name)
84:                     @actions[name] = {}
85:                     @actions[name][:action] = name
86:                     @actions[name][:input] = {}
87:                     @actions[name][:output] = {}
88:                     @actions[name][:display] = :failed
89:                     @actions[name][:description] = input[:description]
90:                 end
91: 
92:                 # if a block is passed it might be creating input methods, call it
93:                 # we set @current_action so the input block can know what its talking
94:                 # to, this is probably an epic hack, need to improve.
95:                 @current_action = name
96:                 block.call if block_given?
97:                 @current_action = nil
98:             end

Returns the interface for a specific action

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 175
175:             def action_interface(name)
176:                 @actions[name] || {}
177:             end

Returns an array of actions this agent support

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 170
170:             def actions
171:                 @actions.keys
172:             end

Sets the display preference to either :ok, :failed, :flatten or :always operates on action level

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 148
148:             def display(pref)
149:                 # defaults to old behavior, complain if its supplied and invalid
150:                 unless [:ok, :failed, :always].include?(pref)
151:                     raise "Display preference #{pref} :ok, :failed, :flatten or :always"
152:                 end
153: 
154:                 action = @current_action
155:                 @actions[action][:display] = pref
156:             end

Generates help using the template based on the data created with metadata and input

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 160
160:             def help(template)
161:                 template = IO.readlines(template).join
162:                 meta = @meta
163:                 actions = @actions
164: 
165:                 erb = ERB.new(template, 0, '%')
166:                 erb.result(binding)
167:             end

Registers an input argument for a given action

See the documentation for action for how to use this

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 103
103:             def input(argument, properties)
104:                 raise "Cannot figure out what action input #{argument} belongs to" unless @current_action
105: 
106:                 action = @current_action
107: 
108:                 [:prompt, :description, :type, :optional].each do |arg|
109:                     raise "Input needs a :#{arg}" unless properties.include?(arg)
110:                 end
111: 
112:                 @actions[action][:input][argument] = {:prompt => properties[:prompt],
113:                                                       :description => properties[:description],
114:                                                       :type => properties[:type],
115:                                                       :optional => properties[:optional]}
116: 
117:                 case properties[:type]
118:                     when :string
119:                         raise "Input type :string needs a :validation" unless properties.include?(:validation)
120:                         raise "String inputs need a :maxlength" unless properties.include?(:validation)
121: 
122:                         @actions[action][:input][argument][:validation] = properties[:validation]
123:                         @actions[action][:input][argument][:maxlength] = properties[:maxlength]
124: 
125:                     when :list
126:                         raise "Input type :list needs a :list argument" unless properties.include?(:list)
127: 
128:                         @actions[action][:input][argument][:list] = properties[:list]
129:                 end
130:             end

Registers meta data for the introspection hash

[Source]

    # File lib/mcollective/rpc/ddl.rb, line 55
55:             def metadata(meta)
56:                 [:name, :description, :author, :license, :version, :url, :timeout].each do |arg|
57:                     raise "Metadata needs a :#{arg}" unless meta.include?(arg)
58:                 end
59: 
60:                 @meta = meta
61:             end

Registers an output argument for a given action

See the documentation for action for how to use this

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 135
135:             def output(argument, properties)
136:                 raise "Cannot figure out what action input #{argument} belongs to" unless @current_action
137:                 raise "Output #{argument} needs a description" unless properties.include?(:description)
138:                 raise "Output #{argument} needs a description" unless properties.include?(:display_as)
139: 
140:                 action = @current_action
141: 
142:                 @actions[action][:output][argument] = {:description => properties[:description],
143:                                                        :display_as  => properties[:display_as]}
144:             end

Helper to use the DDL to figure out if the remote call should be allowed based on action name and inputs.

[Source]

     # File lib/mcollective/rpc/ddl.rb, line 181
181:             def validate_request(action, arguments)
182:                 # is the action known?
183:                 unless actions.include?(action)
184:                     raise DDLValidationError, "Attempted to call action #{action} for #{@agent} but it's not declared in the DDL"
185:                 end
186: 
187:                 input = action_interface(action)[:input]
188: 
189:                 input.keys.each do |key|
190:                     unless input[key][:optional]
191:                         unless arguments.keys.include?(key)
192:                             raise DDLValidationError, "Action #{action} needs a #{key} argument"
193:                         end
194:                     end
195: 
196:                     # validate strings, lists and booleans, we'll add more types of validators when
197:                     # all the use cases are clear
198:                     #
199:                     # only does validation for arguments actually given, since some might
200:                     # be optional.  We validate the presense of the argument earlier so
201:                     # this is a safe assumption, just to skip them.
202:                     #
203:                     # :string can have maxlength and regex.  A maxlength of 0 will bypasss checks
204:                     # :list has a array of valid values
205:                     if arguments.keys.include?(key)
206:                         case input[key][:type]
207:                             when :string
208:                                 raise DDLValidationError, "Input #{key} should be a string" unless arguments[key].is_a?(String)
209: 
210:                                 if input[key][:maxlength].to_i > 0
211:                                     if arguments[key].size > input[key][:maxlength].to_i
212:                                         raise DDLValidationError, "Input #{key} is longer than #{input[key][:maxlength]}"
213:                                     end
214:                                 end
215: 
216:                                 unless arguments[key].match(Regexp.new(input[key][:validation]))
217:                                     raise DDLValidationError, "Input #{key} does not match validation regex #{input[key][:validation]}"
218:                                 end
219: 
220:                             when :list
221:                                 unless input[key][:list].include?(arguments[key])
222:                                     raise DDLValidationError, "Input #{key} doesn't match list #{input[key][:list].join(', ')}"
223:                                 end
224: 
225:                             when :boolean
226:                                 unless [TrueClass, FalseClass].include?(arguments[key].class)
227:                                     raise DDLValidationError, "Input #{key} should be a boolean"
228:                                 end
229:                         end
230:                     end
231:                 end
232:             end

[Validate]