Search code examples
puppet

How to test the value of ensure in a custom type?


I've been writing custom types for Puppet, and I've run into a case where for both 'latest' and 'present' I require the existence of certain parameters, but for 'absent' I would like those parameters to be optional.

Unfortunately, I haven't been able to figure out how to test the value of 'ensure' within the ruby code.

Puppet::Type.type(:fubar) do
  ensurable do
    desc 'Has three states: absent, present, and latest.'
    newvalue(:absent) do
      # ...
    end

    newvalue(:latest) do
      # ...
    end

    newvalue(:present) do
      # ...
    end

    def insync?(is)
      # ...
    end

    defaultto :latest
  end

  # ...

  validate do
    # This if condition doesn't work.  The error is still raised.
    if :ensure != :absent
      unless value(:myprop)
        raise ArgumentError, "Property 'myprop' is required."
      end
    end
  end
end

So, my question is simple... How do I test the value of 'ensure' so that when it is 'absent', the validation is NOT performed?


Solution

  • Thanks to both Matt Schuchard and John Bollinger for their help.

    The problem is that:

    if :ensure != :absent
    

    is indeed comparing two symbols, where I need to be comparing the value of a property to a symbol:

    if self[:ensure] != :absent
    

    I am sufficiently new to both Puppet and Ruby that I didn't realize the difference. John stated it clearly, and Matt provided a good example.

    Again, Thanks to both Matt and John.