2d848c2b19569a765a0c788ec25f8438563a8b7c
[packages/precise/mcollective.git] / lib / mcollective / data.rb
1 module MCollective
2   module Data
3     autoload :Base, "mcollective/data/base"
4     autoload :Result, "mcollective/data/result"
5
6     def self.load_data_sources
7       PluginManager.find_and_load("data")
8
9       PluginManager.grep(/_data$/).each do |plugin|
10         begin
11           unless PluginManager[plugin].class.activate?
12             Log.debug("Disabling data plugin %s due to plugin activation policy" % plugin)
13             PluginManager.delete(plugin)
14           end
15         rescue Exception => e
16           Log.debug("Disabling data plugin %s due to exception #{e.class}: #{e}" % plugin)
17           PluginManager.delete(plugin)
18         end
19       end
20     end
21
22     def self.pluginname(plugin)
23       plugin.to_s =~ /_data$/i ? plugin.to_s.downcase : "%s_data" % plugin.to_s.downcase
24     end
25
26     def self.[](plugin)
27       PluginManager[pluginname(plugin)]
28     end
29
30     # Data.package("httpd").architecture
31     def self.method_missing(method, *args)
32       super unless PluginManager.include?(pluginname(method))
33
34       PluginManager[pluginname(method)].lookup(args.first)
35     end
36
37     def self.ddl(plugin)
38       DDL.new(pluginname(plugin), :data)
39     end
40
41     def self.ddl_validate(ddl, argument)
42       name = ddl.meta[:name]
43       query = ddl.entities[:data]
44
45       DDL.validation_fail!(:PLMC31, "No dataquery has been defined in the DDL for data plugin '%{plugin}'", :error, :plugin => name)  unless query
46
47       input = query.fetch(:input, {})
48       output = query.fetch(:output, {})
49
50       DDL.validation_fail!(:PLMC32, "No output has been defined in the DDL for data plugin %{plugin}", :error, :plugin => name) if output.keys.empty?
51
52       if input[:query]
53         return true if argument.nil? && input[:query][:optional]
54
55         ddl.validate_input_argument(input, :query, argument)
56       else
57         DDL.validation_fail!(:PLMC33, "No data plugin argument was declared in the '%{plugin}' DDL but an input was supplied", :error, :plugin => name) if argument
58         return true
59       end
60     end
61
62     def self.ddl_has_output?(ddl, output)
63       ddl.entities[:data][:output].include?(output.to_sym) rescue false
64     end
65
66     # For an input where the DDL requests a boolean or some number
67     # this will convert the input to the right type where possible
68     # else just returns the origin input unedited
69     #
70     # if anything here goes wrong just return the input value
71     # this is not really the end of the world or anything since
72     # all that will happen is that DDL validation will fail and
73     # the user will get an error, no need to be too defensive here
74     def self.ddl_transform_input(ddl, input)
75       begin
76         type = ddl.entities[:data][:input][:query][:type]
77
78         case type
79           when :boolean
80             return DDL.string_to_boolean(input)
81
82           when :number, :integer, :float
83             return DDL.string_to_number(input)
84         end
85       rescue
86       end
87
88       return input
89     end
90   end
91 end