On Fri, Aug 7, 2015 at 1:48 PM <[email protected]> wrote:
> Hi all!
>
> I'm having trouble with a custom type's type-wide validate call. I've
> done a lot of digging into the Puppet documentation and a lot of Googling
> and haven't found a lot of guidance. My Puppet version is 3.7.5.
>
> Basically, I have a property defined like this in my type:
>
> newproperty(:servers,:array_matching=>:all) do
> desc "List of database servers; first server in the list will be
> considered the primary server"
>
> isrequired
> def insync?(is)
> return false unless is == should
> true
> end
>
> end
>
> I want to check that the array is non-empty. I figured out if I specify
> a validate block inside of the newproperty block then I'll just get each
> individual array member, one at a time, which isn't what I want. So,
> instead, I implemented a type-wide validate call like this:
>
> Puppet::Type.newtype(:my_type) do
>
> validate do
> fail("servers should have at least one member") if
> self[:servers].size == 0
> done
>
It's kind of awkward, but if your type uses self.instances, then in the
validate block is run after self.instances runs and has
resource.provider.servers but not self[:servers]. So I usually just do
something like `if self[:servers] and self[:servers].size == 0` to avoid
validating self.instances stuff.
The validate block is then run again when each resource is evaluated, and
that is when self's hash is populated with values from the catalog.
>
>
> When I try to run puppet resource my_type, I get:
>
> Error: Could not run: undefined method `size' for nil:NilClass
>
> When I do a pp on self, I get something that looks like (in part):
>
> #<Puppet::Type::My_type:0x000000035f7528
> @managed=false,
> @name_var_cache=:name,
> @original_parameters=
> {:provider=>
> #<Puppet::Type::My_type::ProviderMy_type:0x000000035d1350
> @property_flush={},
> @property_hash=
> {
> :servers=>["db1"],
> },
> @resource=#<Puppet::Type::My_type:0x000000035f7528 ...>>},
> @parameters=
> {
> *snip*
> },
> @provider=
> #<Puppet::Type::My_type::ProviderMy_type:0x000000035d1350
> @property_flush={},
> @property_hash=
> {
> :servers=>["db1"],
> },
> @resource=#<Puppet::Type::My_type:0x000000035f7528 ...>>,
> @tags=
> #<Puppet::Util::TagSet: {"my_type",
> "mytitle"}>,
> @title="mytitle">
>
> I poked around the types provided by Puppet and it looks like I should be
> able to do
>
> self[:servers]
>
> to access the property, but in practice that doesn't seem to work. It
> looks like the data I want is buried in the object, but I'm not sure of the
> correct means to get at it.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/5d8ea6d1-1afd-4983-a059-832d238eb6fa%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/5d8ea6d1-1afd-4983-a059-832d238eb6fa%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/CAJaQvGBxm1Td3peaDfBzLsVD5feJn1_P-saP%2BN2qjHoCOmWm9Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.