Hi, I''m trying to pass a parameter different from the default to the child of a parametrized class with inheritance, but the inherited code from the parent class doesn''t seem to be taking the correct parameter. For example, in the following code: class parent ( $foo = "bar" ) { notice("parent: foo is $foo ") } class child ( $foo = "bar" ) inherits parent { notice("child: foo is $foo ") } class { "child": foo => "test" } I would expect the following result: notice: Scope(Class[Parent]): parent: foo is test notice: Scope(Class[Child]): child: foo is test I''m getting the following one instead: notice: Scope(Class[Parent]): parent: foo is bar notice: Scope(Class[Child]): child: foo is test Is that the correct behaviour? -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On May 20, 10:20 am, Carles Amigó <carles.am...@softonic.com> wrote:> Hi, > > I''m trying to pass a parameter different from the default to the child > of a parametrized class with inheritance,I would not recommend mixing class inheritance with parameterized classes. As a separate matter, I would recommend to most people to avoid parameterized classes altogether.> but the inherited code from > the parent class doesn''t seem to be taking the correct parameter. > > For example, in the following code: > > class parent ( $foo = "bar" ) { > notice("parent: foo is $foo ") > > } > > class child ( $foo = "bar" ) inherits parent { > notice("child: foo is $foo ") > > } > > class { "child": foo => "test" } > > I would expect the following result: > > notice: Scope(Class[Parent]): parent: foo is test > notice: Scope(Class[Child]): child: foo is test > > I''m getting the following one instead: > > notice: Scope(Class[Parent]): parent: foo is bar > notice: Scope(Class[Child]): child: foo is test > > Is that the correct behaviour?"Correct" is such a nuanced word. I don''t know whether the observed behavior is by design, but I don''t find it surprising. I would not have found your expected behavior surprising either, however. You may be able to achieve your desired result like this: class { "parent": foo => "test" } class { "child": foo => "test" } That is, it is safe to include both a class and one of its subclasses in the same manifest. Do be aware, however, that you should create subclasses *only* to override superclass resource properties. And with parameterized classes, you can achieve an equivalent result via class parameters instead. If you want a class that declares all the resources of another, plus more, then have one class "include" the other instead of inheriting it. Except don''t do that with parameterized classes: Puppet Labs recommends against it, and there are practical reasons to avoid doing so. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Carles Amigó
2011-May-24 13:55 UTC
Re: [Puppet Users] Re: Inheritance in parametrized classes
> "Correct" is such a nuanced word. I don''t know whether the observed > behavior is by design, but I don''t find it surprising. I would not > have found your expected behavior surprising either, however. > > You may be able to achieve your desired result like this: > > class { "parent": foo => "test" } > class { "child": foo => "test" }Yes, I finally reached the same conclusion. This is exactly how I fixed it.> That is, it is safe to include both a class and one of its subclasses > in the same manifest. Do be aware, however, that you should create > subclasses *only* to override superclass resource properties. And > with parameterized classes, you can achieve an equivalent result via > class parameters instead.In this example I wasn''t overriding any property but I was doing so in my "real" case, so inheritance was needed here.> If you want a class that declares all the resources of another, plus > more, then have one class "include" the other instead of inheriting > it. Except don''t do that with parameterized classes: Puppet Labs > recommends against it, and there are practical reasons to avoid doing > so.That''s how I''m doing it when I don''t have to override any property. IMHO It''s becoming more and more difficult to know what''s the correct way to do something since the introduction of the parametrized classes. They are a huge improvement though. Cheers -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.