How do I assign a value to another property from within newproperty?

asked 2018-01-15 22:44:50 -0500

Spindizzy gravatar image

There is a reason for this mad question. If you use a property named "name", you cannot use title_patterns. But if you don't have a property named "name", then you cannot use other features in a custom type such as purging. (See Issue 7629)

I'm thinking that a workaround might be found where the "name" namevar is parsed to set the other namevar values, something like the following pseudo-code:

newproperty(:name, namevar: true) do
  validate do |value|
    if value.match(%r{(some)/(pattern)/(here)})
      set(:parta,$1)
      set(:partb,$2)
      set(:partc,$3)
    else
     raise Puppet::Error, "Pseudo title_pattern does not match."
    end
  end
end

Where 'parta' through 'partc' are other newproperty instances in the class. I'm running into several problems though, through a lack of knowledge of how Ruby and Puppet work. I have no clue how to:

  1. Make the assignments happen, or
  2. Refer to the existing 'title_patterns' already defined in the class,

from within the 'validate' method.

I'm not stuck on using the 'validate' method, that's just what came first to mind. If you have some other solution for this that supports both using 'name' as a namevar and having properties populated via pattern parsing, I'd be delighted to see it!

edit retag flag offensive close merge delete