summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails/resource.rb
blob: 7cf6f685264727e7c5925fe52f767e0eb066ad20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'puppet'
require 'puppet/rails/lib/init'
require 'puppet/rails/param_name'

class Puppet::Rails::Resource < ActiveRecord::Base
    has_many :param_values, :through => :param_names
    has_many :param_names
    has_many :source_files
    belongs_to :hosts

    acts_as_taggable

    Puppet::Type.loadall
    Puppet::Type.eachtype do |type|
        klass = Class.new(Puppet::Rails::Resource)
        Object.const_set("Puppet%s" % type.name.to_s.capitalize, klass)
    end

    def parameters
        hash = {}
        self.param_values.find(:all).each do |pvalue|
            pname = self.param_names.find(:first)
            hash.store(pname.name.to_sym, pvalue.value)
        end
        return hash
    end

    # Convert our object to a resource.  Do not retain whether the object
    # is collectable, though, since that would cause it to get stripped
    # from the configuration.
    def to_resource(scope)
        hash = self.attributes
        hash.delete("type")
        hash.delete("host_id")
        hash.delete("source_file_id")
        hash.delete("id")
        hash.each do |p, v|
            hash.delete(p) if v.nil?
        end
        hash[:type] = self.class.to_s.gsub(/Puppet/,'').downcase
        hash[:scope] = scope
        hash[:source] = scope.source
        obj = Puppet::Parser::Resource.new(hash)
        self.param_names.each do |pname|
            obj.set(pname.to_resourceparam(scope.source))
        end

        return obj
    end
end