lib/puppet/provider/nameservice.rb in puppet-2.7.26 vs lib/puppet/provider/nameservice.rb in puppet-3.0.0.rc4

- old
+ new

@@ -113,11 +113,11 @@ # with DirectoryServices, because DirectoryServices is stupid. def autogen(field) field = field.intern id_generators = {:user => :uid, :group => :gid} if id_generators[@resource.class.name] == field - return autogen_id(field) + return self.class.autogen_id(field, @resource.class.name) else if value = self.class.autogen_default(field) return value elsif respond_to?("autogen_#{field}") return send("autogen_#{field}") @@ -125,36 +125,36 @@ return nil end end end - # Autogenerate either a uid or a gid. This is hard-coded: we can only - # generate one field type per class. - def autogen_id(field) - highest = 0 - - group = method = nil - case @resource.class.name - when :user; group = :passwd; method = :uid - when :group; group = :group; method = :gid + # Autogenerate either a uid or a gid. This is not very flexible: we can + # only generate one field type per class, and get kind of confused if asked + # for both. + def self.autogen_id(field, resource_type) + # Figure out what sort of value we want to generate. + case resource_type + when :user; database = :passwd; method = :uid + when :group; database = :group; method = :gid else raise Puppet::DevError, "Invalid resource name #{resource}" end - # Make sure we don't use the same value multiple times - if defined?(@@prevauto) - @@prevauto += 1 - else - Etc.send(group) { |obj| - if obj.gid > highest - highest = obj.send(method) unless obj.send(method) > 65000 - end - } + # Initialize from the data set, if needed. + unless @prevauto + # Sadly, Etc doesn't return an enumerator, it just invokes the block + # given, or returns the first record from the database. There is no + # other, more convenient enumerator for these, so we fake one with this + # loop. Thanks, Ruby, for your awesome abstractions. --daniel 2012-03-23 + highest = [] + Etc.send(database) {|entry| highest << entry.send(method) } + highest = highest.reject {|x| x > 65000 }.max - @@prevauto = highest + 1 + @prevauto = highest || 1000 end - @@prevauto + # ...and finally increment and return the next value. + @prevauto += 1 end def create if exists? info "already exists"