FAQ
Hi Stefan,

My apologies that I did not get back sooner. What you are suggesting
yields the same results I was seeing before. Let me try to explain
better:

Using no modules (just standard puppet types), I can do this:
---
Package {
   notify => Exec["package-changed"]
}

exec { "package-changed":
   command => "/bin/true",
   refreshonly => true;
}

package { "vim-enhanced":
   ensure => installed;
}
---

If the above code happens to install vim-enhanced, then I will see
output like this:
---
Notice: /Stage[main]//Package[vim-enhanced]/ensure: created
Notice: /Stage[main]//Exec[package-changed]: Triggered 'refresh' from 1
events
Notice: Finished catalog run in 4.75 seconds
---

The 'Package {}' defaults are applied and the Exec gets notified.

Now, I write a simple module with a type like this:
---
Puppet::Type.newtype(:foo) do
   newparam(:title) do
     isnamevar
   end
   def eval_generate
     resources = []
     resources << Puppet::Type.type(:package).new(:title => self.title)
     resources
   end
end
---

All it does is add a new package resource via eval_generate. I execute
it using a manifest that looks like this:
---
Package {
   notify => Exec["package-changed"]
}

exec { "package-changed":
   command => "/bin/true",
   refreshonly => true;
}

foo { "vim-enhanced": }
---

It yields the following:
---
Notice: /Package[vim-enhanced]/ensure: created
Notice: Finished catalog run in 5.10 seconds
---

The Exec from the resource defaults in my current scope is not notified.
If they were, the Exec resource should have run, just like in the first
example.

I am looking for a way to generate resources from a type, and have the
resource defaults from the scope they are defined applied to them.

Any other thoughts or suggestions are most appreciated, as always.

Thanks!
- Ryan
From: "Stefan Schulte" <stefan.schulte@taunusstein.net>
To: puppet-users@googlegroups.com
Sent: Wednesday, May 1, 2013 4:54:38 AM
Subject: Re: [Puppet Users] Retrieve scoped resource defaults from class
method in custom type

On Tue, 30 Apr 2013 15:37:11 -0700
Ryan Uber wrote:
[accidently sent this to puppet-dev, re-posting to puppet-users]

Hello puppet-users,

I am working on a module that provides a custom type. The type when
called will create new resources in the catalog using syntax like:

Puppet::Type.type(:file).new(:title => 'blah')

This works fine and the resources are added as expected. However, I am
unable to apply any scoped defaults to the generated resources. So
something like:

File {
mode => 0750
}
were do you generate the resources? Puppet already has a method
"eval_generate" that every type can implement and which has to return
an array of generated resources (that's how puppet generates implicit
file resource when you use `recurse`). These resources are
automatically added to the catalog.

So e.g.

Puppet::Type.newtype(:foo) do
newparam(:name)
[...]
def eval_generate
resources = []
resources << Puppet::Type.type(:file).new(:title => 'blah')
resources
end
end

I played with it a little bit (wanted to make a proof of concept for
a `dirtree` type that simulates the behaviour of `mkdir -p`) but I hit
problems because autorequirements of generated resources do not seem to
work (e.g. You have File['/foo'] in your puppet manifest and your
custom type generates a resource File['/foo/bar'] there a no automatic
dependencies so File['/foo/bar'] may be applied before File['/foo'] but
maybe I am wrong about that one.

-Stefan

--
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 puppet-users+unsubscribe@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
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 puppet-users+unsubscribe@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jcbollinger at May 6, 2013 at 4:55 pm

    On Sunday, May 5, 2013 2:23:25 PM UTC-5, Ryan R. Uber wrote:
    Hi Stefan,

    My apologies that I did not get back sooner. What you are suggesting
    yields the same results I was seeing before. Let me try to explain
    better:
    I think Stefan understood the question. He advised you on the appropriate
    interface for a resource type to use for adding generated resources to the
    catalog, but he was clearly uncertain about whether using that approach
    (which you should do regardless) would be sufficient to resolve your issue.

    I think the final answer is probably that the Puppet API does not provide a
    mechanism for what you're asking. It's certainly good that Puppet doesn't
    automatically exhibit the behavior you describe, but I don't think it
    provides even an optional way to obtain the defaults. If it did, however,
    then probably it would be associated with a scope object, since resource
    defaults are (dynamic) scope-dependent.

    Can you implement your resource type as a defined type? That would give
    you resource defaults automatically.


    John

    --
    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 puppet-users+unsubscribe@googlegroups.com.
    To post to this group, send email to puppet-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/puppet-users?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppuppet-users @
categoriespuppet
postedMay 5, '13 at 7:23p
activeMay 6, '13 at 4:55p
posts2
users2
websitepuppetlabs.com

2 users in discussion

Ryan Uber: 1 post Jcbollinger: 1 post

People

Translate

site design / logo © 2022 Grokbase