Class: Puppet::Resource::Type
- Extended by:
- Indirector
- Includes:
- Util::Errors, Util::Warnings
- Defined in:
- lib/puppet/resource/type.rb
Overview
Puppet::Resource::Type represents nodes, classes and defined types.
It has a standard format for external consumption, usable from the resource_type indirection via rest and the resource_type face. See the resource type schema description.
Constant Summary collapse
- RESOURCE_KINDS =
[:hostclass, :node, :definition]
- RESOURCE_KINDS_TO_EXTERNAL_NAMES =
Map the names used in our documentation to the names used internally
{ :hostclass => "class", :node => "node", :definition => "defined_type", }
- RESOURCE_EXTERNAL_NAMES_TO_KINDS =
RESOURCE_KINDS_TO_EXTERNAL_NAMES.invert
Constants included from Indirector
Instance Attribute Summary collapse
-
#argument_types ⇒ Hash<Symbol, Puppet::Pops::Types::PAnyType] map from name to type
readonly
Map from argument (aka parameter) names to Puppet Type.
- #arguments ⇒ Object readonly
- #behaves_like ⇒ Object readonly
- #code ⇒ Object
- #doc ⇒ Object
- #file ⇒ Object
- #line ⇒ Object
- #module_name ⇒ Object readonly
- #namespace ⇒ Object readonly
- #parent ⇒ Object
- #resource_type_collection ⇒ Object
-
#type ⇒ Object
readonly
This should probably be renamed to ‘kind’ eventually, in accordance with the changes made for serialization and API usability (#14137).
Class Method Summary collapse
Instance Method Summary collapse
- #assign_parameter_values(parameters, resource) ⇒ Object
-
#child_of?(klass) ⇒ Boolean
Are we a child of the passed class? Do a recursive search up our parentage tree to figure it out.
-
#ensure_in_catalog(scope, parameters = nil) ⇒ Object
Make an instance of the resource type, and place it in the catalog if it isn’t in the catalog already.
-
#evaluate_code(resource) ⇒ Object
Now evaluate the code associated with this class or definition.
-
#initialize(type, name, options = {}) ⇒ Type
constructor
A new instance of Type.
- #instantiate_resource(scope, resource) ⇒ Object
-
#match(string) ⇒ Object
This is only used for node names, and really only when the node name is a regexp.
-
#merge(other) ⇒ Object
Add code from a new instance to our code.
- #name ⇒ Object
- #name_is_regex? ⇒ Boolean
- #parent_type(scope = nil) ⇒ Object
-
#set_argument_types(name_to_type_hash) ⇒ Object
Sets the argument name to Puppet Type hash used for type checking.
- #set_arguments(arguments) ⇒ Object
-
#set_resource_parameters(resource, scope) ⇒ Object
Set any arguments passed by the resource as variables in the scope.
- #to_data_hash ⇒ Object
-
#valid_parameter?(param) ⇒ Boolean
Check whether a given argument is valid.
Methods included from Indirector
Methods included from Util::Errors
#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail
Methods included from Util::Warnings
clear_warnings, debug_once, notice_once, warnonce
Constructor Details
#initialize(type, name, options = {}) ⇒ Type
Returns a new instance of Type.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/puppet/resource/type.rb', line 123 def initialize(type, name, = {}) @type = type.to_s.downcase.to_sym raise ArgumentError, "Invalid resource supertype '#{type}'" unless RESOURCE_KINDS.include?(@type) name = convert_from_ast(name) if name.is_a?(Puppet::Parser::AST::HostName) set_name_and_namespace(name) [:code, :doc, :line, :file, :parent].each do |param| next unless value = [param] send(param.to_s + "=", value) end set_arguments([:arguments]) set_argument_types([:argument_types]) @match = nil @module_name = [:module_name] end |
Instance Attribute Details
#argument_types ⇒ Hash<Symbol, Puppet::Pops::Types::PAnyType] map from name to type (readonly)
Map from argument (aka parameter) names to Puppet Type
35 36 37 |
# File 'lib/puppet/resource/type.rb', line 35 def argument_types @argument_types end |
#arguments ⇒ Object (readonly)
30 31 32 |
# File 'lib/puppet/resource/type.rb', line 30 def arguments @arguments end |
#behaves_like ⇒ Object (readonly)
30 31 32 |
# File 'lib/puppet/resource/type.rb', line 30 def behaves_like @behaves_like end |
#module_name ⇒ Object (readonly)
30 31 32 |
# File 'lib/puppet/resource/type.rb', line 30 def module_name @module_name end |
#namespace ⇒ Object (readonly)
30 31 32 |
# File 'lib/puppet/resource/type.rb', line 30 def namespace @namespace end |
#resource_type_collection ⇒ Object
29 30 31 |
# File 'lib/puppet/resource/type.rb', line 29 def resource_type_collection @resource_type_collection end |
#type ⇒ Object (readonly)
This should probably be renamed to ‘kind’ eventually, in accordance with the changes
made for serialization and API usability (#14137). At the moment that seems like
it would touch a whole lot of places in the code, though. --cprice 2012-04-23
40 41 42 |
# File 'lib/puppet/resource/type.rb', line 40 def type @type end |
Class Method Details
.from_data_hash(data) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/puppet/resource/type.rb', line 50 def self.from_data_hash(data) name = data.delete('name') or raise ArgumentError, "Resource Type names must be specified" kind = data.delete('kind') || "definition" unless type = RESOURCE_EXTERNAL_NAMES_TO_KINDS[kind] raise ArgumentError, "Unsupported resource kind '#{kind}'" end data = data.inject({}) { |result, ary| result[ary[0].intern] = ary[1]; result } # External documentation uses "parameters" but the internal name # is "arguments" data[:arguments] = data.delete(:parameters) new(type, name, data) end |
Instance Method Details
#assign_parameter_values(parameters, resource) ⇒ Object
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/puppet/resource/type.rb', line 230 def assign_parameter_values(parameters, resource) return unless parameters # It'd be nice to assign default parameter values here, # but we can't because they often rely on local variables # created during set_resource_parameters. parameters.each do |name, value| resource.set_parameter name, value end end |
#child_of?(klass) ⇒ Boolean
Are we a child of the passed class? Do a recursive search up our parentage tree to figure it out.
89 90 91 92 93 |
# File 'lib/puppet/resource/type.rb', line 89 def child_of?(klass) return false unless parent return(klass == parent_type ? true : parent_type.child_of?(klass)) end |
#ensure_in_catalog(scope, parameters = nil) ⇒ Object
Make an instance of the resource type, and place it in the catalog if it isn’t in the catalog already. This is only possible for classes and nodes. No parameters are be supplied–if this is a parameterized class, then all parameters take on their default values.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/puppet/resource/type.rb', line 186 def ensure_in_catalog(scope, parameters=nil) type == :definition and raise ArgumentError, "Cannot create resources for defined resource types" resource_type = type == :hostclass ? :class : :node # Do nothing if the resource already exists; this makes sure we don't # get multiple copies of the class resource, which helps provide the # singleton nature of classes. # we should not do this for classes with parameters # if parameters are passed, we should still try to create the resource # even if it exists so that we can fail # this prevents us from being able to combine param classes with include if resource = scope.catalog.resource(resource_type, name) and !parameters return resource end resource = Puppet::Parser::Resource.new(resource_type, name, :scope => scope, :source => self) assign_parameter_values(parameters, resource) instantiate_resource(scope, resource) scope.compiler.add_resource(scope, resource) resource end |
#evaluate_code(resource) ⇒ Object
Now evaluate the code associated with this class or definition.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/puppet/resource/type.rb', line 96 def evaluate_code(resource) static_parent = evaluate_parent_type(resource) scope = static_parent || resource.scope scope = scope.newscope(:namespace => namespace, :source => self, :resource => resource) unless resource.title == :main scope.compiler.add_class(name) unless definition? set_resource_parameters(resource, scope) resource.add_edge_to_stage if code if @match # Only bother setting up the ephemeral scope if there are match variables to add into it begin elevel = scope.ephemeral_level scope.ephemeral_from(@match, file, line) code.safeevaluate(scope) ensure scope.unset_ephemeral_var(elevel) end else code.safeevaluate(scope) end end end |
#instantiate_resource(scope, resource) ⇒ Object
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/puppet/resource/type.rb', line 207 def instantiate_resource(scope, resource) # 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 if ['Class', 'Node'].include? resource.type scope.catalog.tag(*resource.) end end |
#match(string) ⇒ Object
This is only used for node names, and really only when the node name is a regexp.
146 147 148 149 150 |
# File 'lib/puppet/resource/type.rb', line 146 def match(string) return string.to_s.downcase == name unless name_is_regex? @match = @name.match(string) end |
#merge(other) ⇒ Object
Add code from a new instance to our code.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/puppet/resource/type.rb', line 153 def merge(other) fail "#{name} is not a class; cannot add code to it" unless type == :hostclass fail "#{other.name} is not a class; cannot add code from it" unless other.type == :hostclass fail "Cannot have code outside of a class/node/define because 'freeze_main' is enabled" if name == "" and Puppet.settings[:freeze_main] if parent and other.parent and parent != other.parent fail "Cannot merge classes with different parent classes (#{name} => #{parent} vs. #{other.name} => #{other.parent})" end # We know they're either equal or only one is set, so keep whichever parent is specified. self.parent ||= other.parent if other.doc self.doc ||= "" self.doc += other.doc end # This might just be an empty, stub class. return unless other.code unless self.code self.code = other.code return end self.code = Puppet::Parser::ParserFactory.code_merger.concatenate([self, other]) end |
#name ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/puppet/resource/type.rb', line 218 def name if type == :node && name_is_regex? "__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/,'').sub(/^\.+/,'')}" else @name end end |
#name_is_regex? ⇒ Boolean
226 227 228 |
# File 'lib/puppet/resource/type.rb', line 226 def name_is_regex? @name.is_a?(Regexp) end |
#parent_type(scope = nil) ⇒ Object
241 242 243 244 245 246 |
# File 'lib/puppet/resource/type.rb', line 241 def parent_type(scope = nil) return nil unless parent @parent_type ||= scope.environment.known_resource_types.send("find_#{type}", parent) || fail(Puppet::ParseError, "Could not find parent resource type '#{parent}' of type #{type} in #{scope.environment}") end |
#set_argument_types(name_to_type_hash) ⇒ Object
Sets the argument name to Puppet Type hash used for type checking. Names must correspond to available arguments (they must be defined first). Arguments not mentioned will not be type-checked.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/puppet/resource/type.rb', line 308 def set_argument_types(name_to_type_hash) @argument_types = {} return unless name_to_type_hash name_to_type_hash.each do |name, t| # catch internal errors unless @arguments.include?(name) raise Puppet::DevError, "Parameter '#{name}' is given a type, but is not a valid parameter." end unless t.is_a? Puppet::Pops::Types::PAnyType raise Puppet::DevError, "Parameter '#{name}' is given a type that is not a Puppet Type, got #{t.class}" end @argument_types[name] = t end end |
#set_arguments(arguments) ⇒ Object
293 294 295 296 297 298 299 300 301 302 |
# File 'lib/puppet/resource/type.rb', line 293 def set_arguments(arguments) @arguments = {} return if arguments.nil? arguments.each do |arg, default| arg = arg.to_s (arg, default) @arguments[arg] = default end end |
#set_resource_parameters(resource, scope) ⇒ Object
Set any arguments passed by the resource as variables in the scope.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/puppet/resource/type.rb', line 249 def set_resource_parameters(resource, scope) set = {} resource.to_hash.each do |param, value| param = param.to_sym fail Puppet::ParseError, "#{resource.ref} does not accept attribute #{param}" unless valid_parameter?(param) exceptwrap { scope[param.to_s] = value } set[param] = true end if @type == :hostclass scope["title"] = resource.title.to_s.downcase unless set.include? :title scope["name"] = resource.name.to_s.downcase unless set.include? :name else scope["title"] = resource.title unless set.include? :title scope["name"] = resource.name unless set.include? :name end scope["module_name"] = module_name if module_name and ! set.include? :module_name if caller_name = scope.parent_module_name and ! set.include?(:caller_module_name) scope["caller_module_name"] = caller_name end scope.class_set(self.name,scope) if hostclass? or node? # Evaluate the default parameters, now that all other variables are set default_params = resource.set_default_parameters(scope) default_params.each { |param| scope[param] = resource[param] } # This has to come after the above parameters so that default values # can use their values resource.validate_complete end |
#to_data_hash ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/puppet/resource/type.rb', line 67 def to_data_hash data = [:doc, :line, :file, :parent].inject({}) do |hash, param| next hash unless (value = self.send(param)) and (value != "") hash[param.to_s] = value hash end # External documentation uses "parameters" but the internal name # is "arguments" data['parameters'] = arguments.dup unless arguments.empty? data['name'] = name unless RESOURCE_KINDS_TO_EXTERNAL_NAMES.has_key?(type) raise ArgumentError, "Unsupported resource kind '#{type}'" end data['kind'] = RESOURCE_KINDS_TO_EXTERNAL_NAMES[type] data end |
#valid_parameter?(param) ⇒ Boolean
Check whether a given argument is valid.
284 285 286 287 288 289 290 291 |
# File 'lib/puppet/resource/type.rb', line 284 def valid_parameter?(param) param = param.to_s return true if param == "name" return true if Puppet::Type.(param) return false unless defined?(@arguments) return(arguments.include?(param) ? true : false) end |