summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails/resource.rb
diff options
context:
space:
mode:
authorballman <ballman@980ebf18-57e1-0310-9a29-db15c13687c0>2007-06-12 00:31:16 +0000
committerballman <ballman@980ebf18-57e1-0310-9a29-db15c13687c0>2007-06-12 00:31:16 +0000
commit68e37a99d5357f022662f9ba7cc564c48aed21a9 (patch)
treee8d94cc9ea44d312e8b96f94c887b88fc1b04e0f /lib/puppet/rails/resource.rb
parentc26f678178d173bd7360362b2959af51c4d39762 (diff)
downloadpuppet-68e37a99d5357f022662f9ba7cc564c48aed21a9.tar.gz
puppet-68e37a99d5357f022662f9ba7cc564c48aed21a9.tar.xz
puppet-68e37a99d5357f022662f9ba7cc564c48aed21a9.zip
Major rework of the rails feature. Changed the relationship between
host and facts (now many-to-many with fact_name through fact_values). Also changed the relationship between resource and params (similarly many-to-many with param_names through param_values). Added the resource_tags and puppet_tags. The latter has the tag names and the former is the man-to-many link with resources. There is a little clean up left but the schema is in order. Also a test for the tags stuff is required. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2565 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/rails/resource.rb')
-rw-r--r--lib/puppet/rails/resource.rb49
1 files changed, 39 insertions, 10 deletions
diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb
index 49bd1dacc..d2d4ec21f 100644
--- a/lib/puppet/rails/resource.rb
+++ b/lib/puppet/rails/resource.rb
@@ -1,15 +1,25 @@
require 'puppet'
require 'puppet/rails/param_name'
+require 'puppet/rails/puppet_tag'
require 'puppet/util/rails/collection_merger'
class Puppet::Rails::Resource < ActiveRecord::Base
include Puppet::Util::CollectionMerger
- has_many :param_values, :through => :param_names
- has_many :param_names, :dependent => :destroy
+ has_many :param_values, :dependent => :destroy
+ has_many :param_names, :through => :param_values
+
+ has_many :resource_tags, :dependent => :destroy
+ has_many :puppet_tags, :through => :resource_tags
+
belongs_to :source_file
belongs_to :host
+ def add_resource_tag(tag)
+ pt = Puppet::Rails::PuppetTag.find_or_create_by_name(tag)
+ resource_tags.create(:puppet_tag => pt)
+ end
+
def file
if f = self.source_file
return f.filename
@@ -19,7 +29,25 @@ class Puppet::Rails::Resource < ActiveRecord::Base
end
def file=(file)
- self.source_file = Puppet::Rails::SourceFile.new(:filename => file)
+ self.source_file = Puppet::Rails::SourceFile.find_or_create_by_filename(file)
+ end
+
+ # returns a hash of param_names.name => [param_values]
+ def get_params_hash
+ param_values = self.param_values.find(:all, :include => :param_name)
+ return param_values.inject({}) do | hash, value |
+ hash[value.param_name.name] ||= []
+ hash[value.param_name.name] << value
+ hash
+ end
+ end
+
+ def get_tag_hash
+ tags = self.resource_tags.find(:all, :include => :puppet_tag)
+ return tags.inject({}) do |hash, tag|
+ hash[tag.puppet_tag.name] = tag.puppet_tag.name
+ hash
+ end
end
def [](param)
@@ -32,7 +60,7 @@ class Puppet::Rails::Resource < ActiveRecord::Base
def parameter(param)
if pn = param_names.find_by_name(param)
- if pv = pn.param_values.find(:first)
+ if pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn] )
return pv.value
else
return nil
@@ -41,12 +69,12 @@ class Puppet::Rails::Resource < ActiveRecord::Base
end
def parameters
- hash = {}
- self.param_values.find(:all).each do |pvalue|
- pname = pvalue.param_name.name
- hash.store(pname, pvalue.value)
+ return self.param_values.find(:all,
+ :include => :param_name).inject({}) do |hash, pvalue|
+ hash[pvalue.param_name.name] ||= []
+ hash[pvalue.param_name.name] << pvalue.value
+ hash
end
- return hash
end
def ref
@@ -73,8 +101,9 @@ class Puppet::Rails::Resource < ActiveRecord::Base
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))
+ obj.set(pname.to_resourceparam(self, scope.source))
end
# Store the ID, so we can check if we're re-collecting the same resource.