Class: Puppet::Pops::Resource::ResourceTypeImpl

Inherits:
Object
  • Object
show all
Includes:
CompilableResourceType, Types::PuppetObject
Defined in:
lib/puppet/pops/resource/resource_type_impl.rb

Constant Summary collapse

METAPARAMS =
[
:noop,
:schedule,
:audit,
:loglevel,
:alias,
:tag,
:require,
:subscribe,
:before,
:notify,
:stage,
:export,
:consume
].freeze
METAPARAMSET =

Speed up lookup

Set.new(METAPARAMS).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type

Constructor Details

#initialize(name, properties = EMPTY_ARRAY, parameters = EMPTY_ARRAY, title_patterns_hash = nil, isomorphic = true, capability = false) ⇒ ResourceTypeImpl

Returns a new instance of ResourceTypeImpl.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 104

def initialize(name, properties = EMPTY_ARRAY, parameters = EMPTY_ARRAY, title_patterns_hash = nil, isomorphic = true, capability = false)
  @name = name
  @properties = properties
  @parameters = parameters
  @title_patterns_hash = title_patterns_hash
  @isomorphic = isomorphic
  @capability = capability

  # Compute attributes hash
  # Compute key_names (possibly compound key if there are multiple name vars).
  @attributes = {}
  @key_attributes = []

  # Name to kind of attribute
  @attr_types = {}

  # Add all meta params
  METAPARAMS.each {|p| @attr_types[p] = :meta }

  @property_set = Set.new(properties.map do |p|
    symname = p.name.to_sym
    @attributes[symname] = p
    @key_attributes << symname if p.name_var
    @attr_types[symname] = :property
    symname
  end).freeze

  @param_set = Set.new(parameters.map do |p|
    symname = p.name.to_sym
    @attributes[symname] = p
    @key_attributes << symname if p.name_var
    @attr_types[symname] = :param
    symname
  end).freeze

  # API for title patterns is [ [regexp, [ [ [sym, <lambda>], [sym, <lambda>] ] ] ] ]
  # Where lambdas are optional. This resource type impl does not support lambdas
  # Note that the pcore file has a simpler hashmap that is post processed here
  # since the structure must have Symbol instances for names which the .pp representation
  # does not deliver.
  #
  @title_patterns =
    case @key_attributes.length
    when 0
      # TechDebt: The case of specifying title patterns when having no name vars is unspecified behavior in puppet
      # Here it is silently ignored.
      nil
    when 1
      if @title_pattners_hash.nil?
        [ [ /(.*)/m, [ [@key_attributes.first] ] ] ]
      else
        # TechDebt: The case of having one namevar and an empty title patterns is unspecified behavior in puppet.
        # Here, it may lead to an empty map which may or may not trigger the wanted/expected behavior.
        #
        @title_patterns_hash.map { |k,v| [ k, v.map { |n| [ n.to_sym ] } ] }
      end
    else
      if @title_patterns_hash.nil? || @title_patterns_hash.empty?
        # TechDebt: While title patterns must be specified when more than one is used, they do not have
        # to match/set them all since some namevars can be omitted (to support the use case in
        # the 'package' type where 'provider' attribute is handled as part of the key without being
        # set from the title.
        #
        raise Puppet::DevError,"you must specify title patterns when there are two or more key attributes"
      end
      @title_patterns_hash.nil? ? [] : @title_patterns_hash.map { |k,v| [ k, v.map { |n| [ n.to_sym] } ] }
    end
end

Instance Attribute Details

#nameObject (readonly)



98
99
100
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 98

def name
  @name
end

#parametersObject (readonly)



100
101
102
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 100

def parameters
  @parameters
end

#propertiesObject (readonly)



99
100
101
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 99

def properties
  @properties
end

#title_patternsObject (readonly)



102
103
104
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 102

def title_patterns
  @title_patterns
end

#title_patterns_hashObject (readonly)



101
102
103
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 101

def title_patterns_hash
  @title_patterns_hash
end

Class Method Details

._pcore_typeObject

Returns the Puppet Type for this instance.



24
25
26
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 24

def self._pcore_type
  @ptype
end

.register_ptype(loader, ir) ⇒ Object



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
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 28

def self.register_ptype(loader, ir)
  param_ref = Types::PTypeReferenceType.new('Puppet::Resource::Param')
  @ptype = Pcore::create_object_type(loader, ir, self, 'Puppet::Resource::ResourceType3', nil,
    {
      Types::KEY_NAME => Types::PStringType::NON_EMPTY,
      'properties' => {
        Types::KEY_TYPE => Types::PArrayType.new(param_ref),
        Types::KEY_VALUE => EMPTY_ARRAY
      },
      'parameters' => {
        Types::KEY_TYPE => Types::PArrayType.new(param_ref),
        Types::KEY_VALUE => EMPTY_ARRAY
      },
      'title_patterns_hash' => {
        Types::KEY_TYPE => Types::POptionalType.new(
          Types::PHashType.new(Types::PRegexpType::DEFAULT, Types::PArrayType.new(Types::PStringType::NON_EMPTY))),
        Types::KEY_VALUE => nil
      },
      'isomorphic' => {
        Types::KEY_TYPE => Types::PBooleanType::DEFAULT,
        Types::KEY_VALUE => true
      },
      'capability' => {
        Types::KEY_TYPE => Types::PBooleanType::DEFAULT,
        Types::KEY_VALUE => false
      },
    },
    EMPTY_HASH,
    [Types::KEY_NAME]
  )
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Compares this type against the given other (type) and returns -1, 0, or +1 depending on the order.

Parameters:

  • other (Object)

    the object to compare against (produces nil, if not kind of Type}

Returns:

  • (-1, 0, +1, nil)

    produces -1 if this type is before the given other type, 0 if equals, and 1 if after. Returns nil, if the given other is not a kind of Type.

See Also:

  • Comparable


71
72
73
74
75
76
77
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 71

def <=>(other)
  # Order is only maintained against other types, not arbitrary objects.
  # The natural order is based on the reference name used when comparing
  return nil unless other.is_a?(Puppet::CompilableResourceType)
  # against other type instances.
  self.ref <=> other.ref
end

#allattrsObject

PROBABLY NOT USED WHEN COMPILING Returns the names of all attributes in a defined order:

  • all key attributes (the composite id)

  • :provider if it is specified

  • all properties

  • all parameters

  • meta parameters

Raises:

  • (NotImplementedError)


286
287
288
289
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 286

def allattrs
  raise NotImplementedError, "allattrs() - return all attribute names in order - probably not used master side"
  # key_attributes | (parameters & [:provider]) | properties.collect { |property| property.name } | parameters | metaparams
end

#application?Boolean

Applications are not supported

Returns:

  • (Boolean)


251
252
253
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 251

def application?
  false
end

#apply_toObject

Sets “applies to host”

Raises:

  • (NotImplementedError)


292
293
294
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 292

def apply_to
  raise NotImplementedError, "apply_to() - probably used when selecting a provider (device/host support)"
end

#apply_to_allObject

Raises:

  • (NotImplementedError)


304
305
306
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 304

def apply_to_all
  raise NotImplementedError, "apply_to_all() - probably used when selecting a provider (device/host support)"
end

#apply_to_deviceObject

Raises:

  • (NotImplementedError)


300
301
302
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 300

def apply_to_device
  raise NotImplementedError, "apply_to_device() - probably used when selecting a provider (device/host support)"
end

#apply_to_hostObject

Raises:

  • (NotImplementedError)


296
297
298
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 296

def apply_to_host
  raise NotImplementedError, "apply_to_host() - probably used when selecting a provider (device/host support)"
end

#attrclass(name) ⇒ Object

Returns the implementation of a param/property/attribute - i.e. a Param class

Raises:

  • (NotImplementedError)


274
275
276
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 274

def attrclass(name)
  raise NotImplementedError, "attrclass() - returns the (param) class of the parameter"
end

#attrtype(name) ⇒ Object

Answers :property, :param or :meta depending on the type of the attribute According to original version, this is called millions of times and a cache is required.

Parameters:

  • name (Symbol)

Raises:

  • (NotImplementedError)


268
269
270
271
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 268

def attrtype(name)
  raise NotImplementedError, "attrtype() - returns the kind (:meta, :param, or :property) of the parameter"
  # @attr_types[name]
end

#can_apply_to_target(target) ⇒ Object

Raises:

  • (NotImplementedError)


308
309
310
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 308

def can_apply_to_target(target)
  raise NotImplementedError, "can_apply_to_target() - probably used when selecting a provider (device/host support)"
end

#deprecate_params(title, attributes) ⇒ Object

Gives a type a chance to issue deprecations for parameters.

Parameters:

  • title (String)

    the title of the resource of this type

  • attributes (Array<Param>)

    the set parameters in the resource instance



241
242
243
244
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 241

def deprecate_params(title, attributes)
  # TODO: Current API somewhat unclear, if done at type level, or per
  #       Param.
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


60
61
62
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 60

def eql?(other)
  other.is_a?(Puppet::CompilableResourceType) && @name == other.name
end

#finishObject

The type implementation of finish does a lot of things

  • iterates over all parameters and calls post_compile on them if the parameter impl responds to post_compile

  • validates the relationship parameters

This implementation does nothing - it is assumed that the catalog is already validated via the relationship validator (done late in the game).



196
197
198
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 196

def finish()
  # Do nothing.
end

#instantiate_resource(scope, resource) ⇒ Object

This is called on a resource type it performs tagging if it is a Class or Node. It also ensure the parent type is in the catalog, but that is weird since resource types cannot really inherit



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 204

def instantiate_resource(scope, resource)
  # Do nothing because nothing is needed when compiling.

  # This is what the Puppet::Type implementation does
  # None of this should be needed

    #    # Make sure our parent class has been evaluated, if we have one.
    #    if parent && !scope.catalog.resource(resource.type, parent)
    #      parent_type(scope).ensure_in_catalog(scope)
    #    end

  # This will never happen

    #    if ['Class', 'Node'].include? resource.type
    #      scope.catalog.tag(*resource.tags)
    #    end
end

#is_3x_ruby_plugin?Boolean

Override CompilableResource inclusion

Returns:

  • (Boolean)


174
175
176
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 174

def is_3x_ruby_plugin?
  false
end

#is_capability?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 178

def is_capability?
  @capability
end

#isomorphic?Boolean

Being isomorphic in puppet means that the resource is managing a state (as opposed to a resource like Exec that is a function, possibly with side effect. In a Ruby implementation of a resource type, @isomorphic = false is used to turn off isomorphism, it is true by default. This is part of the Puppet::Type API.

Returns:

  • (Boolean)


228
229
230
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 228

def isomorphic?
  @isomorphic
end

#key_attributesObject

Produces the names of the attributes that make out the unique id of a resource



234
235
236
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 234

def key_attributes
  @key_attributes
end

#valid_parameter?(name) ⇒ Boolean

Answers if the parameter name is a parameter/attribute of this type This is part of the Puppet::Type API Check if used when compiling (it is triggered in an apply)

Returns:

  • (Boolean)


186
187
188
# File 'lib/puppet/pops/resource/resource_type_impl.rb', line 186

def valid_parameter?(name)
  @attributes.include?(name) || METAPARAMSET.include?(name)
end