summaryrefslogtreecommitdiffstats
path: root/lib/puppet/property/ensure.rb
blob: 8b97ddeab86ecb013c58f1c14f333babe558c710 (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
require 'puppet/property'

# This property will get automatically added to any type that responds
# to the methods 'exists?', 'create', and 'destroy'.
class Puppet::Property::Ensure < Puppet::Property
  @name = :ensure

  def self.defaultvalues
    newvalue(:present) do
      if @resource.provider and @resource.provider.respond_to?(:create)
        @resource.provider.create
      else
        @resource.create
      end
      nil # return nil so the event is autogenerated
    end

    newvalue(:absent) do
      if @resource.provider and @resource.provider.respond_to?(:destroy)
        @resource.provider.destroy
      else
        @resource.destroy
      end
      nil # return nil so the event is autogenerated
    end

    defaultto do
      if @resource.managed?
        :present
      else
        nil
      end
    end

    # This doc will probably get overridden
    @doc ||= "The basic property that the resource should be in."
  end

  def self.inherited(sub)
    # Add in the two properties that everyone will have.
    sub.class_eval do
    end
  end

  def change_to_s(currentvalue, newvalue)
    begin
      if currentvalue == :absent or currentvalue.nil?
        return "created"
      elsif newvalue == :absent
        return "removed"
      else
        return "#{self.name} changed '#{self.is_to_s(currentvalue)}' to '#{self.should_to_s(newvalue)}'"
      end
    rescue Puppet::Error, Puppet::DevError
      raise
    rescue => detail
      raise Puppet::DevError, "Could not convert change #{self.name} to string: #{detail}"
    end
  end

  def retrieve
    # XXX This is a problem -- whether the object exists or not often
    # depends on the results of other properties, yet we're the first property
    # to get checked, which means that those other properties do not have
    # @is values set.  This seems to be the source of quite a few bugs,
    # although they're mostly logging bugs, not functional ones.
    if prov = @resource.provider and prov.respond_to?(:exists?)
      result = prov.exists?
    elsif @resource.respond_to?(:exists?)
      result = @resource.exists?
    else
      raise Puppet::DevError, "No ability to determine if #{@resource.class.name} exists"
    end
    if result
      return :present
    else
      return :absent
    end
  end

  # If they're talking about the thing at all, they generally want to
  # say it should exist.
  #defaultto :present
  defaultto do
    if @resource.managed?
      :present
    else
      nil
    end
  end
end