Class: AbideDataProcessor::Parser::Resource

Inherits:
ProcessorObject show all
Defined in:
lib/abide-data-processor/parser.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ProcessorObject

#after_me, #before_me, #inspect, #name?, #to_s

Methods included from Validation

#array_of_hashes?, #not_nil_or_empty?, #validate_control_maps, #validate_hiera_data

Constructor Details

#initialize(name, data, control_maps) ⇒ Resource

Returns a new instance of Resource.



524
525
526
527
528
529
530
531
# File 'lib/abide-data-processor/parser.rb', line 524

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

Instance Attribute Details

#control_namesObject (readonly)

Returns the value of attribute control_names.



522
523
524
# File 'lib/abide-data-processor/parser.rb', line 522

def control_names
  @control_names
end

#controlsObject (readonly)

Returns the value of attribute controls.



522
523
524
# File 'lib/abide-data-processor/parser.rb', line 522

def controls
  @controls
end

#mapped_control_namesObject (readonly)

Returns the value of attribute mapped_control_names.



522
523
524
# File 'lib/abide-data-processor/parser.rb', line 522

def mapped_control_names
  @mapped_control_names
end

#nameObject (readonly)

Returns the value of attribute name.



522
523
524
# File 'lib/abide-data-processor/parser.rb', line 522

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



522
523
524
# File 'lib/abide-data-processor/parser.rb', line 522

def type
  @type
end

Instance Method Details

#add_control_configs(control_configs) ⇒ Object

Adds overriding parameter values to controls in this resource if this resource has a matching control.

Parameters:

  • data (Hash)

    The resource data to be parsed.



536
537
538
539
540
541
542
543
544
545
546
# File 'lib/abide-data-processor/parser.rb', line 536

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) ⇒ Boolean

This method checks if this Resource contains the given control.

Parameters:

  • control (String)

    The control to be checked.

Returns:

  • (Boolean)

    True if this Resource contains the given control, false otherwise.



574
575
576
# File 'lib/abide-data-processor/parser.rb', line 574

def control?(control_name)
  @control_names.include?(control_name) || @mapped_control_names.include?(control_name)
end

#param?(param_name) ⇒ Boolean

This method checks if this Resource contains the given parameter.

Parameters:

  • param_name (String)

    The parameter to be checked.

Returns:

  • (Boolean)

    True if this Resource contains the given parameter, false otherwise.



581
582
583
584
585
586
587
# File 'lib/abide-data-processor/parser.rb', line 581

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_dataObject

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



550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/abide-data-processor/parser.rb', line 550

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_referenceString

This method returns a string representation of this Resource in the resource reference format used by Puppet.

Returns:

  • (String)

    A string representation of this Resource in the resource reference format.



566
567
568
569
# File 'lib/abide-data-processor/parser.rb', line 566

def resource_reference
  type_ref = @type.split('::').map(&:capitalize).join('::')
  "#{type_ref}['#{@name}']"
end