diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index f1f8ccd00..ac5a4bcdc 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -1,275 +1,282 @@ require 'puppet/resource' # The primary difference between this class and its # parent is that this class has rules on who can set # parameters class Puppet::Parser::Resource < Puppet::Resource require 'puppet/parser/resource/param' require 'puppet/util/tagging' require 'puppet/parser/yaml_trimmer' require 'puppet/resource/type_collection_helper' include Puppet::Resource::TypeCollectionHelper include Puppet::Util include Puppet::Util::MethodHelper include Puppet::Util::Errors include Puppet::Util::Logging include Puppet::Parser::YamlTrimmer attr_accessor :source, :scope, :collector_id attr_accessor :virtual, :override, :translated, :catalog, :evaluated attr_accessor :file, :line attr_reader :exported, :parameters # Determine whether the provided parameter name is a relationship parameter. def self.relationship_parameter?(name) @relationship_names ||= Puppet::Type.relationship_params.collect { |p| p.name } @relationship_names.include?(name) end # Set up some boolean test methods def translated?; !!@translated; end def override?; !!@override; end def evaluated?; !!@evaluated; end def [](param) param = param.intern if param == :title return self.title end if @parameters.has_key?(param) @parameters[param].value else nil end end def eachparam @parameters.each do |name, param| yield param end end def environment scope.environment end # Process the stage metaparameter for a class. A containment edge # is drawn from the class to the stage. The stage for containment # defaults to main, if none is specified. def add_edge_to_stage return unless self.class? unless stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main) raise ArgumentError, "Could not find stage #{self[:stage] || :main} specified by #{self}" end self[:stage] ||= stage.title unless stage.title == :main catalog.add_edge(stage, self) end # Retrieve the associated definition and evaluate it. def evaluate return if evaluated? @evaluated = true if klass = resource_type and ! builtin_type? finish evaluated_code = klass.evaluate_code(self) return evaluated_code elsif builtin? devfail "Cannot evaluate a builtin type (#{type})" else self.fail "Cannot find definition #{type}" end end # Mark this resource as both exported and virtual, # or remove the exported mark. def exported=(value) if value @virtual = true @exported = value else @exported = value end end # Do any finishing work on this object, called before evaluation or # before storage/translation. def finish return if finished? @finished = true add_defaults add_scope_tags validate end # Has this resource already been finished? def finished? @finished end def initialize(*args) raise ArgumentError, "Resources require a hash as last argument" unless args.last.is_a? Hash raise ArgumentError, "Resources require a scope" unless args.last[:scope] super @source ||= scope.source end # Is this resource modeling an isomorphic resource type? def isomorphic? if builtin_type? return resource_type.isomorphic? else return true end end # Merge an override resource in. This will throw exceptions if # any overrides aren't allowed. def merge(resource) # Test the resource scope, to make sure the resource is even allowed # to override. unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source) raise Puppet::ParseError.new("Only subclasses can override parameters", resource.line, resource.file) end # Some of these might fail, but they'll fail in the way we want. resource.parameters.each do |name, param| override_parameter(param) end end # This only mattered for clients < 0.25, which we don't support any longer. # ...but, since this hasn't been deprecated, and at least some functions # used it, deprecate now rather than just eliminate. --daniel 2012-07-15 def metaparam_compatibility_mode? Puppet.deprecation_warning "metaparam_compatibility_mode? is obsolete since < 0.25 clients are really, really not supported any more" false end def name self[:name] || self.title end # A temporary occasion, until I get paths in the scopes figured out. alias path to_s # Define a parameter in our resource. # if we ever receive a parameter named 'tag', set # the resource tags with its value. def set_parameter(param, value = nil) if ! param.is_a?(Puppet::Parser::Resource::Param) param = Puppet::Parser::Resource::Param.new( :name => param, :value => value, :source => self.source ) end tag(*param.value) if param.name == :tag # And store it in our parameter hash. @parameters[param.name] = param end alias []= set_parameter def to_hash @parameters.inject({}) do |hash, ary| param = ary[1] # Skip "undef" and nil values. hash[param.name] = param.value if param.value != :undef && !param.value.nil? hash end end # Convert this resource to a RAL resource. def to_ral copy_as_resource.to_ral end - # Is the receiver tagged with the given tags? - # This match takes into account the tags that a resource will inherit from its container + # Answers if this resource is tagged with at least one of the tags given in downcased string form. + # + # The method is a faster variant of the tagged? method that does no conversion of its + # arguments. + # + # The match takes into account the tags that a resource will inherit from its container # but have not been set yet. # It does *not* take tags set via resource defaults as these will *never* be set on # the resource itself since all resources always have tags that are automatically # assigned. # - def tagged?(*tags) - super || ((scope_resource = scope.resource) && scope_resource != self && scope_resource.tagged?(tags)) + # @param tag_array [Array[String]] list tags to look for + # @return [Boolean] true if this instance is tagged with at least one of the provided tags + # + def raw_tagged?(tag_array) + super || ((scope_resource = scope.resource) && !scope_resource.equal?(self) && scope_resource.raw_tagged?(tag_array)) end private # Add default values from our definition. def add_defaults scope.lookupdefaults(self.type).each do |name, param| unless @parameters.include?(name) self.debug "Adding default for #{name}" @parameters[name] = param.dup end end end def add_scope_tags if scope_resource = scope.resource tag(*scope_resource.tags) end end # Accept a parameter from an override. def override_parameter(param) # This can happen if the override is defining a new parameter, rather # than replacing an existing one. (set_parameter(param) and return) unless current = @parameters[param.name] # The parameter is already set. Fail if they're not allowed to override it. unless param.source.child_of?(current.source) msg = "Parameter '#{param.name}' is already set on #{self}" msg += " by #{current.source}" if current.source.to_s != "" if current.file or current.line fields = [] fields << current.file if current.file fields << current.line.to_s if current.line msg += " at #{fields.join(":")}" end msg += "; cannot redefine" raise Puppet::ParseError.new(msg, param.line, param.file) end # If we've gotten this far, we're allowed to override. # Merge with previous value, if the parameter was generated with the +> # syntax. It's important that we use a copy of the new param instance # here, not the old one, and not the original new one, so that the source # is registered correctly for later overrides but the values aren't # implcitly shared when multiple resources are overrriden at once (see # ticket #3556). if param.add param = param.dup param.value = [current.value, param.value].flatten end set_parameter(param) end # Make sure the resource's parameters are all valid for the type. def validate @parameters.each do |name, param| validate_parameter(name) end rescue => detail self.fail Puppet::ParseError, detail.to_s + " on #{self}", detail end def extract_parameters(params) params.each do |param| # Don't set the same parameter twice self.fail Puppet::ParseError, "Duplicate parameter '#{param.name}' for on #{self}" if @parameters[param.name] set_parameter(param) end end end diff --git a/lib/puppet/pops/evaluator/collector_transformer.rb b/lib/puppet/pops/evaluator/collector_transformer.rb index 2c7499ae6..94319d3dc 100644 --- a/lib/puppet/pops/evaluator/collector_transformer.rb +++ b/lib/puppet/pops/evaluator/collector_transformer.rb @@ -1,208 +1,219 @@ class Puppet::Pops::Evaluator::CollectorTransformer def initialize @@query_visitor ||= Puppet::Pops::Visitor.new(nil, "query", 1, 1) @@match_visitor ||= Puppet::Pops::Visitor.new(nil, "match", 1, 1) @@evaluator ||= Puppet::Pops::Evaluator::EvaluatorImpl.new @@compare_operator ||= Puppet::Pops::Evaluator::CompareOperator.new() end def transform(o, scope) raise ArgumentError, "Expected CollectExpression" unless o.is_a? Puppet::Pops::Model::CollectExpression raise "LHS is not a type" unless o.type_expr.is_a? Puppet::Pops::Model::QualifiedReference type = o.type_expr.value().downcase() if type == 'class' fail "Classes cannot be collected" end resource_type = scope.find_resource_type(type) fail "Resource type #{type} doesn't exist" unless resource_type adapter = Puppet::Pops::Adapters::SourcePosAdapter.adapt(o) line_num = adapter.line position = adapter.pos file_path = adapter.locator.file if !o.operations.empty? overrides = { :parameters => o.operations.map{ |x| to_3x_param(x).evaluate(scope)}, :file => file_path, :line => [line_num, position], :source => scope.source, :scope => scope } end code = query_unless_nop(o.query, scope) case o.query when Puppet::Pops::Model::VirtualQuery newcoll = Puppet::Pops::Evaluator::Collectors::CatalogCollector.new(scope, resource_type.name, code, overrides) when Puppet::Pops::Model::ExportedQuery match = match_unless_nop(o.query, scope) newcoll = Puppet::Pops::Evaluator::Collectors::ExportedCollector.new(scope, resource_type.name, match, code, overrides) end scope.compiler.add_collection(newcoll) newcoll end protected def query(o, scope) @@query_visitor.visit_this_1(self, o, scope) end def match(o, scope) @@match_visitor.visit_this_1(self, o, scope) end def query_unless_nop(query, scope) unless query.expr.nil? || query.expr.is_a?(Puppet::Pops::Model::Nop) query(query.expr, scope) end end def match_unless_nop(query, scope) unless query.expr.nil? || query.expr.is_a?(Puppet::Pops::Model::Nop) match(query.expr, scope) end end def query_AndExpression(o, scope) left_code = query(o.left_expr, scope) right_code = query(o.right_expr, scope) proc do |resource| left_code.call(resource) && right_code.call(resource) end end def query_OrExpression(o, scope) left_code = query(o.left_expr, scope) right_code = query(o.right_expr, scope) proc do |resource| left_code.call(resource) || right_code.call(resource) end end def query_ComparisonExpression(o, scope) left_code = query(o.left_expr, scope) right_code = query(o.right_expr, scope) case o.operator when :'==' if left_code == "tag" + # Ensure that to_s and downcase is done once, i.e. outside the proc block and + # then use raw_tagged? instead of tagged? + if right_code.is_a?(Array) + tags = right_code + else + tags = [ right_code ] + end + tags = tags.collect do |t| + raise ArgumentError, 'Cannot transform a number to a tag' if t.is_a?(Numeric) + t.to_s.downcase + end proc do |resource| - resource.tagged?(right_code) + resource.raw_tagged?(tags) end else proc do |resource| if (tmp = resource[left_code]).is_a?(Array) @@compare_operator.include?(tmp, right_code, scope) else @@compare_operator.equals(tmp, right_code) end end end when :'!=' proc do |resource| !@@compare_operator.equals(resource[left_code], right_code) end end end def query_VariableExpression(o, scope) @@evaluator.evaluate(o, scope) end def query_LiteralBoolean(o, scope) @@evaluator.evaluate(o, scope) end def query_LiteralString(o, scope) @@evaluator.evaluate(o, scope) end def query_ConcatenatedString(o, scope) @@evaluator.evaluate(o, scope) end def query_LiteralNumber(o, scope) @@evaluator.evaluate(o, scope) end def query_QualifiedName(o, scope) @@evaluator.evaluate(o, scope) end def query_ParenthesizedExpression(o, scope) query(o.expr, scope) end def query_Object(o, scope) raise ArgumentError, "Cannot transform object of class #{o.class}" end def match_AndExpression(o, scope) left_match = match(o.left_expr, scope) right_match = match(o.right_expr, scope) return [left_match, 'and', right_match] end def match_OrExpression(o, scope) left_match = match(o.left_expr, scope) right_match = match(o.right_expr, scope) return [left_match, 'or', right_match] end def match_ComparisonExpression(o, scope) left_match = match(o.left_expr, scope) right_match = match(o.right_expr, scope) return [left_match, o.operator.to_s, right_match] end def match_VariableExpression(o, scope) @@evaluator.evaluate(o, scope) end def match_LiteralBoolean(o, scope) @@evaluator.evaluate(o, scope) end def match_LiteralString(o, scope) @@evaluator.evaluate(o, scope) end def match_ConcatenatedString(o, scope) @@evaluator.evaluate(o, scope) end def match_LiteralNumber(o, scope) @@evaluator.evaluate(o, scope) end def match_QualifiedName(o, scope) @@evaluator.evaluate(o, scope) end def match_ParenthesizedExpression(o, scope) match(o.expr, scope) end def match_Object(o, scope) raise ArgumentError, "Cannot transform object of class #{o.class}" end # Produces (name => expr) or (name +> expr) def to_3x_param(o) bridge = Puppet::Parser::AST::PopsBridge::Expression.new(:value => o.value_expr) args = { :value => bridge } args[:add] = true if o.operator == :'+>' args[:param] = o.attribute_name args= Puppet::Pops::Model::AstTransformer.new().merge_location(args, o) Puppet::Parser::AST::ResourceParam.new(args) end end diff --git a/lib/puppet/util/tagging.rb b/lib/puppet/util/tagging.rb index 266029a1f..15c4904e8 100644 --- a/lib/puppet/util/tagging.rb +++ b/lib/puppet/util/tagging.rb @@ -1,56 +1,75 @@ require 'puppet/util/tag_set' module Puppet::Util::Tagging ValidTagRegex = /^\w[-\w:.]*$/ # Add a tag to the current tag set. # When a tag set is used for a scope, these tags will be added to all of # the objects contained in this scope when the objects are finished. # def tag(*ary) @tags ||= new_tags ary.flatten.each do |tag| name = tag.to_s.downcase if name =~ ValidTagRegex @tags << name name.split("::").each do |section| @tags << section end else fail(Puppet::ParseError, "Invalid tag '#{name}'") end end end - # Is the receiver tagged with the given tags? + # Answers if this resource is tagged with at least one of the given tags. + # + # The given tags are converted to downcased strings before the match is performed. + # + # @param *tags [String] splat of tags to look for + # @return [Boolean] true if this instance is tagged with at least one of the provided tags + # def tagged?(*tags) - not ( self.tags & tags.flatten.collect { |t| t.to_s } ).empty? + raw_tagged?(tags.collect {|t| t.to_s.downcase}) + end + + # Answers if this resource is tagged with at least one of the tags given in downcased string form. + # + # The method is a faster variant of the tagged? method that does no conversion of its + # arguments. + # + # @param tag_array [Array[String]] array of tags to look for + # @return [Boolean] true if this instance is tagged with at least one of the provided tags + # + def raw_tagged?(tag_array) + my_tags = self.tags + !tag_array.index { |t| my_tags.include?(t) }.nil? end # Return a copy of the tag list, so someone can't ask for our tags # and then modify them. def tags @tags ||= new_tags @tags.dup end def tags=(tags) @tags = new_tags return if tags.nil? or tags == "" tags = tags.strip.split(/\s*,\s*/) if tags.is_a?(String) tags.each {|t| tag(t) } end private def valid_tag?(tag) tag.is_a?(String) and tag =~ ValidTagRegex end def new_tags Puppet::Util::TagSet.new end end diff --git a/spec/unit/util/tagging_spec.rb b/spec/unit/util/tagging_spec.rb index 53bb39d7a..4b7e1578e 100755 --- a/spec/unit/util/tagging_spec.rb +++ b/spec/unit/util/tagging_spec.rb @@ -1,162 +1,169 @@ #! /usr/bin/env ruby require 'spec_helper' require 'puppet/util/tagging' describe Puppet::Util::Tagging do let(:tagger) { Object.new.extend(Puppet::Util::Tagging) } it "should add tags to the returned tag list" do tagger.tag("one") expect(tagger.tags).to include("one") end it "should return a duplicate of the tag list, rather than the original" do tagger.tag("one") tags = tagger.tags tags << "two" expect(tagger.tags).to_not include("two") end it "should add all provided tags to the tag list" do tagger.tag("one", "two") expect(tagger.tags).to include("one") expect(tagger.tags).to include("two") end it "should fail on tags containing '*' characters" do expect { tagger.tag("bad*tag") }.to raise_error(Puppet::ParseError) end it "should fail on tags starting with '-' characters" do expect { tagger.tag("-badtag") }.to raise_error(Puppet::ParseError) end it "should fail on tags containing ' ' characters" do expect { tagger.tag("bad tag") }.to raise_error(Puppet::ParseError) end it "should allow alpha tags" do expect { tagger.tag("good_tag") }.not_to raise_error end it "should allow tags containing '.' characters" do expect { tagger.tag("good.tag") }.to_not raise_error end it "should add qualified classes as tags" do tagger.tag("one::two") expect(tagger.tags).to include("one::two") end it "should add each part of qualified classes as tags" do tagger.tag("one::two::three") expect(tagger.tags).to include('one') expect(tagger.tags).to include("two") expect(tagger.tags).to include("three") end it "should indicate when the object is tagged with a provided tag" do tagger.tag("one") expect(tagger).to be_tagged("one") end it "should indicate when the object is not tagged with a provided tag" do expect(tagger).to_not be_tagged("one") end it "should indicate when the object is tagged with any tag in an array" do tagger.tag("one") expect(tagger).to be_tagged("one","two","three") end it "should indicate when the object is not tagged with any tag in an array" do tagger.tag("one") expect(tagger).to_not be_tagged("two","three") end context "when tagging" do it "converts symbols to strings" do tagger.tag(:hello) expect(tagger.tags).to include('hello') end it "downcases tags" do tagger.tag(:HEllO) tagger.tag("GooDByE") expect(tagger).to be_tagged("hello") expect(tagger).to be_tagged("goodbye") end + it "downcases tag arguments" do + tagger.tag("hello") + tagger.tag("goodbye") + expect(tagger).to be_tagged(:HEllO) + expect(tagger).to be_tagged("GooDByE") + end + it "accepts hyphenated tags" do tagger.tag("my-tag") expect(tagger).to be_tagged("my-tag") end end context "when querying if tagged" do it "responds true if queried on the entire set" do tagger.tag("one", "two") expect(tagger).to be_tagged("one", "two") end it "responds true if queried on a subset" do tagger.tag("one", "two", "three") expect(tagger).to be_tagged("two", "one") end it "responds true if queried on an overlapping but not fully contained set" do tagger.tag("one", "two") expect(tagger).to be_tagged("zero", "one") end it "responds false if queried on a disjoint set" do tagger.tag("one", "two", "three") expect(tagger).to_not be_tagged("five") end it "responds false if queried on the empty set" do expect(tagger).to_not be_tagged end end context "when assigning tags" do it "splits a string on ','" do tagger.tags = "one, two, three" expect(tagger).to be_tagged("one") expect(tagger).to be_tagged("two") expect(tagger).to be_tagged("three") end it "protects against empty tags" do expect { tagger.tags = "one,,two"}.to raise_error(/Invalid tag ''/) end it "takes an array of tags" do tagger.tags = ["one", "two"] expect(tagger).to be_tagged("one") expect(tagger).to be_tagged("two") end it "removes any existing tags when reassigning" do tagger.tags = "one, two" tagger.tags = "three, four" expect(tagger).to_not be_tagged("one") expect(tagger).to_not be_tagged("two") expect(tagger).to be_tagged("three") expect(tagger).to be_tagged("four") end it "allows empty tags that are generated from :: separated tags" do tagger.tags = "one::::two::three" expect(tagger).to be_tagged("one") expect(tagger).to be_tagged("") expect(tagger).to be_tagged("two") expect(tagger).to be_tagged("three") end end end