Forge Home

ruby_plugin_helper

A helper for writing Bolt plugins in Ruby

65,481 downloads

25,088 latest version

3.6 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Version information

  • 0.2.0 (latest)
  • 0.1.0
released Jan 22nd 2021

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'puppetlabs-ruby_plugin_helper', '0.2.0'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add puppetlabs-ruby_plugin_helper
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install puppetlabs-ruby_plugin_helper --version 0.2.0

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

puppetlabs/ruby_plugin_helper — version 0.2.0 Jan 22nd 2021

Ruby Plugin Helper

A helper library for writing Bolt plugins in Ruby. It provides a module which has two methods:

  • required_data which accepts a template and returns an array of lookups that need to be provided by the plugin
  • populate_template which accepts a template and an array of maps, where each map is the lookups and their values for a single target.

Lookups can optional have dot syntax, which can be used to traverse data structures. For example foo.0.bar should get the value baz from the following structure:

{ foo: [ { bar: 'baz' }, { qux: 'corge' } ] }

Setup

To use this library, include this module in a Puppetfile:

mod 'puppetlabs-ruby_plugin_helper'

Add it to your task metadata

{
  "files": ["ruby_plugin_helper/files/plugin_helper.rb"],
}

Usage

When writing your plugin, include the module in the plugin class and use the methods provided to populate templated data that users may define in their inventory when using your plugin.

required_data

This method accepts a template from the Bolt inventory file, which should look approximately like:

{ name: 'lookup',
  uri: 'look.0.uri',
  config: {
    ssh: {
      user: 'user.lookup'
    }
  }
}

And returns an array of arrays, where each array is the lookup to be made:

[['lookup'], ['look', '0', 'uri'], ['user', 'lookup']]

Note that integers remain strings, and must be converted to array indices by the plugin itself.

apply_mapping

This method accepts a template and a single map of lookup keys to their values (i.e. a lookup for a target). It's...hard to describe in words:

Template:

{ name: 'lookup',
  uri: 'look.0.uri',
  config: {
    ssh: {
      user: 'user.lookup'
    }
  }
}

Lookups:

{ 'lookup' => 'Target 1',
  'look' => [ { 'uri' => 'hostname.com' } ],
  'user' => { 'lookup' => 'AcidBurn' }
}

Return:

{ name: 'Target 1',
  uri: 'hostname.com',
  config: {
    ssh: {
      user: 'AcidBurn'
    }
  }
}

This will often be mapped over an array of records, like so:

lookups = [
  { 'lookup' => 'Target 1',
    'look' => [ { 'uri' => 'kate.com' } ],
    'user' => { 'lookup' => 'AcidBurn' }
  },
  { 'lookup' => 'Target 2',
    'look' => [ { 'uri' => 'dade.com' } ],
    'user' => { 'lookup' => 'CrashOverride' }
  }
]

lookups.map { |lookup| apply_mapping(template, lookup) }

Examples

It's best to use live examples from the aws_inventory plugin, though this is a simplified illustration of using the helper:

def resolve_reference(plugin_config)
  template = plugin_config.delete(:target_mapping)
  client = config_client(plugin_config)
  targets = request_targets(client)
  
  # This gets the lookup values that the plugin needs to 'fill in'
  attributes = required_data(template)
  target_data = targets.map do |target|
    attributes.each_with_object({}) do |attr, acc|
      attr = attr.first
      acc[attr] = target.respond_to?(attr) ? target.send(attr) : nil
    end
  end 

  # Returns data structure to be sent to the inventory
  target_data.map { |data| apply_mapping(template, data) }
end