Ask Your Question
0

how to create inherited custom type

asked 2017-08-21 11:44:42 -0600

gra gravatar image

i would like to create inherited custom type and override some attribute.

like below

fil_extent inherited file

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2017-08-24 04:48:41 -0600

Henrik Lindberg gravatar image

Puppet only supports inheritance between puppet class - and then only with a single level. There is no way to get inheritance working for Ruby resource types nor for user defined types. Attempts to do so in the past (in Ruby) have not been successful. The closest thing is to write a custom provider for a ruby resource type since it can be based on an existing provider. That is probably not what you want.

What you can do instead is to create a wrapper (class or user defined type) that delegates - that is a commonly used mechanism. If you need more advanced composition you may consider doing it with the use of functions.

edit flag offensive delete link more
0

answered 2017-08-23 06:48:50 -0600

updated 2017-08-23 06:51:26 -0600

Inside the body of your new improved custom type's definition, you declare the resource. For instance

define my_module::file_extent (
  Boolean $do_something     = false,
  Hash    $additional_attrs = {},
) {
  if $do_something {
    exec { "${title}_boom":
      # ...
      before => File[$title],
    }
  }

  # Hash + Hash: duplicate keys right hand values take precedence
  $_attrs = $additional_attrs + {
    'mode'  => '0644',
    'owner' => 'root',
    'group' => 'wheel',
  }

  # in the end, you just declare the resource
  # you wanna "inherit" from
  file { $title:
    * => $_attrs,
  }
}

PP doc about splat

edit flag offensive delete link more

Comments

I think he's talking about inheriting the base custom type that is ruby, in another ruby custom type, but I may be wrong

DarylW gravatar imageDarylW ( 2017-08-23 07:15:51 -0600 )edit

Well, touché, but KISS, given his*her description “create inherited custom type and override some attribute” I'd implement that in “Puppet language”. Only when it gets 2 more complex behaviors for its implementation I'd like 2 have access 2 Puppet's underlying mechanics, I write it directly in Ruby.

Kai Burghardt gravatar imageKai Burghardt ( 2017-08-23 19:53:16 -0600 )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

1 follower

Stats

Asked: 2017-08-21 11:44:42 -0600

Seen: 169 times

Last updated: Aug 24