Nested class not working

160 views
Skip to first unread message

Navid Paya

unread,
Dec 4, 2012, 9:32:29 PM12/4/12
to puppet...@googlegroups.com
Hi
I am trying to use nested classes but for whatever reason it doesn't work. Here's my site.pp file:

include stdlib
include apt
node 'zenoss.example.com' {
  include mock
}
class mock {
  class my_file {
    file { '/tmp/test':
      ensure  => present,
      content => "Just testing to see if 'This' is working",
    }
  }
  apt::source { "debian-squeeze":
    location          => "http://ftp.jp.debian.org/debian/",
    release           => "squeeze",
    repos             => "main contrib non-free",
    include_src       => false,
  }
}
The problem is when I run it, the apt repository is added but the file is not. What am I doing wrong? 

José Juan Montes

unread,
Dec 4, 2012, 10:10:09 PM12/4/12
to puppet...@googlegroups.com
Nested classes are not automatically included.

Try including your nested class explicitly ,after apt:source:

include my_file

Alternatively, you don't need to do that just for a file. Simply put your "file" resource directly within the "mock" class.

I hope it helps. Best regards.


Gary Larizza

unread,
Dec 4, 2012, 10:21:53 PM12/4/12
to puppet...@googlegroups.com
It's a bit deeper than that,

Nesting a class definition inside of a class definition will result in a class namespaced as first_class::second_class.  Because of this, you'll need to "include mock::my_file" in your node declaration.

It's recommended to keep one class definition per file and to follow the Module patterns [2] and Style Guide [3] when doing so.  






--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.



--

Gary Larizza
Professional Services Engineer
Puppet Labs

Navid Paya

unread,
Dec 4, 2012, 10:25:04 PM12/4/12
to puppet...@googlegroups.com
OK, that makes sense. Thanks for the tip. Gotta go do some reading. The thing is this very same manifest used to work under puppet 2.6. If there's one thing I really don't like about puppet it's the amount of change although I understand it's needed for a product under such heavy development.

jcbollinger

unread,
Dec 5, 2012, 9:03:49 AM12/5/12
to puppet...@googlegroups.com


On Tuesday, December 4, 2012 9:21:53 PM UTC-6, Gary Larizza wrote:
It's a bit deeper than that,

Nesting a class definition inside of a class definition will result in a class namespaced as first_class::second_class.


That is, nesting will result only in a class definition namespaced as Gary describes.  It does itself not cause the nested class to be assigned to those nodes to which its containing class is assigned.

 
 Because of this, you'll need to "include mock::my_file" in your node declaration.


Or add that 'include' statement in the body of class mock, after the nested class definition.  That would produce exactly the result you expected.  If you're going to do that, however, then you should consider why you wanted a nested class in the first place.

 

It's recommended to keep one class definition per file and to follow the Module patterns [2] and Style Guide [3] when doing so.  




Indeed so.  To be a bit more blunt: don't lexically nest classes.  Nesting provides little more than namespacing, which you can get explicitly instead.  Furthermore, nested classes may be missed by the autoloader, and also by humans.  Class nesting is mostly pain for little, if any, gain.


John

jcbollinger

unread,
Dec 5, 2012, 9:17:16 AM12/5/12
to puppet...@googlegroups.com


On Tuesday, December 4, 2012 9:25:04 PM UTC-6, Navid Paya wrote:
OK, that makes sense. Thanks for the tip. Gotta go do some reading. The thing is this very same manifest used to work under puppet 2.6.


No, I'm sure it didn't.  None of the relevant characteristics of Puppet have changed between 2.6 and 3.0.

I don't doubt that you have a real-world manifest that broke between 2.6 and 3.0, but it's not because Puppet ever auto-included nested classes.  If I were to take a wild guess, I'd say it's far more likely that your failure is related to the removal of dynamic scoping (which was discussed a lot around here, and for which the entire Puppet 2.7 series produces deprecation warnings).

You can see a full list of changes from the 2.x series to the 3.0 series in the Puppet 3 release notes: http://docs.puppetlabs.com/puppet/3/reference/release_notes.html.  Honestly, there is very little in the backwards incompatibility list that pertains to the Puppet language, and all changes of that type involve features that were already deprecated in the previous release series.


John

Reply all
Reply to author
Forward
0 new messages