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.



528
529
530
531
532
533
534
535
# 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

Instance Attribute Details

#control_namesObject (readonly)

Returns the value of attribute control_names.



526
527
528
# File 'lib/abide-data-processor/parser.rb', line 526

def control_names
  @control_names
end

#controlsObject (readonly)

Returns the value of attribute controls.



526
527
528
# File 'lib/abide-data-processor/parser.rb', line 526

def controls
  @controls
end

#mapped_control_namesObject (readonly)

Returns the value of attribute mapped_control_names.



526
527
528
# File 'lib/abide-data-processor/parser.rb', line 526

def mapped_control_names
  @mapped_control_names
end

#nameObject (readonly)

Returns the value of attribute name.



526
527
528
# File 'lib/abide-data-processor/parser.rb', line 526

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



526
527
528
# File 'lib/abide-data-processor/parser.rb', line 526

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.



540
541
542
543
544
545
546
547
548
549
550
# 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) ⇒ 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.



578
579
580
# 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) ⇒ 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.



585
586
587
588
589
590
591
# 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_dataObject

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



554
555
556
557
558
559
560
561
562
563
564
565
# 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_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.



570
571
572
573
# File 'lib/abide-data-processor/parser.rb', line 570

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