Module: EasyType::Provider::ClassMethods

Defined in:
lib/easy_type/provider.rb

Overview

nodoc

Instance Method Summary collapse

Instance Method Details

#instancesArray

Retrieve the raw_resource information by calling the`get_raw_resources` method on the Type. Map every element of this Array to a Puppet resource. and return this mapped Array

Returns:

  • (Array)

    Mapped Array of resources

Raises:

  • (Puppet::Error)

    When ‘get_raw_resources` is not defined on the type.



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/easy_type/provider.rb', line 517

def instances
  load_identity
  ::Entitlement::Entitlement.entitled?("#{module_name}##{type_name}#index")
  raise("information: to_get_raw_resources not defined on type #{resource_type.name}") unless defined?(resource_type.get_raw_resources)
  raw_resources = resource_type.get_raw_resources
  #
  # This is a hackish way to find out if we are in the type generation mode. If we are, we only
  # want to fetch the :ensure property. We do this to speed up auto purging on machines where
  # fetching the properties take a lot of time.
  #
  properties_to_fetch = caller.any? { |c| c.scan(/resources.rb.*generate'$/).first } ? [:ensure] : []
  raw_resources.map do |raw_resource|
    map_raw_to_resource(raw_resource, properties_to_fetch)
  end
end

#load_identityObject



555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/easy_type/provider.rb', line 555

def load_identity
  provider_name = to_s.split('::')[2].downcase
  reloaded = false
  begin
    require "puppet/provider/#{provider_name}/.identity"
  rescue LoadError => e
    raise e if reloaded
    # Use puppet plugin to download any created identity files
    `puppet plugin download`
    reloaded = true
    retry
  end
end

#map_raw_to_resource(raw_resource, selection = []) ⇒ Object

Creates a resource with specfied properties based on the value of property values returned by the translate_to_resource methods on the parameters and properies.

The selection array allows you to fetch only a subset of the properties and parametsr

rubocop: disable Metrics/MethodLength



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/easy_type/provider.rb', line 577

def map_raw_to_resource(raw_resource, selection = [])
  resource = {}
  selected_properties = if selection.empty?
                          Puppet.debug 'Fetching all enabled properties.'
                          non_meta_parameter_classes
                        else
                          Puppet.debug "Fetching only #{selection.join(',')} properties."
                          property_classes_for(selection) << resource_type.paramclass(:name) # Use selected classes AND name
                        end

  selected_properties.each do |parameter_class|
    next unless translation?(parameter_class)
    translate_arity = parameter_class.method(:translate_to_resource).arity
    value = case translate_arity
    when 1, -1
      parameter_class.translate_to_resource(raw_resource)
    when 2
      parameter_class.translate_to_resource(raw_resource, resource)
    else
      raise ArgumentError, "wrong number of arguments (#{translate_arity} for 1 or 2)"
    end
    #
    # Let the parameter class coerce it to the correct data type
    #
    resource[parameter_class.name] = parameter_class.coerce(value) unless value.nil?
  end
  resource[:ensure] ||= :present
  new(resource)
end

#mk_resource_methodsObject

define a getter and a setter method for evert specified parameter and property in the type. Define the setter so it modifies the property_hash and the property_flush based on what the other provider methods expect.

Raises:

  • (Puppet::Error)


493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/easy_type/provider.rb', line 493

def mk_resource_methods
  attributes = [resource_type.validproperties, resource_type.parameters].flatten
  raise Puppet::Error, 'no parameters or properties defined. Probably an error' if attributes == [:provider]
  attributes.each do |attr|
    attr = attr.intern
    next if attr == :name
    define_method(attr) do
      @property_hash[attr] || :absent
    end

    define_method(attr.to_s + '=') do |value|
      @property_flush[attr] = value
    end
  end
end

#module_nameObject

Get the module name



477
478
479
# File 'lib/easy_type/provider.rb', line 477

def module_name
  @module_name
end

#prefetch(resources) ⇒ Array

Prefetch all information of the specified resource. Because we already have everything in the instances array, we just have to set the provider.

Returns:

  • (Array)

    of Puppet Resources



540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/easy_type/provider.rb', line 540

def prefetch(resources)
  if resource_type.respond_to?(:execute_prefetch)
    load_identity
    ::Entitlement::Entitlement.entitled?("#{module_name}##{type_name}#index", resources)
    return resource_type.execute_prefetch(resources, self)
  end
  objects = instances
  resources.each_key do |name|
    provider = objects.find { |object| object.name == name }
    resources[name].provider = provider if provider
  end
end

#type_nameObject

Get the type name



484
485
486
# File 'lib/easy_type/provider.rb', line 484

def type_name
  @type_name
end