class AbideDataProcessor::Parser::Resource

This class represents a single Puppet resource (class, defined type, etc.)

Attributes

control_names[R]
controls[R]
mapped_control_names[R]
name[R]
type[R]

Public Class Methods

new(name, data, control_maps) click to toggle source
# File lib/abide-data-processor/parser.rb, line 528
def initialize(name, data, control_maps)
  super(name, data, control_maps)
  @type = @data['type']
  @controls = create_control_classes(@data['controls'])
  @control_names = Set.new(@controls.map(&:name)).flatten
  @mapped_control_names = Set.new(@controls.map(&:mapped_names).flatten).flatten
  initialize_control_metaparams
end

Public Instance Methods

add_control_configs(control_configs) click to toggle source

Adds overriding parameter values to controls in this resource if this resource has a matching control. @param data [Hash] The resource data to be parsed.

# File lib/abide-data-processor/parser.rb, line 540
def add_control_configs(control_configs)
  control_configs.each do |control, configs|
    next unless control?(control)

    @controls.each do |control_class|
      next unless control_class.name?(control)

      control_class.resource_params.deep_merge!(configs)
    end
  end
end
control?(control_name) click to toggle source

This method checks if this Resource contains the given control. @param control [String] The control to be checked. @return [Boolean] True if this Resource contains the given control, false otherwise.

# File lib/abide-data-processor/parser.rb, line 578
def control?(control_name)
  @control_names.include?(control_name) || @mapped_control_names.include?(control_name)
end
param?(param_name) click to toggle source

This method checks if this Resource contains the given parameter. @param param_name [String] The parameter to be checked. @return [Boolean] True if this Resource contains the given parameter, false otherwise.

# File lib/abide-data-processor/parser.rb, line 585
def param?(param_name)
  if param_name.respond_to?(:each)
    param_name.all? { |name| param?(name) }
  else
    @param_names.include?(param_name)
  end
end
resource_data() click to toggle source

Outputs a representation of this object as a Hash usable by Puppet’s create_resources function.

# File lib/abide-data-processor/parser.rb, line 554
def resource_data
  control_params = control_parameters
  METAPARAMS.each do |mparam|
    next if mparam == 'dependent'

    refs = resource_references(mparam, control_params)
    next if refs.nil?

    control_params[mparam] = refs
  end
  { @type => { @name => control_params } }
end
resource_reference() click to toggle source

This method returns a string representation of this Resource in the resource reference format used by Puppet. @return [String] A string representation of this Resource in the resource reference format.

# File lib/abide-data-processor/parser.rb, line 570
def resource_reference
  type_ref = @type.split('::').map(&:capitalize).join('::')
  "#{type_ref}['#{@name}']"
end

Private Instance Methods

add_after_me(resource) click to toggle source

Adds a Resource to the after_me list. @param resource [Resource] The Resource to be added to the after_me list.

# File lib/abide-data-processor/parser.rb, line 631
def add_after_me(resource)
  after_me.append(resource)
end
add_before_me(resource) click to toggle source

Adds a Resource to the before_me list. @param resource [Resource] The Resource to be added to the before_me list.

# File lib/abide-data-processor/parser.rb, line 625
def add_before_me(resource)
  before_me.append(resource)
end
control_parameters() click to toggle source

This method gathers the resource data for each control this Resource contains. @return [Hash] The resource data for each control this Resource contains.

# File lib/abide-data-processor/parser.rb, line 597
def control_parameters
  @controls.each_with_object({}) do |control, h|
    h.deep_merge(control.resource_data)
  end
end
create_control_classes(control_data) click to toggle source

Creates a new Control class for each control in the data structure if that Control class does not already exist in the cache. @param control_data [Array] The control data of the resource from the Hiera data.

# File lib/abide-data-processor/parser.rb, line 660
def create_control_classes(control_data)
  case control_data.class.to_s
  when 'Hash'
    control_data.map { |cname, cdata| AbideDataProcessor::Parser.new_control(cname, cdata, @control_maps) }
  when 'Array'
    control_data.map { |cname| AbideDataProcessor::Parser.new_control(cname, :no_params, @control_maps) }
  else
    raise ArgumentError, "Cannot create control because control data is not the expected type. Data type is #{control_data.class}"
  end
end
initialize_control_metaparameter(mparam) click to toggle source

Initializes a single supplied metaparameter for all controls that this Resource contains and brings those values into the scope of this Resource. @param mparam [String] The metaparameter to be initialized.

# File lib/abide-data-processor/parser.rb, line 647
def initialize_control_metaparameter(mparam)
  ctrl_objects = @controls.map { |c| c.send(mparam.to_sym) }.flatten
  return if ctrl_objects.empty?

  current_objects = instance_variable_get("@#{mparam}") || []
  all_meta_objects = ctrl_objects.concat(current_objects)
  instance_variable_set("@#{mparam}", all_meta_objects.flatten.compact.uniq)
  define_singleton_method("#{mparam}?".to_sym) { all_meta_objects.any? }
end
initialize_control_metaparams() click to toggle source

Initializes all metaparameter values of all controls that this Resource contains and brings those values into the scope of this Resource. Also initializes the @before_me and @after_me instance variables.

# File lib/abide-data-processor/parser.rb, line 638
def initialize_control_metaparams
  METAPARAMS.each { |mparam| initialize_control_metaparameter(mparam) }
  @before_me = initialize_before_me
  @after_me = initialize_after_me
end
resource_references(mparam, control_params) click to toggle source

This method gets the resource references for the given metaparameter and control parameters. @param mparam [String] The metaparameter to be checked. @param control_params [Hash] The control parameters to be checked. @return [Array] The resource references for the given metaparameter and control parameters. @return [nil] If the given metaparameter is not set on this Resource.

# File lib/abide-data-processor/parser.rb, line 608
def resource_references(mparam, control_params)
  # rubocop:disable Style/RedundantSelf
  # we use self here because `require` is a metaparam and we don't want to
  # call `Kernel#require` accidentally.
  this_mparam = self.send(mparam.to_sym)
  # rubocop:enable Style/RedundantSelf
  return if this_mparam.nil? || this_mparam.compact.empty?

  if control_params.key?(mparam)
    control_params[mparam].concat(this_mparam.map(&:resource_reference))
  else
    this_mparam.map(&:resource_reference)
  end
end