summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-01-13 20:55:13 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-01-13 20:55:13 +0000
commita6e367e5989d0646c2f53c2de839893be76988cb (patch)
tree188239cf15caa5f0a31caf32c8f68e86159b8ce5
parentc8b64014f1018dd437d1a0bd17db96b312e8af8a (diff)
downloadpuppet-a6e367e5989d0646c2f53c2de839893be76988cb.tar.gz
puppet-a6e367e5989d0646c2f53c2de839893be76988cb.tar.xz
puppet-a6e367e5989d0646c2f53c2de839893be76988cb.zip
Changing host and port aliases to also create Puppet aliases. This involved futzing around with the attr* methods in Type.rb, to make sure states are always checked first.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@823 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet/type.rb40
-rwxr-xr-xlib/puppet/type/parsedtype/host.rb21
-rwxr-xr-xlib/puppet/type/parsedtype/port.rb16
-rwxr-xr-xtest/types/host.rb21
-rwxr-xr-xtest/types/port.rb4
5 files changed, 71 insertions, 31 deletions
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index 9c5f17ac9..0e4c1425d 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -196,17 +196,21 @@ class Type < Puppet::Element
# the objects multiple times when iterating over them.
def self.alias(name, obj)
if @objects.include?(name)
- raise Puppet::Error.new(
- "Cannot create alias %s: object already exists" %
- [name]
- )
+ unless @objects[name] == obj
+ raise Puppet::Error.new(
+ "Cannot create alias %s: object already exists" %
+ [name]
+ )
+ end
end
if @aliases.include?(name)
- raise Puppet::Error.new(
- "Object %s already has alias %s" %
- [@aliases[name].name, name]
- )
+ unless @aliases[name] == obj
+ raise Puppet::Error.new(
+ "Object %s already has alias %s" %
+ [@aliases[name].name, name]
+ )
+ end
end
@aliases[name] = obj
@@ -263,6 +267,9 @@ class Type < Puppet::Element
if defined? @objects
@objects.clear
end
+ if defined? @aliases
+ @aliases.clear
+ end
end
# remove a specified object
@@ -484,9 +491,9 @@ class Type < Puppet::Element
# Find the class associated with any given attribute.
def self.attrclass(name)
case self.attrtype(name)
- when :param: @paramhash[name]
- when :meta: @@metaparamhash[name]
when :state: @validstates[name]
+ when :meta: @@metaparamhash[name]
+ when :param: @paramhash[name]
end
end
@@ -546,9 +553,9 @@ class Type < Puppet::Element
# What type of parameter are we dealing with?
def self.attrtype(name)
case
- when @paramhash.include?(name): return :param
- when @@metaparamhash.include?(name): return :meta
when @validstates.include?(name): return :state
+ when @@metaparamhash.include?(name): return :meta
+ when @paramhash.include?(name): return :param
else
raise Puppet::DevError, "Invalid attribute '%s' for class '%s'" %
[name, self.name]
@@ -644,9 +651,8 @@ class Type < Puppet::Element
raise Puppet::Error.new("Got nil value for %s" % name)
end
- if Puppet::Type.metaparam?(name)
- self.newmetaparam(self.class.metaparamclass(name), value)
- elsif stateklass = self.class.validstate?(name)
+ case self.class.attrtype(name)
+ when :state
if value.is_a?(Puppet::State)
self.debug "'%s' got handed a state for '%s'" % [self,name]
@states[name] = value
@@ -662,7 +668,9 @@ class Type < Puppet::Element
end
end
end
- elsif self.class.validparameter?(name)
+ when :meta
+ self.newmetaparam(self.class.metaparamclass(name), value)
+ when :param
# if they've got a method to handle the parameter, then do it that way
self.newparam(self.class.attrclass(name), value)
else
diff --git a/lib/puppet/type/parsedtype/host.rb b/lib/puppet/type/parsedtype/host.rb
index 69c1dbe06..93d0a9002 100755
--- a/lib/puppet/type/parsedtype/host.rb
+++ b/lib/puppet/type/parsedtype/host.rb
@@ -9,9 +9,12 @@ module Puppet
desc "The host's IP address."
end
- newstate(:aliases) do
- desc "Any aliases the host might have. Multiple values must be
- specified as an array."
+ newstate(:alias) do
+ desc "Any alias the host might have. Multiple values must be
+ specified as an array. Note that this state has the same name
+ as one of the metaparams; using this state to set aliases will
+ make those aliases available in your Puppet scripts and also on
+ disk."
# We have to override the feeding mechanism; it might be nil or
# white-space separated
@@ -36,6 +39,12 @@ module Puppet
raise Puppet::Error, "Aliases cannot include whitespace"
end
end
+
+ munge do |value|
+ # Add the :alias metaparam in addition to the state
+ @parent.newmetaparam(@parent.class.metaparamclass(:alias), value)
+ value
+ end
end
newparam(:name) do
@@ -51,7 +60,7 @@ module Puppet
@instances = []
@path = "/etc/hosts"
- @fields = [:ip, :name, :aliases]
+ @fields = [:ip, :name, :alias]
@filetype = Puppet::FileType.filetype(:flat)
# case Facter["operatingsystem"].value
@@ -83,8 +92,8 @@ module Puppet
raise Puppet::Error, "Could not match '%s'" % line
end
- if hash[:aliases] == ""
- hash.delete(:aliases)
+ if hash[:alias] == ""
+ hash.delete(:alias)
end
hash2obj(hash)
diff --git a/lib/puppet/type/parsedtype/port.rb b/lib/puppet/type/parsedtype/port.rb
index 65549d7da..be63c11be 100755
--- a/lib/puppet/type/parsedtype/port.rb
+++ b/lib/puppet/type/parsedtype/port.rb
@@ -44,9 +44,11 @@ module Puppet
desc "The port description."
end
- newstate(:aliases) do
+ newstate(:alias) do
desc "Any aliases the port might have. Multiple values must be specified
- as an array."
+ as an array. Note that this state has the same name as one of the
+ metaparams; using this state to set aliases will make those aliases
+ available in your Puppet scripts and also on disk."
# We have to override the feeding mechanism; it might be nil or
# white-space separated
@@ -71,6 +73,12 @@ module Puppet
raise Puppet::Error, "Aliases cannot have whitespace in them"
end
end
+
+ munge do |value|
+ # Add the :alias metaparam in addition to the state
+ @parent.newmetaparam(@parent.class.metaparamclass(:alias), value)
+ value
+ end
end
newparam(:name) do
@@ -84,7 +92,7 @@ module Puppet
will have different solutions."
@path = "/etc/services"
- @fields = [:ip, :name, :aliases]
+ @fields = [:ip, :name, :alias]
@filetype = Puppet::FileType.filetype(:flat)
# case Facter["operatingsystem"].value
@@ -121,7 +129,7 @@ module Puppet
line.sub!(/^([^#]+)\s*/) do |value|
aliases = $1
unless aliases =~ /^\s*$/
- hash[:aliases] = aliases
+ hash[:alias] = aliases
end
""
diff --git a/test/types/host.rb b/test/types/host.rb
index 4c3c6d142..b79c38e16 100755
--- a/test/types/host.rb
+++ b/test/types/host.rb
@@ -38,7 +38,7 @@ class TestHost < Test::Unit::TestCase
host = Puppet.type(:host).create(
:name => "culain",
:ip => "192.168.0.3",
- :aliases => "puppet"
+ :alias => "puppet"
)
}
@@ -90,17 +90,32 @@ class TestHost < Test::Unit::TestCase
# This was a hard bug to track down.
assert_instance_of(String, host.is(:ip))
- host[:aliases] = %w{madstop kirby yayness}
+ host[:alias] = %w{madstop kirby yayness}
assert_events([:host_changed], host)
end
+ def test_aliasisstate
+ assert_equal(:state, @hosttype.attrtype(:alias))
+ end
+
def test_multivalues
host = mkhost
assert_raise(Puppet::Error) {
- host[:aliases] = "puppetmasterd yayness"
+ host[:alias] = "puppetmasterd yayness"
}
end
+
+ def test_puppetalias
+ host = mkhost()
+
+ assert_nothing_raised {
+ host[:alias] = "testing"
+ }
+
+ same = host.class["testing"]
+ assert(same, "Could not retrieve by alias")
+ end
end
# $Id$
diff --git a/test/types/port.rb b/test/types/port.rb
index 74a445a94..688a0b622 100755
--- a/test/types/port.rb
+++ b/test/types/port.rb
@@ -40,7 +40,7 @@ class TestPort < Test::Unit::TestCase
:number => 8139,
:protocols => "tcp",
:description => "The port that Puppet runs on",
- :aliases => "coolness"
+ :alias => "coolness"
)
}
@@ -117,7 +117,7 @@ class TestPort < Test::Unit::TestCase
port[:protocols] = "udp tcp"
}
assert_raise(Puppet::Error) {
- port[:aliases] = "puppetmasterd yayness"
+ port[:alias] = "puppetmasterd yayness"
}
end
end