summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLuke Kanies <luke@puppetlabs.com>2010-04-17 19:46:23 -0700
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commit8b99367ccedefe0e341a79487ccea2a95f2720a3 (patch)
tree4dca9eeeda8be43006eda6acf0d66aa7a874560e /lib
parent748aed9a4fe70cc2ecc0c782b694114356d9eb25 (diff)
downloadpuppet-8b99367ccedefe0e341a79487ccea2a95f2720a3.tar.gz
puppet-8b99367ccedefe0e341a79487ccea2a95f2720a3.tar.xz
puppet-8b99367ccedefe0e341a79487ccea2a95f2720a3.zip
Adding indirector support to Resource Types
Also adding JSON support. This is so that we can remotely retrieve information about resource types and classes, such as what arguments are required. Signed-off-by: Luke Kanies <luke@puppetlabs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/indirector/resource_type.rb5
-rw-r--r--lib/puppet/indirector/resource_type/parser.rb27
-rw-r--r--lib/puppet/indirector/resource_type/rest.rb7
-rw-r--r--lib/puppet/resource/type.rb34
4 files changed, 73 insertions, 0 deletions
diff --git a/lib/puppet/indirector/resource_type.rb b/lib/puppet/indirector/resource_type.rb
new file mode 100644
index 000000000..0564dc2b3
--- /dev/null
+++ b/lib/puppet/indirector/resource_type.rb
@@ -0,0 +1,5 @@
+require 'puppet/resource/type'
+
+# A stub class, so our constants work.
+class Puppet::Indirector::ResourceType # :nodoc:
+end
diff --git a/lib/puppet/indirector/resource_type/parser.rb b/lib/puppet/indirector/resource_type/parser.rb
new file mode 100644
index 000000000..81ec1bf76
--- /dev/null
+++ b/lib/puppet/indirector/resource_type/parser.rb
@@ -0,0 +1,27 @@
+require 'puppet/resource/type'
+require 'puppet/indirector/code'
+require 'puppet/indirector/resource_type'
+
+class Puppet::Indirector::ResourceType::Parser < Puppet::Indirector::Code
+ desc "Return the data-form of a resource type."
+
+ def find(request)
+ krt = request.environment.known_resource_types
+
+ # This is a bit ugly.
+ [:hostclass, :definition, :node].each do |type|
+ if r = krt.send(type, request.key)
+ return r
+ end
+ end
+ nil
+ end
+
+ def search(request)
+ raise ArgumentError, "Only '*' is acceptable as a search request" unless request.key == "*"
+ krt = request.environment.known_resource_types
+ result = [krt.hostclasses.values, krt.definitions.values, krt.nodes.values].flatten
+ return nil if result.empty?
+ result
+ end
+end
diff --git a/lib/puppet/indirector/resource_type/rest.rb b/lib/puppet/indirector/resource_type/rest.rb
new file mode 100644
index 000000000..66d332011
--- /dev/null
+++ b/lib/puppet/indirector/resource_type/rest.rb
@@ -0,0 +1,7 @@
+require 'puppet/resource/type'
+require 'puppet/indirector/rest'
+require 'puppet/indirector/resource_type'
+
+class Puppet::Indirector::ResourceType::Rest < Puppet::Indirector::REST
+ desc "Retrieve resource types via a REST HTTP interface."
+end
diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb
index 402df06a8..3657f89b5 100644
--- a/lib/puppet/resource/type.rb
+++ b/lib/puppet/resource/type.rb
@@ -6,6 +6,7 @@ require 'puppet/parser/ast/leaf'
require 'puppet/dsl'
class Puppet::Resource::Type
+ Puppet::ResourceType = self
include Puppet::Util::InlineDocs
include Puppet::Util::Warnings
include Puppet::Util::Errors
@@ -19,6 +20,38 @@ class Puppet::Resource::Type
define_method("#{t}?") { self.type == t }
end
+ require 'puppet/indirector'
+ extend Puppet::Indirector
+ indirects :resource_type, :terminus_class => :parser
+
+ def self.from_pson(data)
+ name = data.delete('name') or raise ArgumentError, "Resource Type names must be specified"
+ type = data.delete('type') || "definition"
+
+ data = data.inject({}) { |result, ary| result[ary[0].intern] = ary[1]; result }
+
+ new(type, name, data)
+ end
+
+ def to_pson_data_hash
+ data = [:code, :doc, :line, :file, :parent].inject({}) do |hash, param|
+ next hash unless value = self.send(param)
+ hash[param.to_s] = value
+ hash
+ end
+
+ data['arguments'] = arguments.dup
+
+ data['name'] = name
+ data['type'] = type
+
+ data
+ end
+
+ def to_pson(*args)
+ to_pson_data_hash.to_pson(*args)
+ end
+
# Are we a child of the passed class? Do a recursive search up our
# parentage tree to figure it out.
def child_of?(klass)
@@ -260,3 +293,4 @@ class Puppet::Resource::Type
end
end
end
+