summaryrefslogtreecommitdiffstats
path: root/lib/puppet/resource/status.rb
blob: a52b927fceb2879a138adfbe0f37fe2046e3b385 (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
module Puppet
    class Resource
        class Status
            include Puppet::Util::Tagging
            include Puppet::Util::Logging

            ATTRIBUTES = [:resource, :node, :version, :file, :line, :current_values, :skipped_reason, :status, :evaluation_time, :change_count]
            attr_accessor *ATTRIBUTES

            STATES = [:skipped, :failed, :failed_to_restart, :restarted, :changed, :out_of_sync, :scheduled]
            attr_accessor *STATES

            attr_reader :source_description, :default_log_level, :time, :resource

            # Provide a boolean method for each of the states.
            STATES.each do |attr|
                define_method("#{attr}?") do
                    !! send(attr)
                end
            end

            def <<(event)
                add_event(event)
                self
            end

            def add_event(event)
                @events << event
                if event.status == 'failure'
                    self.failed = true
                end
            end

            def events
                @events
            end

            def initialize(resource)
                @source_description = resource.path
                @resource = resource.to_s

                [:file, :line, :version].each do |attr|
                    send(attr.to_s + "=", resource.send(attr))
                end

                tag(*resource.tags)
                @time = Time.now
                @events = []
            end

            private

            def log_source
                source_description
            end
        end
    end
end