summaryrefslogtreecommitdiffstats
path: root/lib/puppet/resource.rb
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-06-02 17:12:38 -0500
committerJames Turnbull <james@lovedthanlost.net>2009-06-06 19:57:58 +1000
commitc0bd0aa1a5aaed94dfab25f390199a722d0d5c0d (patch)
treef337015acb2e729ae8ef687dfe077a45390dd01b /lib/puppet/resource.rb
parentc16fd1bff64fd426d36e07648a7f8550563ae6e1 (diff)
downloadpuppet-c0bd0aa1a5aaed94dfab25f390199a722d0d5c0d.tar.gz
puppet-c0bd0aa1a5aaed94dfab25f390199a722d0d5c0d.tar.xz
puppet-c0bd0aa1a5aaed94dfab25f390199a722d0d5c0d.zip
Providing JSON support to the Resource class
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/resource.rb')
-rw-r--r--lib/puppet/resource.rb72
1 files changed, 69 insertions, 3 deletions
diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb
index b4458e638..862aadf99 100644
--- a/lib/puppet/resource.rb
+++ b/lib/puppet/resource.rb
@@ -1,15 +1,84 @@
require 'puppet'
require 'puppet/util/tagging'
require 'puppet/resource/reference'
+require 'puppet/util/json'
# The simplest resource class. Eventually it will function as the
# base class for all resource-like behaviour.
class Puppet::Resource
include Puppet::Util::Tagging
+ extend Puppet::Util::Json
include Enumerable
attr_accessor :file, :line, :catalog, :exported
attr_writer :type, :title
+ ATTRIBUTES = [:file, :line, :exported]
+
+ def self.from_json(json)
+ raise ArgumentError, "No resource type provided in json data" unless type = json['type']
+ raise ArgumentError, "No resource title provided in json data" unless title = json['title']
+
+ resource = new(type, title)
+
+ if params = json['parameters']
+ params.each { |param, value| resource[param] = value }
+ end
+
+ if tags = json['tags']
+ tags.each { |tag| resource.tag(tag) }
+ end
+
+ ATTRIBUTES.each do |a|
+ if value = json[a.to_s]
+ resource.send(a.to_s + "=", value)
+ end
+ end
+
+ resource.exported ||= false
+
+ resource
+ end
+
+ def to_json(*args)
+ raise "Cannot convert to JSON unless the 'json' library is installed" unless Puppet.features.json?
+
+ data = ([:type, :title, :tags] + ATTRIBUTES).inject({}) do |hash, param|
+ next hash unless value = self.send(param)
+ hash[param.to_s] = value
+ hash
+ end
+
+ data["exported"] ||= false
+
+ params = self.to_hash.inject({}) do |hash, ary|
+ param, value = ary
+
+ # Don't duplicate the title as the namevar
+ next hash if param == namevar and value == title
+ value = [value] unless value.is_a?(Array)
+ hash[param] = value
+ hash
+ end
+
+ unless params.empty?
+ data["parameters"] = params
+ end
+
+ res = {
+ 'json_class' => self.class.name,
+ 'data' => data
+ }
+ #data.each do |key, value|
+ # puts "Converting %s (%s)" % [key, value.inspect]
+ # p value
+ # value.to_json(*args)
+ # key.to_json(*args)
+ #end
+ #puts "Converted all"
+ #p res
+ res.to_json(*args)
+ end
+
# Proxy these methods to the parameters hash. It's likely they'll
# be overridden at some point, but this works for now.
%w{has_key? keys length delete empty? <<}.each do |method|
@@ -83,9 +152,6 @@ class Puppet::Resource
unless result.include?(namevar)
result[namevar] = title
end
- if result.has_key?(nil)
- raise "wtf? %s" % namevar.inspect
- end
result
end