Puppet: System Administration Automated

Support

Ticket #1179: ticket_1179.patch

File ticket_1179.patch, 8.6 kB (added by wrobel, 7 months ago)

Updated patch for stacked parameters

  • a/ext/ldap/puppet.schema

    old new  
    1717        EQUALITY caseIgnoreIA5Match 
    1818        SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 ) 
    1919 
     20attributetype ( 1.1.3.12 NAME 'puppetvar' 
     21        DESC 'A variable setting for puppet' 
     22        EQUALITY caseIgnoreIA5Match 
     23        SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 ) 
     24 
    2025objectclass ( 1.1.1.2 NAME 'puppetClient' SUP top AUXILIARY 
    2126        DESC 'Puppet Client objectclass' 
    22         MAY ( puppetclass $ parentnode $ environment )) 
     27        MAY ( puppetclass $ parentnode $ environment $ puppetvar )) 
  • a/lib/puppet/defaults.rb

    old new  
    637637        :ldapclassattrs => ["puppetclass", 
    638638            "The LDAP attributes to use to define Puppet classes.  Values 
    639639            should be comma-separated."], 
     640        :ldapstackedattrs => ["puppetvar", 
     641            "The LDAP attributes that should be stacked to arrays by adding 
     642            the values in all hierarchy elements of the tree.  Values 
     643            should be comma-separated."], 
    640644        :ldapattrs => ["all", 
    641645            "The LDAP attributes to include when querying LDAP for nodes.  All 
    642646            returned attributes are set as variables in the top-level scope. 
  • a/lib/puppet/indirector/node/ldap.rb

    old new  
    1919 
    2020        node = Puppet::Node.new(name) 
    2121 
     22        information[:stacked_parameters] = {} 
     23 
    2224        parent_info = nil 
    2325        parent = information[:parent] 
    2426        parents = [name] 
     
    3436                raise Puppet::Error.new("Could not find parent node '%s'" % parent) 
    3537            end 
    3638            information[:classes] += parent_info[:classes] 
     39            parent_info[:stacked].each do |value| 
     40                param = value.split('=', 2) 
     41                information[:stacked_parameters][param[0]] = param[1] 
     42            end 
    3743            parent_info[:parameters].each do |param, value| 
    3844                # Specifically test for whether it's set, so false values are handled 
    3945                # correctly. 
     
    4551            parent = parent_info[:parent] 
    4652        end 
    4753 
     54        information[:stacked].each do |value| 
     55            param = value.split('=', 2) 
     56            information[:stacked_parameters][param[0]] = param[1] 
     57        end 
     58 
     59        information[:stacked_parameters].each do |param, value| 
     60            information[:parameters][param] = value unless information[:parameters].include?(param) 
     61        end 
     62 
    4863        node.classes = information[:classes].uniq unless information[:classes].empty? 
    4964        node.parameters = information[:parameters] unless information[:parameters].empty? 
    5065        node.environment = information[:environment] if information[:environment] 
     
    6277        end 
    6378    end 
    6479 
     80    # The attributes that Puppet will stack as array over the full 
     81    # hierarchy. 
     82    def stacked_attributes 
     83        Puppet[:ldapstackedattrs].split(/\s*,\s*/) 
     84    end 
     85 
    6586    # Process the found entry.  We assume that we don't just want the 
    6687    # ldap object. 
    6788    def process(name, entry) 
     
    85106            end 
    86107        } 
    87108 
     109        result[:stacked] = [] 
     110        stacked_attributes.each { |attr| 
     111            if values = entry.vals(attr) 
     112                result[:stacked] = result[:stacked] + values 
     113            end 
     114        } 
     115         
     116 
    88117        result[:parameters] = entry.to_hash.inject({}) do |hash, ary| 
    89118            if ary[1].length == 1 
    90119                hash[ary[0]] = ary[1].shift 
  • a/spec/unit/indirector/node/ldap.rb

    old new  
    1717            @searcher.stubs(:connection).returns(@connection) 
    1818            @searcher.stubs(:class_attributes).returns([]) 
    1919            @searcher.stubs(:parent_attribute).returns(nil) 
     20            @searcher.stubs(:stacked_attributes).returns([]) 
    2021            @searcher.stubs(:search_base).returns(:yay) 
    2122            @searcher.stubs(:search_filter).returns(:filter) 
    2223 
     
    195196                proc { @searcher.find(@request) }.should raise_error(ArgumentError) 
    196197            end 
    197198        end 
     199 
     200        describe "and a puppet variable is specified" do 
     201            before do 
     202                @searcher.stubs(:stacked_attributes).returns(['puppetvar']) 
     203            end 
     204 
     205            it "should add the variable to the node parameters" do 
     206                @entry.stubs(:vals).with("puppetvar").returns(%w{one=two}) 
     207                @entry.stubs(:to_hash).returns({}) 
     208                @node.expects(:parameters=).with("one" => "two") 
     209                @searcher.find(@request) 
     210            end 
     211 
     212            it "should not overwrite node parameters specified as ldap object attribute" do 
     213                @entry.stubs(:vals).with("puppetvar").returns(%w{one=two}) 
     214                @entry.stubs(:to_hash).returns("one" => "three") 
     215                @node.expects(:parameters=).with("one" => "three") 
     216                @searcher.find(@request) 
     217            end 
     218 
     219            it "should set entries without an equal sign to nil" do 
     220                @entry.stubs(:vals).with("puppetvar").returns(%w{one}) 
     221                @entry.stubs(:to_hash).returns({}) 
     222                @node.expects(:parameters=).with("one" => nil) 
     223                @searcher.find(@request) 
     224            end 
     225 
     226            it "should ignore empty entries" do 
     227                @entry.stubs(:vals).with("puppetvar").returns(%w{}) 
     228                @entry.stubs(:to_hash).returns({}) 
     229                @searcher.find(@request) 
     230            end 
     231        end 
     232        describe "and a puppet variable as well as a parent node are specified" do 
     233            before do 
     234                @parent = mock 'parent' 
     235 
     236                @searcher.meta_def(:search_filter) do |name| 
     237                    return name 
     238                end 
     239                @connection.stubs(:search).with { |*args| args[2] == @name              }.yields(@entry) 
     240                @connection.stubs(:search).with { |*args| args[2] == 'parent'           }.yields(@parent) 
     241 
     242                @searcher.stubs(:stacked_attributes).returns(['puppetvar']) 
     243                @searcher.stubs(:parent_attribute).returns(:parent) 
     244            end 
     245 
     246            it "should add parent node variables to the child node parameters" do 
     247                @parent.stubs(:to_hash).returns({}) 
     248                @parent.stubs(:vals).with("puppetvar").returns(%w{one=two}) 
     249                @parent.stubs(:vals).with(:parent).returns(nil) 
     250 
     251                @entry.stubs(:to_hash).returns({}) 
     252                @entry.stubs(:vals).with("puppetvar").returns(%w{}) 
     253                @entry.stubs(:vals).with(:parent).returns(%w{parent}) 
     254 
     255                @node.expects(:parameters=).with("one" => "two") 
     256 
     257                @searcher.find(@request) 
     258            end 
     259 
     260            it "should overwrite parent node variables with child node parameters" do 
     261                @parent.stubs(:to_hash).returns({}) 
     262                @parent.stubs(:vals).with("puppetvar").returns(%w{one=two}) 
     263                @parent.stubs(:vals).with(:parent).returns(nil) 
     264 
     265                @entry.stubs(:to_hash).returns({}) 
     266                @entry.stubs(:vals).with("puppetvar").returns(%w{one=three}) 
     267                @entry.stubs(:vals).with(:parent).returns(%w{parent}) 
     268 
     269                @node.expects(:parameters=).with("one" => "three") 
     270 
     271                @searcher.find(@request) 
     272            end 
     273 
     274            it "should not overwrite parent node parameters specified as ldap object attribute" do 
     275                @parent.stubs(:to_hash).returns("one" => "three") 
     276                @parent.stubs(:vals).with("puppetvar").returns(%w{}) 
     277                @parent.stubs(:vals).with(:parent).returns(nil) 
     278 
     279                @entry.stubs(:vals).with("puppetvar").returns(%w{one=two}) 
     280                @entry.stubs(:to_hash).returns({}) 
     281                @entry.stubs(:vals).with(:parent).returns(%w{parent}) 
     282 
     283                @node.expects(:parameters=).with("one" => "three") 
     284 
     285                @searcher.find(@request) 
     286            end 
     287 
     288        end 
    198289    end 
    199290end 
    200291