Class: Puppet::Resource::Type
- Extended by:
- Indirector
- Includes:
- Util::Errors, Util::InlineDocs, Util::Warnings
- Defined in:
- lib/vendor/puppet/resource/type.rb
Constant Summary collapse
- RESOURCE_SUPERTYPES =
[:hostclass, :node, :definition]
Constants included from Indirector
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#behaves_like ⇒ Object
readonly
Returns the value of attribute behaves_like.
-
#code ⇒ Object
Returns the value of attribute code.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#file ⇒ Object
Returns the value of attribute file.
-
#line ⇒ Object
Returns the value of attribute line.
-
#module_name ⇒ Object
readonly
Returns the value of attribute module_name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#resource_type_collection ⇒ Object
Returns the value of attribute resource_type_collection.
-
#ruby_code ⇒ Object
Returns the value of attribute ruby_code.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#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
MQR TODO:.
- #set_arguments(arguments) ⇒ Object
-
#set_resource_parameters(resource, scope) ⇒ Object
Set any arguments passed by the resource as variables in the scope.
- #to_pson(*args) ⇒ Object
- #to_pson_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, notice_once, warnonce
Methods included from Util::InlineDocs
Constructor Details
#initialize(type, name, options = {}) ⇒ Type
Returns a new instance of Type.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/vendor/puppet/resource/type.rb', line 81 def initialize(type, name, = {}) @type = type.to_s.downcase.to_sym raise ArgumentError, "Invalid resource supertype '#{type}'" unless RESOURCE_SUPERTYPES.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]) @module_name = [:module_name] end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
17 18 19 |
# File 'lib/vendor/puppet/resource/type.rb', line 17 def arguments @arguments end |
#behaves_like ⇒ Object (readonly)
Returns the value of attribute behaves_like.
17 18 19 |
# File 'lib/vendor/puppet/resource/type.rb', line 17 def behaves_like @behaves_like end |
#code ⇒ Object
Returns the value of attribute code.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def code @code end |
#doc ⇒ Object
Returns the value of attribute doc.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def doc @doc end |
#file ⇒ Object
Returns the value of attribute file.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def file @file end |
#line ⇒ Object
Returns the value of attribute line.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def line @line end |
#module_name ⇒ Object (readonly)
Returns the value of attribute module_name.
17 18 19 |
# File 'lib/vendor/puppet/resource/type.rb', line 17 def module_name @module_name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
17 18 19 |
# File 'lib/vendor/puppet/resource/type.rb', line 17 def namespace @namespace end |
#parent ⇒ Object
Returns the value of attribute parent.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def parent @parent end |
#resource_type_collection ⇒ Object
Returns the value of attribute resource_type_collection.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def resource_type_collection @resource_type_collection end |
#ruby_code ⇒ Object
Returns the value of attribute ruby_code.
16 17 18 |
# File 'lib/vendor/puppet/resource/type.rb', line 16 def ruby_code @ruby_code end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
17 18 19 |
# File 'lib/vendor/puppet/resource/type.rb', line 17 def type @type end |
Class Method Details
.from_pson(data) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/vendor/puppet/resource/type.rb', line 27 def self.from_pson(data) name = data.delete('name') or raise ArgumentError, "Resource Type names must be specified" type = data.delete('type') || "definition" data = data.inject({}) { |result, ary| result[ary[0].intern] = ary[1]; result } new(type, name, data) end |
Instance Method Details
#child_of?(klass) ⇒ Boolean
Are we a child of the passed class? Do a recursive search up our parentage tree to figure it out.
57 58 59 60 61 |
# File 'lib/vendor/puppet/resource/type.rb', line 57 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.
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/vendor/puppet/resource/type.rb', line 148 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) if parameters parameters.each do |k,v| resource.set_parameter(k,v) end end 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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/vendor/puppet/resource/type.rb', line 64 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 code.safeevaluate(scope) if code evaluate_ruby_code(resource, scope) if ruby_code end |
#instantiate_resource(scope, resource) ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/vendor/puppet/resource/type.rb', line 173 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.
101 102 103 104 105 |
# File 'lib/vendor/puppet/resource/type.rb', line 101 def match(string) return string.to_s.downcase == name unless name_is_regex? @name =~ string end |
#merge(other) ⇒ Object
Add code from a new instance to our code.
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 |
# File 'lib/vendor/puppet/resource/type.rb', line 108 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 array_class = Puppet::Parser::AST::ASTArray self.code = array_class.new(:children => [self.code]) unless self.code.is_a?(array_class) if other.code.is_a?(array_class) code.children += other.code.children else code.children << other.code end end |
#name ⇒ Object
184 185 186 187 |
# File 'lib/vendor/puppet/resource/type.rb', line 184 def name return @name unless @name.is_a?(Regexp) @name.source.downcase.gsub(/[^-\w:.]/,'').sub(/^\.+/,'') end |
#name_is_regex? ⇒ Boolean
189 190 191 |
# File 'lib/vendor/puppet/resource/type.rb', line 189 def name_is_regex? @name.is_a?(Regexp) end |
#parent_type(scope = nil) ⇒ Object
MQR TODO:
The change(s) introduced by the fix for #4270 are mostly silly & should be removed, though we didn’t realize it at the time. If it can be established/ ensured that nodes never call parent_type and that resource_types are always (as they should be) members of exactly one resource_type_collection the following method could / should be replaced with:
def parent_type
@parent_type ||= parent && (
resource_type_collection.find_or_load([name],parent,type.to_sym) ||
fail Puppet::ParseError, "Could not find parent resource type '#{parent}' of type #{type} in #{resource_type_collection.environment}"
)
end
…and then the rest of the changes around passing in scope reverted.
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/vendor/puppet/resource/type.rb', line 210 def parent_type(scope = nil) return nil unless parent unless @parent_type raise "Must pass scope to parent_type when called first time" unless scope unless @parent_type = scope.environment.known_resource_types.send("find_#{type}", [name], parent) fail Puppet::ParseError, "Could not find parent resource type '#{parent}' of type #{type} in #{scope.environment}" end end @parent_type end |
#set_arguments(arguments) ⇒ Object
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/vendor/puppet/resource/type.rb', line 276 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.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/vendor/puppet/resource/type.rb', line 224 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.setvar(param.to_s, value) } set[param] = true end if @type == :hostclass scope.setvar("title", resource.title.to_s.downcase) unless set.include? :title scope.setvar("name", resource.name.to_s.downcase ) unless set.include? :name else scope.setvar("title", resource.title ) unless set.include? :title scope.setvar("name", resource.name ) unless set.include? :name end scope.setvar("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.setvar("caller_module_name", caller_name) end scope.class_set(self.name,scope) if hostclass? or node? # Verify that all required arguments are either present or # have been provided with defaults. arguments.each do |param, default| param = param.to_sym next if set.include?(param) # Even if 'default' is a false value, it's an AST value, so this works fine fail Puppet::ParseError, "Must pass #{param} to #{resource.ref}" unless default value = default.safeevaluate(scope) scope.setvar(param.to_s, value) # Set it in the resource, too, so the value makes it to the client. resource[param] = value end end |
#to_pson(*args) ⇒ Object
51 52 53 |
# File 'lib/vendor/puppet/resource/type.rb', line 51 def to_pson(*args) to_pson_data_hash.to_pson(*args) end |
#to_pson_data_hash ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vendor/puppet/resource/type.rb', line 36 def to_pson_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 data['arguments'] = arguments.dup unless arguments.empty? data['name'] = name data['type'] = type data end |
#valid_parameter?(param) ⇒ Boolean
Check whether a given argument is valid.
267 268 269 270 271 272 273 274 |
# File 'lib/vendor/puppet/resource/type.rb', line 267 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 |