summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network/formats.rb
blob: 2d519aea6b5d50b843e8fe10f67705ae320c157f (plain)
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
require 'puppet/network/format_handler'

Puppet::Network::FormatHandler.create(:yaml, :mime => "text/yaml") do
    # Yaml doesn't need the class name; it's serialized.
    def intern(klass, text)
        YAML.load(text)
    end

    # Yaml doesn't need the class name; it's serialized.
    def intern_multiple(klass, text)
        YAML.load(text)
    end

    def render(instance)
        instance.to_yaml
    end

    # Yaml monkey-patches Array, so this works.
    def render_multiple(instances)
        instances.to_yaml
    end

    # Unlike core's yaml, ZAML should support 1.8.1 just fine
    def supported?(klass)
        true
    end
end

# This is a "special" format which is used for the moment only when sending facts
# as REST GET parameters (see Puppet::Configurer::FactHandler).
# This format combines a yaml serialization, then zlib compression and base64 encoding.
Puppet::Network::FormatHandler.create(:b64_zlib_yaml, :mime => "text/b64_zlib_yaml") do
    require 'base64'

    def use_zlib?
        Puppet.features.zlib? && Puppet[:zlib]
    end

    def requiring_zlib
        if use_zlib?
            yield
        else
            raise Puppet::Error, "the zlib library is not installed or is disabled."
        end
    end

    def intern(klass, text)
        decode(text)
    end

    def intern_multiple(klass, text)
        decode(text)
    end

    def render(instance)
        encode(instance.to_yaml)
    end

    def render_multiple(instances)
        encode(instances.to_yaml)
    end

    def supported?(klass)
        true
    end

    def encode(text)
        requiring_zlib do
            Base64.encode64(Zlib::Deflate.deflate(text, Zlib::BEST_COMPRESSION))
        end
    end

    def decode(yaml)
        requiring_zlib do
            YAML.load(Zlib::Inflate.inflate(Base64.decode64(yaml)))
        end
    end
end


Puppet::Network::FormatHandler.create(:marshal, :mime => "text/marshal") do
    # Marshal doesn't need the class name; it's serialized.
    def intern(klass, text)
        Marshal.load(text)
    end

    # Marshal doesn't need the class name; it's serialized.
    def intern_multiple(klass, text)
        Marshal.load(text)
    end

    def render(instance)
        Marshal.dump(instance)
    end

    # Marshal monkey-patches Array, so this works.
    def render_multiple(instances)
        Marshal.dump(instances)
    end

    # Everything's supported
    def supported?(klass)
        true
    end
end

Puppet::Network::FormatHandler.create(:s, :mime => "text/plain", :extension => "txt")

# A very low-weight format so it'll never get chosen automatically.
Puppet::Network::FormatHandler.create(:raw, :mime => "application/x-raw", :weight => 1) do
    def intern_multiple(klass, text)
        raise NotImplementedError
    end

    def render_multiple(instances)
        raise NotImplementedError
    end

    # LAK:NOTE The format system isn't currently flexible enough to handle
    # what I need to support raw formats just for individual instances (rather
    # than both individual and collections), but we don't yet have enough data
    # to make a "correct" design.
    #   So, we hack it so it works for singular but fail if someone tries it
    # on plurals.
    def supported?(klass)
        true
    end
end

Puppet::Network::FormatHandler.create(:pson, :mime => "text/pson", :weight => 10, :required_methods => [:render_method, :intern_method]) do
    confine :true => Puppet.features.pson?

    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.  By default,
    # we'll include class information but we won't rely on it - we don't
    # want class names to be required because we then can't change our
    # internal class names, which is bad.
    def data_to_instance(klass, data)
        if data.is_a?(Hash) and d = data['data']
            data = d
        end
        return data if data.is_a?(klass)
        klass.from_pson(data)
    end
end

# This is really only ever going to be used for Catalogs.
Puppet::Network::FormatHandler.create(:dot, :mime => "text/dot", :required_methods => [:render_method])