diff options
author | Markus Roberts <Markus@reality.com> | 2010-07-09 18:12:17 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-07-09 18:12:17 -0700 |
commit | 3180b9d9b2c844dade1d361326600f7001ec66dd (patch) | |
tree | 98fe7c5ac7eb942aac9c39f019a17b0b3f5a57f4 /lib/puppet/provider/nameservice.rb | |
parent | 543225970225de5697734bfaf0a6eee996802c04 (diff) | |
download | puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.gz puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.xz puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.zip |
Code smell: Two space indentation
Replaced 106806 occurances of ^( +)(.*$) with
The ruby community almost universally (i.e. everyone but Luke, Markus, and the other eleven people
who learned ruby in the 1900s) uses two-space indentation.
3 Examples:
The code:
end
# Tell getopt which arguments are valid
def test_get_getopt_args
element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
becomes:
end
# Tell getopt which arguments are valid
def test_get_getopt_args
element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
The code:
assert_equal(str, val)
assert_instance_of(Float, result)
end
# Now test it with a passed object
becomes:
assert_equal(str, val)
assert_instance_of(Float, result)
end
# Now test it with a passed object
The code:
end
assert_nothing_raised do
klass[:Yay] = "boo"
klass["Cool"] = :yayness
end
becomes:
end
assert_nothing_raised do
klass[:Yay] = "boo"
klass["Cool"] = :yayness
end
Diffstat (limited to 'lib/puppet/provider/nameservice.rb')
-rw-r--r-- | lib/puppet/provider/nameservice.rb | 430 |
1 files changed, 215 insertions, 215 deletions
diff --git a/lib/puppet/provider/nameservice.rb b/lib/puppet/provider/nameservice.rb index 6564a4154..7339b646e 100644 --- a/lib/puppet/provider/nameservice.rb +++ b/lib/puppet/provider/nameservice.rb @@ -4,270 +4,270 @@ require 'puppet' # their backend, but they're pretty similar on the front-end. This class # provides a way for them all to be as similar as possible. class Puppet::Provider::NameService < Puppet::Provider - class << self - def autogen_default(param) - defined?(@autogen_defaults) ? @autogen_defaults[symbolize(param)] : nil - end - - def autogen_defaults(hash) - @autogen_defaults ||= {} - hash.each do |param, value| - @autogen_defaults[symbolize(param)] = value - end - end + class << self + def autogen_default(param) + defined?(@autogen_defaults) ? @autogen_defaults[symbolize(param)] : nil + end - def initvars - @checks = {} - super - end + def autogen_defaults(hash) + @autogen_defaults ||= {} + hash.each do |param, value| + @autogen_defaults[symbolize(param)] = value + end + end - def instances - objects = [] - listbyname do |name| - objects << new(:name => name, :ensure => :present) - end + def initvars + @checks = {} + super + end - objects - end + def instances + objects = [] + listbyname do |name| + objects << new(:name => name, :ensure => :present) + end - def option(name, option) - name = name.intern if name.is_a? String - (defined?(@options) and @options.include? name and @options[name].include? option) ? @options[name][option] : nil - end + objects + end - def options(name, hash) - raise Puppet::DevError, "#{name} is not a valid attribute for #{resource_type.name}" unless resource_type.valid_parameter?(name) - @options ||= {} - @options[name] ||= {} + def option(name, option) + name = name.intern if name.is_a? String + (defined?(@options) and @options.include? name and @options[name].include? option) ? @options[name][option] : nil + end - # Set options individually, so we can call the options method - # multiple times. - hash.each do |param, value| - @options[name][param] = value - end - end + def options(name, hash) + raise Puppet::DevError, "#{name} is not a valid attribute for #{resource_type.name}" unless resource_type.valid_parameter?(name) + @options ||= {} + @options[name] ||= {} - # List everything out by name. Abstracted a bit so that it works - # for both users and groups. - def listbyname - names = [] - Etc.send("set#{section()}ent") - begin - while ent = Etc.send("get#{section()}ent") - names << ent.name - yield ent.name if block_given? - end - ensure - Etc.send("end#{section()}ent") - end - - names - end + # Set options individually, so we can call the options method + # multiple times. + hash.each do |param, value| + @options[name][param] = value + end + end - def resource_type=(resource_type) - super - @resource_type.validproperties.each do |prop| - next if prop == :ensure - define_method(prop) { get(prop) || :absent} unless public_method_defined?(prop) - define_method(prop.to_s + "=") { |*vals| set(prop, *vals) } unless public_method_defined?(prop.to_s + "=") - end + # List everything out by name. Abstracted a bit so that it works + # for both users and groups. + def listbyname + names = [] + Etc.send("set#{section()}ent") + begin + while ent = Etc.send("get#{section()}ent") + names << ent.name + yield ent.name if block_given? end + ensure + Etc.send("end#{section()}ent") + end - # This is annoying, but there really aren't that many options, - # and this *is* built into Ruby. - def section - unless defined?(@resource_type) - raise Puppet::DevError, - "Cannot determine Etc section without a resource type" - - end - - if @resource_type.name == :group - "gr" - else - "pw" - end - end + names + end - def validate(name, value) - name = name.intern if name.is_a? String - if @checks.include? name - block = @checks[name][:block] - raise ArgumentError, "Invalid value #{value}: #{@checks[name][:error]}" unless block.call(value) - end - end + def resource_type=(resource_type) + super + @resource_type.validproperties.each do |prop| + next if prop == :ensure + define_method(prop) { get(prop) || :absent} unless public_method_defined?(prop) + define_method(prop.to_s + "=") { |*vals| set(prop, *vals) } unless public_method_defined?(prop.to_s + "=") + end + end - def verify(name, error, &block) - name = name.intern if name.is_a? String - @checks[name] = {:error => error, :block => block} - end + # This is annoying, but there really aren't that many options, + # and this *is* built into Ruby. + def section + unless defined?(@resource_type) + raise Puppet::DevError, + "Cannot determine Etc section without a resource type" - private + end - def op(property) - @ops[property.name] || ("-#{property.name}") - end + if @resource_type.name == :group + "gr" + else + "pw" + end end - # Autogenerate a value. Mostly used for uid/gid, but also used heavily - # with DirectoryServices, because DirectoryServices is stupid. - def autogen(field) - field = symbolize(field) - id_generators = {:user => :uid, :group => :gid} - if id_generators[@resource.class.name] == field - return autogen_id(field) - else - if value = self.class.autogen_default(field) - return value - elsif respond_to?("autogen_#{field}") - return send("autogen_#{field}") - else - return nil - end - end + def validate(name, value) + name = name.intern if name.is_a? String + if @checks.include? name + block = @checks[name][:block] + raise ArgumentError, "Invalid value #{value}: #{@checks[name][:error]}" unless block.call(value) + 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 - else - raise Puppet::DevError, "Invalid resource name #{resource}" - end + def verify(name, error, &block) + name = name.intern if name.is_a? String + @checks[name] = {:error => error, :block => block} + 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 - } - - @@prevauto = highest + 1 - end + private - @@prevauto + def op(property) + @ops[property.name] || ("-#{property.name}") + end + end + + # Autogenerate a value. Mostly used for uid/gid, but also used heavily + # with DirectoryServices, because DirectoryServices is stupid. + def autogen(field) + field = symbolize(field) + id_generators = {:user => :uid, :group => :gid} + if id_generators[@resource.class.name] == field + return autogen_id(field) + else + if value = self.class.autogen_default(field) + return value + elsif respond_to?("autogen_#{field}") + return send("autogen_#{field}") + else + 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 + else + raise Puppet::DevError, "Invalid resource name #{resource}" end - def create - if exists? - info "already exists" - # The object already exists - return nil + # 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 + } - begin - execute(self.addcmd) - rescue Puppet::ExecutionFailure => detail - raise Puppet::Error, "Could not create #{@resource.class.name} #{@resource.name}: #{detail}" - end + @@prevauto = highest + 1 end - def delete - unless exists? - info "already absent" - # the object already doesn't exist - return nil - end + @@prevauto + end - begin - execute(self.deletecmd) - rescue Puppet::ExecutionFailure => detail - raise Puppet::Error, "Could not delete #{@resource.class.name} #{@resource.name}: #{detail}" - end + def create + if exists? + info "already exists" + # The object already exists + return nil end - def ensure - if exists? - :present - else - :absent - end + begin + execute(self.addcmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not create #{@resource.class.name} #{@resource.name}: #{detail}" end + end - # Does our object exist? - def exists? - !!getinfo(true) + def delete + unless exists? + info "already absent" + # the object already doesn't exist + return nil end - # Retrieve a specific value by name. - def get(param) - (hash = getinfo(false)) ? hash[param] : nil + begin + execute(self.deletecmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not delete #{@resource.class.name} #{@resource.name}: #{detail}" end + end - # Retrieve what we can about our object - def getinfo(refresh) - if @objectinfo.nil? or refresh == true - @etcmethod ||= ("get" + self.class.section.to_s + "nam").intern - begin - @objectinfo = Etc.send(@etcmethod, @resource[:name]) - rescue ArgumentError => detail - @objectinfo = nil - end - end - - # Now convert our Etc struct into a hash. - @objectinfo ? info2hash(@objectinfo) : nil + def ensure + if exists? + :present + else + :absent + end + end + + # Does our object exist? + def exists? + !!getinfo(true) + end + + # Retrieve a specific value by name. + def get(param) + (hash = getinfo(false)) ? hash[param] : nil + end + + # Retrieve what we can about our object + def getinfo(refresh) + if @objectinfo.nil? or refresh == true + @etcmethod ||= ("get" + self.class.section.to_s + "nam").intern + begin + @objectinfo = Etc.send(@etcmethod, @resource[:name]) + rescue ArgumentError => detail + @objectinfo = nil + end end - # The list of all groups the user is a member of. Different - # user mgmt systems will need to override this method. - def groups - groups = [] - - # Reset our group list - Etc.setgrent + # Now convert our Etc struct into a hash. + @objectinfo ? info2hash(@objectinfo) : nil + end - user = @resource[:name] + # The list of all groups the user is a member of. Different + # user mgmt systems will need to override this method. + def groups + groups = [] - # Now iterate across all of the groups, adding each one our - # user is a member of - while group = Etc.getgrent - members = group.mem + # Reset our group list + Etc.setgrent - groups << group.name if members.include? user - end + user = @resource[:name] - # We have to close the file, so each listing is a separate - # reading of the file. - Etc.endgrent + # Now iterate across all of the groups, adding each one our + # user is a member of + while group = Etc.getgrent + members = group.mem - groups.join(",") + groups << group.name if members.include? user end - # Convert the Etc struct into a hash. - def info2hash(info) - hash = {} - self.class.resource_type.validproperties.each do |param| - method = posixmethod(param) - hash[param] = info.send(posixmethod(param)) if info.respond_to? method - end + # We have to close the file, so each listing is a separate + # reading of the file. + Etc.endgrent - hash + groups.join(",") + end + + # Convert the Etc struct into a hash. + def info2hash(info) + hash = {} + self.class.resource_type.validproperties.each do |param| + method = posixmethod(param) + hash[param] = info.send(posixmethod(param)) if info.respond_to? method end - def initialize(resource) - super + hash + end - @objectinfo = nil - end + def initialize(resource) + super - def set(param, value) - self.class.validate(param, value) - cmd = modifycmd(param, value) - raise Puppet::DevError, "Nameservice command must be an array" unless cmd.is_a?(Array) - begin - execute(cmd) - rescue Puppet::ExecutionFailure => detail - raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}" - end + @objectinfo = nil + end + + def set(param, value) + self.class.validate(param, value) + cmd = modifycmd(param, value) + raise Puppet::DevError, "Nameservice command must be an array" unless cmd.is_a?(Array) + begin + execute(cmd) + rescue Puppet::ExecutionFailure => detail + raise Puppet::Error, "Could not set #{param} on #{@resource.class.name}[#{@resource.name}]: #{detail}" end + end end |