1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
require_relative '../../puppet/network/format_handler'
require_relative '../../puppet/util/json'
Puppet::Network::FormatHandler.create_serialized_formats(:msgpack, :weight => 20, :mime => "application/x-msgpack", :required_methods => [:render_method, :intern_method], :intern_method => :from_data_hash) do
confine :feature => :msgpack
def intern(klass, text)
data = MessagePack.unpack(text)
return data if data.is_a?(klass)
klass.from_data_hash(data)
end
def intern_multiple(klass, text)
MessagePack.unpack(text).collect do |data|
klass.from_data_hash(data)
end
end
def render_multiple(instances)
instances.to_msgpack
end
end
Puppet::Network::FormatHandler.create_serialized_formats(:yaml) do
def allowed_yaml_classes
@allowed_yaml_classes ||= [
Puppet::Node::Facts,
Puppet::Node,
Puppet::Transaction::Report,
Puppet::Resource,
Puppet::Resource::Catalog
]
end
def intern(klass, text)
data = Puppet::Util::Yaml.safe_load(text, allowed_yaml_classes)
data_to_instance(klass, data)
rescue Puppet::Util::Yaml::YamlLoadError => e
raise Puppet::Network::FormatHandler::FormatError, _("Serialized YAML did not contain a valid instance of %{klass}: %{message}") % { klass: klass, message: e.message }
end
def intern_multiple(klass, text)
data = Puppet::Util::Yaml.safe_load(text, allowed_yaml_classes)
unless data.respond_to?(:collect)
raise Puppet::Network::FormatHandler::FormatError, _("Serialized YAML did not contain a collection of instances when calling intern_multiple")
end
data.collect do |datum|
data_to_instance(klass, datum)
end
rescue Puppet::Util::Yaml::YamlLoadError => e
raise Puppet::Network::FormatHandler::FormatError, _("Serialized YAML did not contain a valid instance of %{klass}: %{message}") % { klass: klass, message: e.message }
end
def data_to_instance(klass, data)
return data if data.is_a?(klass)
unless data.is_a? Hash
raise Puppet::Network::FormatHandler::FormatError, _("Serialized YAML did not contain a valid instance of %{klass}") % { klass: klass }
end
klass.from_data_hash(data)
end
def render(instance)
instance.to_yaml
end
# Yaml monkey-patches Array, so this works.
def render_multiple(instances)
instances.to_yaml
end
def supported?(klass)
true
end
end
Puppet::Network::FormatHandler.create(:s, :mime => "text/plain", :charset => Encoding::UTF_8, :extension => "txt")
# By default, to_binary is called to render and from_binary called to intern. Note unlike
# text-based formats (json, yaml, etc), we don't use to_data_hash for binary.
Puppet::Network::FormatHandler.create(:binary, :mime => "application/octet-stream", :weight => 1,
:required_methods => [:render_method, :intern_method]) do
end
# PSON is deprecated
Puppet::Network::FormatHandler.create_serialized_formats(:pson, :weight => 10, :required_methods => [:render_method, :intern_method], :intern_method => :from_data_hash) do
def intern(klass, text)
data_to_instance(klass, PSON.parse(text))
end
def intern_multiple(klass, text)
PSON.parse(text).collect do |data|
data_to_instance(klass, data)
end
end
# PSON monkey-patches Array, so this works.
def render_multiple(instances)
instances.to_pson
end
# If they pass class information, we want to ignore it.
# This is required for compatibility with Puppet 3.x
def data_to_instance(klass, data)
d = data['data'] if data.is_a?(Hash)
if d
data = d
end
return data if data.is_a?(klass)
klass.from_data_hash(data)
end
end
Puppet::Network::FormatHandler.create_serialized_formats(:json, :mime => 'application/json', :charset => Encoding::UTF_8, :weight => 15, :required_methods => [:render_method, :intern_method], :intern_method => :from_data_hash) do
def intern(klass, text)
data_to_instance(klass, Puppet::Util::Json.load(text))
end
def intern_multiple(klass, text)
Puppet::Util::Json.load(text).collect do |data|
data_to_instance(klass, data)
end
end
def render_multiple(instances)
Puppet::Util::Json.dump(instances)
end
# Unlike PSON, we do not need to unwrap the data envelope, because legacy 3.x agents
# have never supported JSON
def data_to_instance(klass, data)
return data if data.is_a?(klass)
klass.from_data_hash(data)
end
end
# This is really only ever going to be used for Catalogs.
Puppet::Network::FormatHandler.create_serialized_formats(:dot, :required_methods => [:render_method])
Puppet::Network::FormatHandler.create(:console,
:mime => 'text/x-console-text',
:weight => 0) do
def json
@json ||= Puppet::Network::FormatHandler.format(:json)
end
def render(datum)
return datum if datum.is_a?(String) || datum.is_a?(Numeric)
# Simple hash to table
if datum.is_a?(Hash) && datum.keys.all? { |x| x.is_a?(String) || x.is_a?(Numeric) }
output = ''
column_a = datum.empty? ? 2 : datum.map{ |k,v| k.to_s.length }.max + 2
datum.sort_by { |k,v| k.to_s } .each do |key, value|
output << key.to_s.ljust(column_a)
output << json.render(value).
chomp.gsub(/\n */) { |x| x + (' ' * column_a) }
output << "\n"
end
return output
end
# Print one item per line for arrays
if datum.is_a? Array
output = ''
datum.each do |item|
output << item.to_s
output << "\n"
end
return output
end
# ...or pretty-print the inspect outcome.
Puppet::Util::Json.dump(datum, :pretty => true, :quirks_mode => true)
end
def render_multiple(data)
data.collect(&:render).join("\n")
end
end
Puppet::Network::FormatHandler.create(:flat,
:mime => 'text/x-flat-text',
:weight => 0) do
def flatten_hash(hash)
hash.each_with_object({}) do |(k, v), h|
if v.is_a? Hash
flatten_hash(v).map do |h_k, h_v|
h["#{k}.#{h_k}"] = h_v
end
elsif v.is_a? Array
v.each_with_index do |el, i|
if el.is_a? Hash
flatten_hash(el).map do |el_k, el_v|
h["#{k}.#{i}.#{el_k}"] = el_v
end
else
h["#{k}.#{i}"] = el
end
end
else
h[k] = v
end
end
end
def flatten_array(array)
a={}
array.each_with_index do |el, i|
if el.is_a? Hash
flatten_hash(el).map do |el_k, el_v|
a["#{i}.#{el_k}"] = el_v
end
else
a["#{i}"] = el
end
end
a
end
def construct_output(data)
output = ''
data.each do |key, value|
output << "#{key}=#{value}"
output << "\n"
end
output
end
def render(datum)
return datum if datum.is_a?(String) || datum.is_a?(Numeric)
# Simple hash
if datum.is_a?(Hash)
data = flatten_hash(datum)
return construct_output(data)
elsif datum.is_a?(Array)
data = flatten_array(datum)
return construct_output(data)
end
Puppet::Util::Json.dump(datum, :pretty => true, :quirks_mode => true)
end
def render_multiple(data)
data.collect(&:render).join("\n")
end
end
Puppet::Network::FormatHandler.create(:rich_data_json, mime: 'application/vnd.puppet.rich+json', charset: Encoding::UTF_8, weight: 30) do
def intern(klass, text)
Puppet.override({:rich_data => true}) do
data_to_instance(klass, Puppet::Util::Json.load(text))
end
end
def intern_multiple(klass, text)
Puppet.override({:rich_data => true}) do
Puppet::Util::Json.load(text).collect do |data|
data_to_instance(klass, data)
end
end
end
def render(instance)
Puppet.override({:rich_data => true}) do
instance.to_json
end
end
def render_multiple(instances)
Puppet.override({:rich_data => true}) do
Puppet::Util::Json.dump(instances)
end
end
def data_to_instance(klass, data)
Puppet.override({:rich_data => true}) do
return data if data.is_a?(klass)
klass.from_data_hash(data)
end
end
def supported?(klass)
klass == Puppet::Resource::Catalog &&
Puppet.lookup(:current_environment).rich_data?
end
end
Puppet::Network::FormatHandler.create_serialized_formats(:rich_data_msgpack, mime: "application/vnd.puppet.rich+msgpack", weight: 35) do
confine :feature => :msgpack
def intern(klass, text)
Puppet.override(rich_data: true) do
data = MessagePack.unpack(text)
return data if data.is_a?(klass)
klass.from_data_hash(data)
end
end
def intern_multiple(klass, text)
Puppet.override(rich_data: true) do
MessagePack.unpack(text).collect do |data|
klass.from_data_hash(data)
end
end
end
def render_multiple(instances)
Puppet.override(rich_data: true) do
instances.to_msgpack
end
end
def render(instance)
Puppet.override(rich_data: true) do
instance.to_msgpack
end
end
def supported?(klass)
suitable? &&
klass == Puppet::Resource::Catalog &&
Puppet.lookup(:current_environment).rich_data?
end
end
|