Writing custom service resource types

When writing custom service resource types, remember that service resources typically adhere to the same format as standard custom types.

A service resource type looks very much like any other custom type. Service resources live in a module’s type directory and uses the typical path <ENVIRONMENT DIRECTORY>/modules/<MODULE NAME>/lib/puppet/type/<SERVICE RESOURCE.rb>.

The custom type documentation has all the information you need for writing custom types.

Since Puppet types and providers are written in Ruby, we recommend you have some experience writing Ruby code before writing any custom types.

The fundamentals of writing custom service resource types

A service resource typically adheres to the same format as a standard custom type.

Puppet::Type.newtype :sql, :is_capability => true do
  newparam :name, :namevar => true
  newparam :user
  newparam :password
  newparam :host
  newparam :port do
    defaultto :standard
    newvalues(:standard, /^\d+$/)
  newparam :database
  newparam :type do
    newvalues(:mysql, :postgresql)

The format of the service resource type adheres to the fundamental rules of custom types (as outlined in the custom types documentation):

  • Types are created by calling the newtype method on the Puppet::Type class.

  • The name of the type is the only required argument to newtype. The name must be a Ruby symbol, and the name of the file containing the type must match the type’s name.

  • The newtype method also requires a block of code, specified with either curly braces ({ ... }) or the do ... end syntax. This code block will implement the type, and contains all of the properties and parameters. The block will not be passed any arguments.

  • Every type must have at least one mandatory parameter: the namevar.

  • The is_capability property lets you specially mark these as environment wide service resources for extraction and injection into catalogs of the producer or consumer. Otherwise, sql is just a standard resource that has to be declared on a node.

  • Parameters are defined essentially exactly the same as properties; the only difference between them is that parameters never result in methods being called on providers. To define a new parameter, call the newparam method. This method takes the name of the parameter (as a symbol) as its argument, as well as a block of code.