class Puppet::Pops::Resource::ResourceTypeImpl
Constants
- METAPARAMS
- METAPARAMSET
Speed up lookup
Attributes
Public Class Methods
# File lib/puppet/pops/resource/resource_type_impl.rb 103 def initialize(name, properties = EMPTY_ARRAY, parameters = EMPTY_ARRAY, title_patterns_hash = nil, isomorphic = true, capability = false) 104 @name = name 105 @properties = properties 106 @parameters = parameters 107 @title_patterns_hash = title_patterns_hash 108 @isomorphic = isomorphic 109 # ignore capability parameter 110 111 # Compute attributes hash 112 # Compute key_names (possibly compound key if there are multiple name vars). 113 @attributes = {} 114 @key_attributes = [] 115 116 # Name to kind of attribute 117 @attr_types = {} 118 119 # Add all meta params 120 METAPARAMS.each {|p| @attr_types[p] = :meta } 121 122 @property_set = Set.new(properties.map do |p| 123 symname = p.name.to_sym 124 @attributes[symname] = p 125 @key_attributes << symname if p.name_var 126 @attr_types[symname] = :property 127 symname 128 end).freeze 129 130 @param_set = Set.new(parameters.map do |p| 131 symname = p.name.to_sym 132 @attributes[symname] = p 133 @key_attributes << symname if p.name_var 134 @attr_types[symname] = :param 135 symname 136 end).freeze 137 138 # API for title patterns is [ [regexp, [ [ [sym, <lambda>], [sym, <lambda>] ] ] ] ] 139 # Where lambdas are optional. This resource type impl does not support lambdas 140 # Note that the pcore file has a simpler hashmap that is post processed here 141 # since the structure must have Symbol instances for names which the .pp representation 142 # does not deliver. 143 # 144 @title_patterns = 145 case @key_attributes.length 146 when 0 147 # TechDebt: The case of specifying title patterns when having no name vars is unspecified behavior in puppet 148 # Here it is silently ignored. 149 nil 150 when 1 151 if @title_patterns_hash.nil? 152 [ [ /(.*)/m, [ [@key_attributes.first] ] ] ] 153 else 154 # TechDebt: The case of having one namevar and an empty title patterns is unspecified behavior in puppet. 155 # Here, it may lead to an empty map which may or may not trigger the wanted/expected behavior. 156 # 157 @title_patterns_hash.map { |k,v| [ k, v.map { |n| [ n.to_sym ] } ] } 158 end 159 else 160 if @title_patterns_hash.nil? || @title_patterns_hash.empty? 161 # TechDebt: While title patterns must be specified when more than one is used, they do not have 162 # to match/set them all since some namevars can be omitted (to support the use case in 163 # the 'package' type where 'provider' attribute is handled as part of the key without being 164 # set from the title. 165 # 166 raise Puppet::DevError, _("you must specify title patterns when there are two or more key attributes") 167 end 168 @title_patterns_hash.nil? ? [] : @title_patterns_hash.map { |k,v| [ k, v.map { |n| [ n.to_sym] } ] } 169 end 170 end
# File lib/puppet/pops/resource/resource_type_impl.rb 29 def self.register_ptype(loader, ir) 30 param_ref = Types::PTypeReferenceType.new('Puppet::Resource::Param') 31 @ptype = Pcore::create_object_type(loader, ir, self, 'Puppet::Resource::ResourceType3', nil, 32 { 33 Types::KEY_NAME => Types::PStringType::NON_EMPTY, 34 'properties' => { 35 Types::KEY_TYPE => Types::PArrayType.new(param_ref), 36 Types::KEY_VALUE => EMPTY_ARRAY 37 }, 38 'parameters' => { 39 Types::KEY_TYPE => Types::PArrayType.new(param_ref), 40 Types::KEY_VALUE => EMPTY_ARRAY 41 }, 42 'title_patterns_hash' => { 43 Types::KEY_TYPE => Types::POptionalType.new( 44 Types::PHashType.new(Types::PRegexpType::DEFAULT, Types::PArrayType.new(Types::PStringType::NON_EMPTY))), 45 Types::KEY_VALUE => nil 46 }, 47 'isomorphic' => { 48 Types::KEY_TYPE => Types::PBooleanType::DEFAULT, 49 Types::KEY_VALUE => true 50 }, 51 'capability' => { 52 Types::KEY_TYPE => Types::PBooleanType::DEFAULT, 53 Types::KEY_VALUE => false 54 }, 55 }, 56 EMPTY_HASH, 57 [Types::KEY_NAME] 58 ) 59 end
Public Instance Methods
Compares this type against the given other (type) and returns -1, 0, or +1 depending on the order. @param other [Object] the object to compare against (produces nil, if not kind of Type} @return [-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 Comparable
# File lib/puppet/pops/resource/resource_type_impl.rb 72 def <=>(other) 73 # Order is only maintained against other types, not arbitrary objects. 74 # The natural order is based on the reference name used when comparing 75 return nil unless other.is_a?(Puppet::CompilableResourceType) 76 # against other type instances. 77 self.ref <=> other.ref 78 end
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
# File lib/puppet/pops/resource/resource_type_impl.rb 267 def allattrs 268 raise NotImplementedError, "allattrs() - return all attribute names in order - probably not used master side" 269 # key_attributes | (parameters & [:provider]) | properties.collect { |property| property.name } | parameters | metaparams 270 end
Sets “applies to host”
# File lib/puppet/pops/resource/resource_type_impl.rb 273 def apply_to 274 raise NotImplementedError, "apply_to() - probably used when selecting a provider (device/host support)" 275 end
# File lib/puppet/pops/resource/resource_type_impl.rb 285 def apply_to_all 286 raise NotImplementedError, "apply_to_all() - probably used when selecting a provider (device/host support)" 287 end
# File lib/puppet/pops/resource/resource_type_impl.rb 281 def apply_to_device 282 raise NotImplementedError, "apply_to_device() - probably used when selecting a provider (device/host support)" 283 end
# File lib/puppet/pops/resource/resource_type_impl.rb 277 def apply_to_host 278 raise NotImplementedError, "apply_to_host() - probably used when selecting a provider (device/host support)" 279 end
Returns the implementation of a param/property/attribute - i.e. a Param class
# File lib/puppet/pops/resource/resource_type_impl.rb 255 def attrclass(name) 256 raise NotImplementedError, "attrclass() - returns the (param) class of the parameter" 257 end
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. @param name [Symbol]
# File lib/puppet/pops/resource/resource_type_impl.rb 249 def attrtype(name) 250 raise NotImplementedError, "attrtype() - returns the kind (:meta, :param, or :property) of the parameter" 251 # @attr_types[name] 252 end
# File lib/puppet/pops/resource/resource_type_impl.rb 289 def can_apply_to_target(target) 290 raise NotImplementedError, "can_apply_to_target() - probably used when selecting a provider (device/host support)" 291 end
Gives a type a chance to issue deprecations for parameters. @param title [String] the title of the resource of this type @param attributes [Array<Param>] the set parameters in the resource instance
# File lib/puppet/pops/resource/resource_type_impl.rb 236 def deprecate_params(title, attributes) 237 # TODO: Current API somewhat unclear, if done at type level, or per 238 # Param. 239 end
# File lib/puppet/pops/resource/resource_type_impl.rb 61 def eql?(other) 62 other.is_a?(Puppet::CompilableResourceType) && @name == other.name 63 end
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).
# File lib/puppet/pops/resource/resource_type_impl.rb 191 def finish() 192 # Do nothing. 193 end
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
# File lib/puppet/pops/resource/resource_type_impl.rb 199 def instantiate_resource(scope, resource) 200 # Do nothing because nothing is needed when compiling. 201 202 # This is what the Puppet::Type implementation does 203 # None of this should be needed 204 205 # # Make sure our parent class has been evaluated, if we have one. 206 # if parent && !scope.catalog.resource(resource.type, parent) 207 # parent_type(scope).ensure_in_catalog(scope) 208 # end 209 210 # This will never happen 211 212 # if ['Class', 'Node'].include? resource.type 213 # scope.catalog.tag(*resource.tags) 214 # end 215 end
Override CompilableResource inclusion
# File lib/puppet/pops/resource/resource_type_impl.rb 173 def is_3x_ruby_plugin? 174 false 175 end
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.
# File lib/puppet/pops/resource/resource_type_impl.rb 223 def isomorphic? 224 @isomorphic 225 end
Produces the names of the attributes that make out the unique id of a resource
# File lib/puppet/pops/resource/resource_type_impl.rb 229 def key_attributes 230 @key_attributes 231 end
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)
# File lib/puppet/pops/resource/resource_type_impl.rb 181 def valid_parameter?(name) 182 @attributes.include?(name) || METAPARAMSET.include?(name) 183 end