summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/code/snippets/aliastest.pp16
-rw-r--r--lib/puppet/parser/ast/objectdef.rb4
-rw-r--r--lib/puppet/parser/scope.rb1
-rw-r--r--lib/puppet/sslcertificates/ca.rb2
-rw-r--r--lib/puppet/transportable.rb63
-rw-r--r--lib/puppet/type.rb93
-rw-r--r--lib/puppet/type/pfile.rb6
-rwxr-xr-xlib/puppet/type/pfile/checksum.rb1
-rwxr-xr-xtest/language/ast.rb12
-rwxr-xr-xtest/language/snippets.rb6
-rw-r--r--test/puppettest.rb8
-rw-r--r--test/types/type.rb25
12 files changed, 186 insertions, 51 deletions
diff --git a/examples/code/snippets/aliastest.pp b/examples/code/snippets/aliastest.pp
new file mode 100644
index 000000000..2a8fc9cb9
--- /dev/null
+++ b/examples/code/snippets/aliastest.pp
@@ -0,0 +1,16 @@
+file { "a file":
+ path => "/tmp/aliastest",
+ ensure => file
+}
+
+file { "another":
+ path => "/tmp/aliastest2",
+ ensure => file,
+ require => file["a file"]
+}
+
+file { "a third":
+ path => "/tmp/aliastest3",
+ ensure => file,
+ require => file["/tmp/aliastest"]
+}
diff --git a/lib/puppet/parser/ast/objectdef.rb b/lib/puppet/parser/ast/objectdef.rb
index 8b40bda71..cf1525540 100644
--- a/lib/puppet/parser/ast/objectdef.rb
+++ b/lib/puppet/parser/ast/objectdef.rb
@@ -21,10 +21,10 @@ class Puppet::Parser::AST
when Puppet::Type:
raise Puppet::Error,
"Built-in types must be provided with a name"
- when HostClass:
+ when Node:
return type
else
- Puppet.info "Autogenerating name for object of type %s" %
+ Puppet.debug "Autogenerating name for object of type %s" %
type
return [type, "-", self.object_id].join("")
end
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index 3e88490e3..d05ccac03 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -727,6 +727,7 @@ module Puppet
if defined? @name
bucket = TransBucket.new
bucket.name = @name
+ bucket.autoname = self.autoname
# it'd be nice not to have to do this...
results.each { |result|
diff --git a/lib/puppet/sslcertificates/ca.rb b/lib/puppet/sslcertificates/ca.rb
index a3cd376fc..901b098bf 100644
--- a/lib/puppet/sslcertificates/ca.rb
+++ b/lib/puppet/sslcertificates/ca.rb
@@ -69,12 +69,10 @@ class Puppet::SSLCertificates::CA
#puts "Reading %s" % Puppet[:capass]
#system "ls -al %s" % Puppet[:capass]
#File.read Puppet[:capass]
- Puppet.info "Getting pass"
@config[:password] = self.getpass
else
# Don't create a password if the cert already exists
unless FileTest.exists?(@config[:cacert])
- Puppet.info "Genning pass"
@config[:password] = self.genpass
end
end
diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb
index b9dedfe9e..4bc54e4b8 100644
--- a/lib/puppet/transportable.rb
+++ b/lib/puppet/transportable.rb
@@ -25,13 +25,13 @@ module Puppet
def initialize(name,type)
@type = type
@name = name
- @params = {"name" => name}
+ @params = {}
#self.class.add(self)
@tags = []
end
def longname
- return [self.type,self[:name]].join('--')
+ return [@type,@name].join('--')
end
def tags
@@ -43,7 +43,7 @@ module Puppet
end
def to_s
- return "%s(%s) => %s" % [@type,self[:name],super]
+ return "%s(%s) => %s" % [@type,@name,super]
end
def to_manifest
@@ -164,24 +164,51 @@ module Puppet
unless defined? @type
Puppet.debug "TransBucket '%s' has no type" % @name
end
- hash = {
- :name => @name,
- :type => @type
- }
- if defined? @parameters
- @parameters.each { |param,value|
- Puppet.debug "Defining %s on %s of type %s" %
- [param,@name,@type]
- hash[param] = value
- }
+ usetrans = true
+
+ if usetrans
+ name = nil
+ #if self.autoname or @name =~ /-\d+$/
+ if self.autoname
+ name = @type
+ else
+ name = "%s[%s]" % [@type, @name]
+ end
+ trans = TransObject.new(name, :component)
+ if defined? @parameters
+ @parameters.each { |param,value|
+ Puppet.debug "Defining %s on %s of type %s" %
+ [param,@name,@type]
+ trans[param] = value
+ }
+ else
+ #Puppet.debug "%s[%s] has no parameters" % [@type, @name]
+ end
+
+ if parent
+ trans[:parent] = parent
+ end
+ container = Puppet.type(:component).create(trans)
else
- #Puppet.debug "%s[%s] has no parameters" % [@type, @name]
- end
+ hash = {
+ :name => @name,
+ :type => @type
+ }
+ if defined? @parameters
+ @parameters.each { |param,value|
+ Puppet.debug "Defining %s on %s of type %s" %
+ [param,@name,@type]
+ hash[param] = value
+ }
+ else
+ #Puppet.debug "%s[%s] has no parameters" % [@type, @name]
+ end
- if parent
- hash[:parent] = parent
+ if parent
+ hash[:parent] = parent
+ end
+ container = Puppet.type(:component).create(hash)
end
- container = Puppet.type(:component).create(hash)
#Puppet.info container.inspect
if parent
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index e38fbd690..df001bc7d 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -1081,11 +1081,13 @@ class Type < Puppet::Element
end
name = nil
- unless name = hash["name"] || hash[:name] ||
- hash[self.namevar] || hash[self.namevar.to_s]
- raise Puppet::Error, "You must specify a name for objects of type %s" %
- self.to_s
+ unless hash.is_a? TransObject
+ # if it's not a transobject, then make it one, just to make people's
+ # lives easier
+ hash = self.hash2trans(hash)
end
+ name = hash.name
+
# if the object already exists
if self.isomorphic? and retobj = self[name]
# if only one of our objects is implicit, then it's easy to see
@@ -1132,9 +1134,51 @@ class Type < Puppet::Element
obj.implicit = true
end
+ # Store the object by name
+ self[obj.name] = obj
+
+ if name != obj[self.namevar]
+ self.alias(obj[self.namevar], obj)
+ end
+
return obj
end
+ # Convert a hash to a TransObject.
+ def self.hash2trans(hash)
+ name = nil
+ ["name", :name, self.namevar, self.namevar.to_s].each { |param|
+ if hash.include? param
+ name = hash[param]
+ hash.delete(param)
+ end
+ }
+ unless name
+ raise Puppet::Error,
+ "You must specify a name for objects of type %s" % self.to_s
+ end
+
+ [:type, "type"].each do |type|
+ if hash.include? type
+ unless self.validattr? :type
+ hash.delete type
+ end
+ end
+ end
+ # okay, now make a transobject out of hash
+ begin
+ trans = TransObject.new(name, self.name.to_s)
+ hash.each { |param, value|
+ trans[param] = value
+ }
+ rescue => detail
+ raise Puppet::Error, "Could not create %s: %s" %
+ [name, detail]
+ end
+
+ return trans
+ end
+
def self.implicitcreate(hash)
unless hash.include?(:implicit)
hash[:implicit] = true
@@ -1206,9 +1250,10 @@ class Type < Puppet::Element
unless defined? @inited
self.initvars
end
+ namevar = self.class.namevar
# If we got passed a transportable object, we just pull a bunch of info
- # directly from it.
+ # directly from it. This is the main object instantiation mechanism.
if hash.is_a?(Puppet::TransObject)
#self[:name] = hash[:name]
[:file, :line, :tags].each { |getter|
@@ -1219,6 +1264,15 @@ class Type < Puppet::Element
end
end
}
+
+ @name = hash.name
+
+ # If they did not provide a namevar,
+ if hash.include? namevar
+ self[:alias] = hash.name
+ else
+ hash[namevar] = hash.name
+ end
hash = hash.to_hash
end
@@ -1235,7 +1289,6 @@ class Type < Puppet::Element
# we have the name but before anything else
attrs = self.class.allattrs
- namevar = self.class.namevar
if hash.include?(namevar)
#self.send(namevar.to_s + "=", hash[namevar])
@@ -1281,10 +1334,6 @@ class Type < Puppet::Element
[self.class.name, hash.keys.join(" ")])
end
- # add this object to the specific class's list of objects
- #puts caller
- self.class[self.name] = self
-
if self.respond_to?(:validate)
self.validate
end
@@ -1518,18 +1567,18 @@ class Type < Puppet::Element
# if they're not using :name for the namevar but we got :name (probably
# from the parser)
- if namevar != :name and hash.include?(:name) and ! hash[:name].nil?
- #self[namevar] = hash[:name]
- hash[namevar] = hash[:name]
- hash.delete(:name)
- # else if we got the namevar
- elsif hash.has_key?(namevar) and ! hash[namevar].nil?
- #self[namevar] = hash[namevar]
- #hash.delete(namevar)
- # else something's screwy
- else
- # they didn't specify anything related to names
- end
+# if namevar != :name and hash.include?(:name) and ! hash[:name].nil?
+# #self[namevar] = hash[:name]
+# hash[namevar] = hash[:name]
+# hash.delete(:name)
+# # else if we got the namevar
+# elsif hash.has_key?(namevar) and ! hash[namevar].nil?
+# #self[namevar] = hash[namevar]
+# #hash.delete(namevar)
+# # else something's screwy
+# else
+# # they didn't specify anything related to names
+# end
return hash
end
diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb
index d4779e428..78b108283 100644
--- a/lib/puppet/type/pfile.rb
+++ b/lib/puppet/type/pfile.rb
@@ -22,6 +22,12 @@ module Puppet
newparam(:path) do
desc "The path to the file to manage. Must be fully qualified."
isnamevar
+
+ validate do |value|
+ unless value =~ /^#{File::SEPARATOR}/
+ raise Puppet::Error, "File paths must be fully qualified"
+ end
+ end
end
newparam(:backup) do
diff --git a/lib/puppet/type/pfile/checksum.rb b/lib/puppet/type/pfile/checksum.rb
index 33ef14d9b..f237f2b3b 100755
--- a/lib/puppet/type/pfile/checksum.rb
+++ b/lib/puppet/type/pfile/checksum.rb
@@ -181,7 +181,6 @@ module Puppet
# out of sync. We don't want to generate an event the first
# time we get a sum.
if @should == [:nosum]
- self.warning "updatingness from %s" % @should.inspect
@should = [@is]
# FIXME we should support an updatechecksums-like mechanism
self.updatesum
diff --git a/test/language/ast.rb b/test/language/ast.rb
index b5497eb0d..0dfb0d56c 100755
--- a/test/language/ast.rb
+++ b/test/language/ast.rb
@@ -293,11 +293,11 @@ class TestAST < Test::Unit::TestCase
# And now verify that we got both the top and node objects
assert_nothing_raised("Could not find top-declared object") {
- assert_equal("/testing", objects[0]["name"])
+ assert_equal("/testing", objects[0].name)
}
assert_nothing_raised("Could not find node-declared object") {
- assert_equal("/%s" % name, objects[1][0]["name"])
+ assert_equal("/%s" % name, objects[1][0].name)
}
end
@@ -338,7 +338,7 @@ class TestAST < Test::Unit::TestCase
assert(objects, "Could not retrieve objects")
assert_nothing_raised("Could not find top-declared object") {
- assert_equal("/%s" % klassname, objects[0][0]["name"])
+ assert_equal("/%s" % klassname, objects[0][0].name)
}
end
@@ -395,19 +395,19 @@ class TestAST < Test::Unit::TestCase
# And now verify that we got the subnode file
assert_nothing_raised("Could not find basenode file") {
base = inner[0]
- assert_equal("/basenode", base["name"])
+ assert_equal("/basenode", base.name)
}
# and the parent node file
assert_nothing_raised("Could not find subnode file") {
sub = inner[1]
- assert_equal("/subnode", sub["name"])
+ assert_equal("/subnode", sub.name)
}
inner.each { |obj|
%w{basenode subnode}.each { |tag|
assert(obj.tags.include?(tag),
- "%s did not include %s tag" % [obj["name"], tag]
+ "%s did not include %s tag" % [obj.name, tag]
)
}
}
diff --git a/test/language/snippets.rb b/test/language/snippets.rb
index 5f154cd0a..a7d7b390c 100755
--- a/test/language/snippets.rb
+++ b/test/language/snippets.rb
@@ -394,6 +394,12 @@ class TestSnippets < Test::Unit::TestCase
}
end
+ def snippet_aliastest(trans)
+ %w{/tmp/aliastest /tmp/aliastest2 /tmp/aliastest3}.each { |file|
+ assert(FileTest.file?(file), "File %s does not exist" % file)
+ }
+ end
+
def snippet_emptyclass(trans)
# There's nothing to check other than that it works
end
diff --git a/test/puppettest.rb b/test/puppettest.rb
index 012279d4a..4d44332bb 100644
--- a/test/puppettest.rb
+++ b/test/puppettest.rb
@@ -57,6 +57,14 @@ module TestPuppet
Puppet[:ignoreschedules] = true
end
+ def newobj(type, name, hash)
+ transport = Puppet::TransObject.new(name, "file")
+ transport[:path] = path
+ transport[:ensure] = "file"
+ assert_nothing_raised {
+ file = transport.to_type
+ }
+ end
def spin
# Just disable spin, unless we really need it
diff --git a/test/types/type.rb b/test/types/type.rb
index 7d087b30b..e7808228a 100644
--- a/test/types/type.rb
+++ b/test/types/type.rb
@@ -186,6 +186,31 @@ class TestType < Test::Unit::TestCase
assert(twoobj.requires?(oneobj), "Requirement was not created")
end
+
+ # Verify that names are aliases, not equivalents
+ def test_nameasalias
+ file = nil
+ path = tempfile()
+ name = "a test file"
+ transport = Puppet::TransObject.new(name, "file")
+ transport[:path] = path
+ transport[:ensure] = "file"
+ assert_nothing_raised {
+ file = transport.to_type
+ }
+
+ assert_equal(path, file[:path])
+ assert_equal([name], file[:alias])
+
+ assert_nothing_raised {
+ file.retrieve
+ }
+
+ assert_apply(file)
+
+ assert(Puppet.type(:file)[name], "Could not look up object by name")
+ #assert(Puppet.type(:file)[path], "Could not look up object by path")
+ end
end
# $Id$