Module: Puppet::Pops::Evaluator::Runtime3ResourceSupport Private

Defined in:
lib/puppet/pops/evaluator/runtime3_resource_support.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

CLASS_STRING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'class'

Class Method Summary collapse

Class Method Details

.create_resources(file, line, scope, virtual, exported, type_name, resource_titles, evaluated_parameters) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/pops/evaluator/runtime3_resource_support.rb', line 9

def self.create_resources(file, line, scope, virtual, exported, type_name, resource_titles, evaluated_parameters)
  env = scope.environment
  #    loader = Adapters::LoaderAdapter.loader_for_model_object(o, scope)

  if type_name.is_a?(String) && type_name.casecmp(CLASS_STRING) == 0
    # Resolve a 'class' and its titles
    resource_titles = resource_titles.collect do |a_title|
      hostclass = env.known_resource_types.find_hostclass(a_title)
      hostclass ? hostclass.name : a_title
    end
    # resolved type is just the string CLASS
    resolved_type = CLASS_STRING
  else
    # resolve a resource type - pcore based, ruby impl or user defined
    resolved_type = find_resource_type(scope, type_name)
  end

  # TODO: Unknown resource causes creation of Resource to fail with ArgumentError, should give
  # a proper Issue. Now the result is "Error while evaluating a Resource Statement" with the message
  # from the raised exception. (It may be good enough).
  unless resolved_type
    # TODO: do this the right way
    raise ArgumentError, _("Unknown resource type: '%{type}'") % { type: type_name }
  end

  # Build a resource for each title - use the resolved *type* as opposed to a reference
  # as this makes the created resource retain the type instance.
  #
  resource_titles.map do |resource_title|
    resource = Puppet::Parser::Resource.new(
      resolved_type, resource_title,
      :parameters => evaluated_parameters,
      :file => file,
      :line => line,
      :kind => Puppet::Resource.to_kind(resolved_type),
      :exported => exported,
      :virtual => virtual,
      # WTF is this? Which source is this? The file? The name of the context ?
      :source => scope.source,
      :scope => scope,
      :strict => true
    )

    # If this resource type supports inheritance (e.g. 'class') the parent chain must be walked
    # This impl delegates to the resource type to figure out what is needed.
    #
    if resource.resource_type.is_a? Puppet::Resource::Type
      resource.resource_type.instantiate_resource(scope, resource)
    end

    scope.compiler.add_resource(scope, resource)

    # Classes are evaluated immediately
    scope.compiler.evaluate_classes([resource_title], scope, false) if resolved_type == CLASS_STRING

    # Turn the resource into a PTypeType (a reference to a resource type)
    # weed out nil's
    resource_to_ptype(resource)
  end
end

.find_hostclass(scope, class_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



90
91
92
# File 'lib/puppet/pops/evaluator/runtime3_resource_support.rb', line 90

def self.find_hostclass(scope, class_name)
  scope.environment.known_resource_types.find_hostclass(class_name)
end

.find_main_class(scope) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
# File 'lib/puppet/pops/evaluator/runtime3_resource_support.rb', line 85

def self.find_main_class(scope)
  # Find the main class (known as ''), it does not have to be in the catalog
  scope.environment.known_resource_types.find_hostclass('')
end

.find_resource_type(scope, type_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
# File 'lib/puppet/pops/evaluator/runtime3_resource_support.rb', line 70

def self.find_resource_type(scope, type_name)
  find_builtin_resource_type(scope, type_name) || find_defined_resource_type(scope, type_name)
end

.find_resource_type_or_class(scope, name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/puppet/pops/evaluator/runtime3_resource_support.rb', line 74

def self.find_resource_type_or_class(scope, name)
  find_builtin_resource_type(scope, name) || find_defined_resource_type(scope, name) || find_hostclass(scope, name)
end

.resource_to_ptype(resource) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
83
# File 'lib/puppet/pops/evaluator/runtime3_resource_support.rb', line 78

def self.resource_to_ptype(resource)
  return nil if resource.nil?

  # inference returns the meta type since the 3x Resource is an alternate way to describe a type
  Puppet::Pops::Types::TypeCalculator.singleton().infer(resource).type
end