From 1cbe2ad70eddbf00ef2d3127a9ffcc9f220ee277 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 12 Apr 2011 21:08:57 -0700 Subject: (7080) Adding json support to Indirector Request We'll be using this to do RPC over mcollective. Reviewed-by: Daniel Pittman Reviewed-by: Nick Lewis Signed-off-by: Luke Kanies --- lib/puppet/indirector/request.rb | 51 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb index fd8d654dd..1918a3fb5 100644 --- a/lib/puppet/indirector/request.rb +++ b/lib/puppet/indirector/request.rb @@ -14,6 +14,51 @@ class Puppet::Indirector::Request OPTION_ATTRIBUTES = [:ip, :node, :authenticated, :ignore_terminus, :ignore_cache, :instance, :environment] + def self.from_pson(json) + raise ArgumentError, "No indirection name provided in json data" unless indirection_name = json['type'] + raise ArgumentError, "No method name provided in json data" unless method = json['method'] + raise ArgumentError, "No key provided in json data" unless key = json['key'] + + request = new(indirection_name, method, key, json['attributes']) + + if instance = json['instance'] + klass = Puppet::Indirector::Indirection.instance(request.indirection_name).model + if instance.is_a?(klass) + request.instance = instance + else + request.instance = klass.from_pson(instance) + end + end + + request + end + + def to_pson(*args) + result = { + 'document_type' => 'Puppet::Indirector::Request', + 'data' => { + 'type' => indirection_name, + 'method' => method, + 'key' => key + } + } + data = result['data'] + attributes = {} + OPTION_ATTRIBUTES.each do |key| + next unless value = send(key) + attributes[key] = value + end + + options.each do |opt, value| + attributes[opt] = value + end + + data['attributes'] = attributes unless attributes.empty? + data['instance'] = instance if instance + + result.to_pson(*args) + end + # Is this an authenticated request? def authenticated? # Double negative, so we just get true or false @@ -61,9 +106,11 @@ class Puppet::Indirector::Request self.indirection_name = indirection_name self.method = method + options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } + set_attributes(options) - @options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } + @options = options if key_or_instance.is_a?(String) || key_or_instance.is_a?(Symbol) key = key_or_instance @@ -153,7 +200,7 @@ class Puppet::Indirector::Request def set_attributes(options) OPTION_ATTRIBUTES.each do |attribute| - if options.include?(attribute) + if options.include?(attribute.to_sym) send(attribute.to_s + "=", options[attribute]) options.delete(attribute) end -- cgit From f4acb025f3a125d4c3c359fb6896ac20b36e06ab Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 12 Apr 2011 21:41:06 -0700 Subject: Adding json support to Puppet::Node Reviewed-by: Daniel Pittman Reviewed-by: Nick Lewis Signed-off-by: Luke Kanies --- lib/puppet/node.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/node.rb b/lib/puppet/node.rb index 5b0a98615..4bd4d1de6 100644 --- a/lib/puppet/node.rb +++ b/lib/puppet/node.rb @@ -20,6 +20,29 @@ class Puppet::Node attr_accessor :name, :classes, :source, :ipaddress, :parameters attr_reader :time + def self.from_pson(pson) + raise ArgumentError, "No name provided in pson data" unless name = pson['name'] + + node = new(name) + node.classes = pson['classes'] + node.parameters = pson['parameters'] + node.environment = pson['environment'] + node + end + + def to_pson(*args) + result = { + 'document_type' => "Puppet::Node", + 'data' => {} + } + result['data']['name'] = name + result['data']['classes'] = classes unless classes.empty? + result['data']['parameters'] = parameters unless parameters.empty? + result['data']['environment'] = environment.name + + result.to_pson(*args) + end + def environment return super if @environment -- cgit From 7e5ca648112a2703ba827f96f36fe2a5f7d1c751 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 7 Jul 2011 00:03:51 -0700 Subject: Making Fact json handling more resilient We were failing if any values were nil, and values were often nil, at least in testing. We now only include non-nil values, and we handle nil values just fine. Signed-off-by: Luke Kanies Reviewed-by: Nick Lewis --- lib/puppet/node/facts.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/node/facts.rb b/lib/puppet/node/facts.rb index 577b62b62..8d0a03474 100755 --- a/lib/puppet/node/facts.rb +++ b/lib/puppet/node/facts.rb @@ -61,18 +61,21 @@ class Puppet::Node::Facts def self.from_pson(data) result = new(data['name'], data['values']) - result.timestamp = Time.parse(data['timestamp']) - result.expiration = Time.parse(data['expiration']) + result.timestamp = Time.parse(data['timestamp']) if data['timestamp'] + result.expiration = Time.parse(data['expiration']) if data['expiration'] result end def to_pson(*args) - { - 'expiration' => expiration, + result = { 'name' => name, - 'timestamp' => timestamp, 'values' => strip_internal, - }.to_pson(*args) + } + + result['timestamp'] = timestamp if timestamp + result['expiration'] = expiration if expiration + + result.to_pson(*args) end # Add internal data to the facts for storage. -- cgit From 361220166525762634dd1886f84c9a928b28766b Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 15 Jul 2011 11:05:50 -0700 Subject: (#7080) Registering PSON document types We were originally using the actual constant for both Indirector Requests and Nodes, and this registers their document types as symbolic names. Signed-off-by: Luke Kanies Reviewed-by: Nick Lewis --- lib/puppet/indirector/request.rb | 6 +++++- lib/puppet/node.rb | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb index 1918a3fb5..697d9df47 100644 --- a/lib/puppet/indirector/request.rb +++ b/lib/puppet/indirector/request.rb @@ -1,6 +1,7 @@ require 'cgi' require 'uri' require 'puppet/indirector' +require 'puppet/util/pson' # This class encapsulates all of the information you need to make an # Indirection call, and as a a result also handles REST calls. It's somewhat @@ -14,6 +15,9 @@ class Puppet::Indirector::Request OPTION_ATTRIBUTES = [:ip, :node, :authenticated, :ignore_terminus, :ignore_cache, :instance, :environment] + # Load json before trying to register. + Puppet.features.pson? and ::PSON.register_document_type('IndirectorRequest',self) + def self.from_pson(json) raise ArgumentError, "No indirection name provided in json data" unless indirection_name = json['type'] raise ArgumentError, "No method name provided in json data" unless method = json['method'] @@ -35,7 +39,7 @@ class Puppet::Indirector::Request def to_pson(*args) result = { - 'document_type' => 'Puppet::Indirector::Request', + 'document_type' => 'IndirectorRequest', 'data' => { 'type' => indirection_name, 'method' => method, diff --git a/lib/puppet/node.rb b/lib/puppet/node.rb index 4bd4d1de6..16a0e5c3d 100644 --- a/lib/puppet/node.rb +++ b/lib/puppet/node.rb @@ -19,6 +19,9 @@ class Puppet::Node attr_accessor :name, :classes, :source, :ipaddress, :parameters attr_reader :time + # + # Load json before trying to register. + Puppet.features.pson? and ::PSON.register_document_type('Node',self) def self.from_pson(pson) raise ArgumentError, "No name provided in pson data" unless name = pson['name'] @@ -32,7 +35,7 @@ class Puppet::Node def to_pson(*args) result = { - 'document_type' => "Puppet::Node", + 'document_type' => "Node", 'data' => {} } result['data']['name'] = name -- cgit