Ask Your Question
0

what happens when defined resource type is given inside a class [closed]

asked 2016-07-26 16:09:31 -0500

I see that rake lint complains that the defined resource type should not be provided inside a class declaration. however, the puppet agent run is successful and it works as expected.

And that defined type is intended to be used only by that class and no where else. So is there any reason behind not having define inside a class?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Redsmile
close date 2016-08-08 13:36:46.794764

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-08-02 09:29:45 -0500

DarylW gravatar image

updated 2016-08-02 09:30:08 -0500

To reply to your question above requires a bit more space than the comment block.

given a class like this.

#foo/manifests/myclass.pp
class foo::myclass() {
  define foo::mydefine() {
    notify{"from inside mydefine: $name":}
  }

  foo::mydefine{'define1':}
  foo::mydefine{'define2':}

}

If you invoke the above class, it will correctly find and resolve the define. However, you cannot invoke it from another class, as the puppet-lint error states "ERROR: foo::mydefine not in autoload module layout on line 2". What this means is that while the parser is parsing the myclass.pp file, it sees the define, but it is only available inside of that class. If any other class is trying to find the define, the parser is looking for the file $modulepath/foo/manifests/mydefine.pp containing a define named foo::mydefine. If those conditions aren't met, it can't look it up.

Your only option is to put it in a file, correctly namespaced like this.

#foo/manifests/mydefine.pp
define foo::mydefine(){
  notify{"from inside mydefine: $name":}
}

I hope that clears things up!

edit flag offensive delete link more

Comments

Thank you for your insight, it does. And today I read that defined types get invoked by Puppet automatically, so we don't have to do include like classes. Namespacing and having define in it's own file is what I need to do.

Redsmile gravatar imageRedsmile ( 2016-08-02 09:57:35 -0500 )edit
1

answered 2016-07-27 10:13:17 -0500

Jeremiah Powell gravatar image

Having a define inside a class is fine from a technical standpoint in Puppet 0.24 through 4. The Puppet parser will parse the define successfully as it parses the class definition.

From a code quality point of view it is a bad practice. Programmers writing or reviewing Puppet code expect that a define will be placed in its own file named after the define.

Comments describing the define may not be properly parsed by documentation generating tools that convert the comments in your code into other forms like HTML. But the actual Puppet code is valid.

Not putting the define in its own file does reduce the ability to reuse the define. There is also no guarantee that future versions of Puppet would not make in-class defines a syntax error.

edit flag offensive delete link more

Comments

1

Another piece of confusion... if you declare a variable in your class, and then expect to use that variable inside the define in your class, it's not actually 'in scope', as the define is parsed independent of the class it's in, we ran into that one

DarylW gravatar imageDarylW ( 2016-07-27 16:04:41 -0500 )edit

Thanks both for the insight. Now my question is, how do I invoke the defined resource type from another class? I tried include profile::tomcat::Tomcat_Context_Resource but that didn't work.

Redsmile gravatar imageRedsmile ( 2016-07-29 09:10:32 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-26 16:09:31 -0500

Seen: 92 times

Last updated: Aug 02 '16