Ask Your Question
7

What is the preferred way of declaring class relationships?

asked 2012-12-18 20:42:44 -0500

Stefan gravatar image

updated 2013-01-03 14:23:28 -0500

llowder gravatar image

I often find myself of having a class of the following structure

class myapp {
  class { 'myapp::package' }
  class { 'myapp::database' }
  class { 'myapp::config' }
  class { 'myapp::service' }
}

The myapp class is intended to be specified at nodelevel and acts as a wrapper class. The inner classes have a relationship, e.g. the database can only be created with an SQL script provided by the package. And the application can only be started as a service if the service is configured and the database is ready.

I am aware of two options now:

  1. Declare the relationships in the wrapper class by using ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
9

answered 2012-12-18 21:57:34 -0500

binford2k gravatar image

updated 2012-12-20 08:48:34 -0500

Hiding relationships inside the inner classes is very opaque. I would suggest something like the following:

class myapp {
  class { 'myapp::package': }
  class { 'myapp::database': }
  class { 'myapp::config': }
  class { 'myapp::service': }

  Class[myapp::package] -> Class[myapp::database] -> Class[myapp::config] ~> Class[myapp::service]
}

This separates your class definitions from the ordering of them, making it more flexible for future refactoring.

Keep in mind that this particular example also falls into the anchor pattern described by Jeff McCune at https://ask.puppetlabs.com/question/12/how-can-i-ensure-three-modules-are-configured-in-the-order-i-specify/. You may want to add anchors or slightly restructure.

edit flag offensive delete link more

Comments

Thanks for your answer. I first thought the author of the class myapp::database probably knows best what he depends on (e.g. myapp::package). On the other hand your ...(more)

Stefan gravatar imageStefan ( 2013-01-07 17:47:58 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2012-12-18 20:42:44 -0500

Seen: 342 times

Last updated: Dec 20 '12