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