diff options
| author | Luke Kanies <luke@madstop.com> | 2009-06-02 17:34:43 -0500 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-06-06 19:57:58 +1000 |
| commit | 7b33b6da4bdcd2263e2c63b443e9bea6fbe8d161 (patch) | |
| tree | 09b3612762b8b0d6a73a0ca6c0d303b2f9add84f /lib | |
| parent | c0bd0aa1a5aaed94dfab25f390199a722d0d5c0d (diff) | |
| download | puppet-7b33b6da4bdcd2263e2c63b443e9bea6fbe8d161.tar.gz puppet-7b33b6da4bdcd2263e2c63b443e9bea6fbe8d161.tar.xz puppet-7b33b6da4bdcd2263e2c63b443e9bea6fbe8d161.zip | |
Adding JSON support to Catalogs
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/puppet/relationship.rb | 3 | ||||
| -rw-r--r-- | lib/puppet/resource/catalog.rb | 65 |
2 files changed, 68 insertions, 0 deletions
diff --git a/lib/puppet/relationship.rb b/lib/puppet/relationship.rb index 4c44adba7..8efebf1e3 100644 --- a/lib/puppet/relationship.rb +++ b/lib/puppet/relationship.rb @@ -6,10 +6,13 @@ # subscriptions are permanent associations determining how different # objects react to an event +require 'puppet/util/json' + # This is Puppet's class for modeling edges in its configuration graph. # It used to be a subclass of GRATR::Edge, but that class has weird hash # overrides that dramatically slow down the graphing. class Puppet::Relationship + extend Puppet::Util::Json attr_accessor :source, :target, :callback attr_reader :event diff --git a/lib/puppet/resource/catalog.rb b/lib/puppet/resource/catalog.rb index eb42ff690..68e6d7de5 100644 --- a/lib/puppet/resource/catalog.rb +++ b/lib/puppet/resource/catalog.rb @@ -4,6 +4,7 @@ require 'puppet/simple_graph' require 'puppet/transaction' require 'puppet/util/cacher' +require 'puppet/util/json' require 'puppet/util/tagging' @@ -18,6 +19,7 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph indirects :catalog, :terminus_class => :compiler include Puppet::Util::Tagging + extend Puppet::Util::Json include Puppet::Util::Cacher::Expirer # The host name this is a catalog for. @@ -388,6 +390,69 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph @resource_table.keys end + def self.from_json(data) + result = new(data['name']) + + if tags = data['tags'] + result.tag(*tags) + end + + if version = data['version'] + result.version = version + end + + if resources = data['resources'] + resources.each do |res| + resource_from_json(result, res) + end + end + + if edges = data['edges'] + edges.each do |edge| + edge_from_json(result, edge) + end + end + + result + end + + def self.edge_from_json(result, edge) + # If no json_class information was presented, we manually find + # the class. + edge = Puppet::Relationship.from_json(edge) if edge.is_a?(Hash) + unless source = result.resource(edge.source) + raise ArgumentError, "Could not convert from json: Could not find relationship source '%s'" % source + end + edge.source = source + + unless target = result.resource(edge.target) + raise ArgumentError, "Could not convert from json: Could not find relationship target '%s'" % target + end + edge.target = target + + result.add_edge(edge) + end + + def self.resource_from_json(result, res) + # If no json_class information was presented, we manually find + # the class. + res = Puppet::Resource.from_json(res) if res.is_a?(Hash) + result.add_resource(res) + end + + def to_json(*args) + { + 'json_class' => 'Puppet::Resource::Catalog', + 'data' => { + 'tags' => tags, + 'name' => name, + 'version' => version, + 'resources' => vertices.to_json(*args), + 'edges' => edges.to_json(*args) + } + }.to_json(*args) + end + # Convert our catalog into a RAL catalog. def to_ral to_catalog :to_ral |
