Class composition + parameterization

34 views
Skip to first unread message

Joaquin Menchaca

unread,
May 6, 2018, 5:55:40 PM5/6/18
to Puppet Users
I am sleuthing for this and haven't found anything obvious yet.

I have an init.pp that looks like this

class dk_oracle_java (
  $docroot     = $::dk_oracle_java::params::docroot,
  Optional[String] $local_repo  = $::dk_oracle_java::params::local_repo,
  $version     = $::dk_oracle_java::params::version,
  $javas       = $::dk_oracle_java::params::javas,
) inherits ::dk_oracle_java::params {
    include ::dk_oracle_java::install
}

And I want to access the $version in my install.pp, but I am not sure how to do this.

class dk_oracle_java::install {
  package { 'oracle-java8-installer':
    ensure => $version,
  }
}

How can I get this to work, or what is the best practice.  Can the install.pp access parameters from the base class, $::version?  Or does this need to be passed downward?

Joaquin Menchaca

unread,
May 6, 2018, 6:03:47 PM5/6/18
to Puppet Users
I spotted it, seems obvious now:

class dk_oracle_java::install inherits dk_oracle_java {

a...@example42.com

unread,
May 7, 2018, 3:21:20 AM5/7/18
to Puppet Users
Actually you don't have to inherit, you can (and should) just have something like:

class dk_oracle_java::install {
 
include dk_oracle_java
  package { 'oracle-java8-installer':
    ensure => $::dk_oracle_java::version,
  }
}

The above include is not necessary if you are already incuding the dk_oracle_java class somewhere else, just be sure that the class is included before you refer to its variables.
The only currently acceptable reasons to use class inheritance is:
- when using the params patterm (main class of a module inheriting the params one), which is actully now totally unnecessary and can be replaced by data in module.
- when for whatever reason you have to change some parameters of a resource already declared in the inherited class, and also for this case you can have other workarounds.

Anyway inheritance still works, and so if it works for you it's ok.

Thomas Müller

unread,
May 7, 2018, 3:15:41 PM5/7/18
to Puppet Users


Am Montag, 7. Mai 2018 09:21:20 UTC+2 schrieb a...@example42.com:
Actually you don't have to inherit, you can (and should) just have something like:

class dk_oracle_java::install {
 
include dk_oracle_java
  package { 'oracle-java8-installer':
    ensure => $::dk_oracle_java::version,
  }
}



 
The above include is not necessary if you are already incuding the dk_oracle_java class somewhere else, just be sure that the class is included before you refer to its variables.


I personally consider the *::install, *::config, *::service classes as private and they should not include the parent class - they should be contained from the main class. Outside of the module only the main class should be included. The *::install and other classes should fail if included from outside (maybe with help of: https://github.com/puppetlabs/puppetlabs-stdlib#assert_private).

And if not yet using: I would recommend you the PDK (https://puppet.com/docs/pdk/1.x/pdk.html) which will also create basic test classes for you.

- Thomas
Reply all
Reply to author
Forward
0 new messages