diff options
author | ballman <ballman@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-06-12 00:31:16 +0000 |
---|---|---|
committer | ballman <ballman@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-06-12 00:31:16 +0000 |
commit | 68e37a99d5357f022662f9ba7cc564c48aed21a9 (patch) | |
tree | e8d94cc9ea44d312e8b96f94c887b88fc1b04e0f /lib/puppet/parser | |
parent | c26f678178d173bd7360362b2959af51c4d39762 (diff) | |
download | puppet-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/parser')
-rw-r--r-- | lib/puppet/parser/resource.rb | 116 | ||||
-rw-r--r-- | lib/puppet/parser/resource/param.rb | 73 |
2 files changed, 120 insertions, 69 deletions
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 69a6439b3..4147d6327 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -56,14 +56,15 @@ class Puppet::Parser::Resource end end - # Add any metaparams defined in our scope. This actually adds any metaparams + # Add any metaparams defined in our scope. This actually adds any metaparams # from any parent scope, and there's currently no way to turn that off. def addmetaparams Puppet::Type.eachmetaparam do |name| next if self[name] if val = scope.lookupvar(name.to_s, false) unless val == :undefined - set Param.new(:name => name, :value => val, :source => scope.source) + set Param.new(:name => name, :value => val, + :source => scope.source) end end end @@ -172,6 +173,48 @@ class Puppet::Parser::Resource end end + def modify_rails(db_resource) + args = rails_args + args.each do |param, value| + db_resource[param] = value unless db_resource[param] == value + end + + # Handle file specially + if (self.file and + (!db_resource.file or db_resource.file != self.file)) + db_resource.file = self.file + end + + updated_params = @params.inject({}) do |hash, ary| + hash[ary[0].to_s] = ary[1] + hash + end + + db_resource.ar_hash_merge(db_resource.get_params_hash(), updated_params, + :create => Proc.new { |name, parameter| + parameter.to_rails(db_resource) + }, :delete => Proc.new { |values| + values.each { |value| db_resource.param_values.delete(value) } + }, :modify => Proc.new { |db, mem| + mem.modify_rails_values(db) + }) + + updated_tags = tags.inject({}) { |hash, tag| + hash[tag] = tag + hash + } + + db_resource.ar_hash_merge(db_resource.get_tag_hash(), + updated_tags, + :create => Proc.new { |name, tag| + db_resource.add_resource_tag(tag) + }, :delete => Proc.new { |rt| + rt.each { |tag| db_resource.resource_tags.delete(tag) } + }, :modify => Proc.new { |db, mem| + # nothing here + }) + end + # This *significantly* reduces the number of calls to Puppet.[]. def paramcheck? unless defined? @@paramcheck @@ -180,13 +223,13 @@ class Puppet::Parser::Resource @@paramcheck end - # Verify that all passed parameters are valid. This throws an error if there's - # a problem, so we don't have to worry about the return value. + # Verify that all passed parameters are valid. This throws an error if + # there's a problem, so we don't have to worry about the return value. def paramcheck(param) param = param.to_s # Now make sure it's a valid argument to our class. These checks - # are organized in order of commonhood -- most types, it's a valid argument - # and paramcheck is enabled. + # are organized in order of commonhood -- most types, it's a valid + # argument and paramcheck is enabled. if @ref.typeclass.validattr?(param) true elsif %w{name title}.include?(param) # always allow these @@ -228,7 +271,8 @@ class Puppet::Parser::Resource if Puppet[:trace] puts caller end - msg = "Parameter '%s' is already set on %s" % [param.name, self.to_s] + msg = "Parameter '%s' is already set on %s" % + [param.name, self.to_s] if param.source.to_s != "" msg += " by %s" % param.source end @@ -269,46 +313,22 @@ class Puppet::Parser::Resource end end - # Turn our parser resource into a Rails resource. - def to_rails(host, resource = nil) - args = {} - [:type, :title, :line, :exported].each do |param| - # 'type' isn't a valid column name, so we have to use something else. - if param == :type - to = :restype - else - to = param - end - if value = self.send(param) - args[to] = value - end - end + # Turn our parser resource into a Rails resource. + def to_rails(host) + args = rails_args - # If we were passed an object, just make sure all of the attributes are correct. - if resource - # We exist - args.each do |param, value| - v = resource[param] - unless v == value - resource[param] = value - end - end - else - # Else create it anew - resource = host.resources.build(args) - end + db_resource = host.resources.build(args) # Handle file specially - if self.file and (!resource.file or resource.file != self.file) - resource.file = self.file - end + db_resource.file = self.file - # Either way, now add our parameters - updated_params = {} - @params.each { |name, p| updated_params[name.to_s] = p } - resource.collection_merge :param_names, :existing => resource.param_names, :updates => updated_params + @params.each { |name, param| + param.to_rails(db_resource) + } + + tags.each { |tag| db_resource.add_resource_tag(tag) } - return resource + return db_resource end def to_s @@ -350,6 +370,18 @@ class Puppet::Parser::Resource def virtual? self.virtual end + + private + def rails_args + return [:type, :title, :line, :exported].inject({}) do |hash, param| + # 'type' isn't a valid column name, so we have to use another name. + to = (param == :type) ? :restype : param + if value = self.send(param) + hash[to] = value + end + hash + end + end end # $Id$ diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb index dc325487c..34c260a44 100644 --- a/lib/puppet/parser/resource/param.rb +++ b/lib/puppet/parser/resource/param.rb @@ -15,43 +15,62 @@ class Puppet::Parser::Resource::Param "#<#{self.class} @name => #{self.name}, @value => #{self.value}, @source => #{self.source.type}>" end - # Store this parameter in a Rails db. - def to_rails(res, pn = nil) + def line_to_i + return line ? Integer(line) : nil + end + + # Store a new parameter in a Rails db. + def to_rails(db_resource) values = value.is_a?(Array) ? value : [value] + values.map! { |v| v.to_s } - values = values.collect { |v| v.to_s } - - unless pn - # We're creating it anew. - pn = res.param_names.build(:name => self.name.to_s) - end - - value_objects = [] + param_name = Puppet::Rails::ParamName.find_or_create_by_name(self.name.to_s) + line_number = line_to_i() - if l = self.line - pn.line = Integer(l) - end - - oldvals = [] - - if pv = pn.param_values - oldvals = pv.collect { |val| val.value } + return values.collect do |v| + db_resource.param_values.create(:value => v, + :line => line_number, + :param_name => param_name) end + end - if oldvals != values - #pn.param_values = values.collect { |v| pn.param_values.build(:value => v.to_s) } - objects = values.collect do |v| - pn.param_values.build(:value => v.to_s) - end - pn.param_values = objects - end + def modify_rails_values(db_values) + #dev_warn if db_values.nil? || db_values.empty? - return pn + values_to_remove(db_values).each { |remove_me| + Puppet::Rails::ParamValue.delete(remove_me) + } + line_number = line_to_i() + values_to_add(db_values).each { |add_me| + db_resource = db_values[0].resource + db_param_name = db_values[0].param_name + db_resource.param_values.create(:value => add_me, + :line => line_number, + :param_name => db_param_name) + } end - + def to_s "%s => %s" % [self.name, self.value] end + + def values_to_remove(db_values) + values = value.is_a?(Array) ? value : [value] + line_number = line_to_i() + db_values.collect do |db| + db unless (db.line == line_number && + values.find { |v| v == db.value } ) + end.compact + end + + def values_to_add(db_values) + values = value.is_a?(Array) ? value : [value] + line_number = line_to_i() + values.collect do |v| + v unless db_values.find { |db| (v == db.value && + line_number == db.line) } + end.compact + end end # $Id$ |