Develop types and providers with the Resource API

The recommended method to create custom types and providers is to use the Resource API, which is built on top of Puppet core. It is easier, faster, and safer than the old types and providers method.

To get started developing types and providers with the Resource API:
  1. Download https://puppet.com/download-puppet-development-kit (PDK) appropriate to your operating system and architecture.
  2. Create a new module with PDK, or work with an existing PDK-enabled module. To create a new module, run pdk new module <MODULE_NAME> from the command line, specifying the name of the module. Respond to the dialog questions.
  3. To add the puppet-resource_api gem and enable modern rspec-style mocking, open the .sync.yml file in your editor, and add the following content:

    # .sync.yml
    ---
    Gemfile:
      optional:
        ':development':
          - gem: 'puppet-resource_api'
    spec/spec_helper.rb:
      mock_with: ':rspec'

  4. Apply these changes by running pdk update
  5. To create the required files for a new type and provider in the module, run: pdk new provider <provider_name>

    You will get the following response:

    $ pdk new provider foo
    pdk (INFO): Creating '.../example/lib/puppet/type/foo.rb'from template.
    pdk (INFO): Creating '.../example/lib/puppet/provider/foo/foo.rb' from template.
    pdk (INFO): Creating '.../example/spec/unit/puppet/provider/foo/foo_spec.rb' from template.
    $
    The three generated files are the type (resource definition), the provider (resource implementation), and the unit tests. The default template contains an example that demonstrates the basic workings of the Resource API. This allows the unit tests to run immediately after creating the provider, which looks like this:
    $ pdk test unit
    [✔] Preparing to run the unit tests.
    [✔] Running unit tests.  
    Evaluated 4 tests in 0.012065973 seconds: 0 failures, 0 pending.
    [✔] Cleaning up after running unit tests.
    $