Class: EasyType::ExtractTask

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_type/extract_task.rb

Overview

This class is an easy way extract all specfied resource information from a database

The resource_types is hash of the name of the puppet type to use for extraction and a closure to filter out any properties or entries you don’t want.

Instance Method Summary collapse

Constructor Details

#initialize(resource_types) ⇒ ExtractTask

Returns a new instance of ExtractTask.



14
15
16
# File 'lib/easy_type/extract_task.rb', line 14

def initialize(resource_types)
  @resource_types = resource_types
end

Instance Method Details

#executeObject

Fetch all known data from resources



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easy_type/extract_task.rb', line 28

def execute
  data = {}
  @resource_types.each do | type, options|
    filter = options.delete(:filter)
    key    = options.delete(:key)
    raise "Unsupported options #{options.keys.join(', ')} provided" if options != {}
    resource_class = Puppet::Type.type(type)
    data[key] = resource_class.instances.collect do |entry| 
      entry_data = to_data(entry)
      filter ? filter.call(entry_data) : entry_data
    end.compact.reduce({}, :merge)
  end
  data
end

#to_data(value) ⇒ Object



18
19
20
21
22
23
# File 'lib/easy_type/extract_task.rb', line 18

def to_data(value)
  resource_data = value.to_resource.to_hash
  # Remove any proprty that is absent and the standard Puppet properties loglevel and provider
  resource_data.delete_if { |k, v| [:loglevel, :provider].include?(k) || v == :absent }
  {resource_data.delete(:name) => resource_data}
end