Class: Puppet::Resource
- Extended by:
- Indirector, Util::Pson
- Includes:
- Enumerable, TypeCollectionHelper, Util::Tagging
- Defined in:
- lib/vendor/puppet/resource.rb,
lib/vendor/puppet/resource/status.rb
Overview
The simplest resource class. Eventually it will function as the base class for all resource-like behaviour.
Direct Known Subclasses
Defined Under Namespace
Modules: TypeCollectionHelper Classes: ActiveRecord, Catalog, Ral, Rest, Status, StoreConfigs, Type, TypeCollection
Constant Summary collapse
- Reference =
This stub class is only needed for serialization compatibility with 0.25.x. Specifically, it exists to provide a compatibility API when using YAML serialized objects loaded from StoreConfigs.
Puppet::Resource
- ATTRIBUTES =
[:file, :line, :exported]
Constants included from Indirector
Instance Attribute Summary collapse
-
#catalog ⇒ Object
Returns the value of attribute catalog.
-
#exported ⇒ Object
Returns the value of attribute exported.
-
#file ⇒ Object
Returns the value of attribute file.
-
#line ⇒ Object
Returns the value of attribute line.
-
#strict ⇒ Object
Returns the value of attribute strict.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validate_parameters ⇒ Object
Returns the value of attribute validate_parameters.
-
#virtual ⇒ Object
Returns the value of attribute virtual.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](param) ⇒ Object
Return a given parameter’s value.
-
#[]=(param, value) ⇒ Object
Set a given parameter.
-
#builtin? ⇒ Boolean
Compatibility method.
-
#builtin_type? ⇒ Boolean
Is this a builtin resource type?.
-
#each ⇒ Object
Iterate over each param/value pair, as required for Enumerable.
-
#environment ⇒ Object
These two methods are extracted into a Helper module, but file load order prevents me from including them in the class, and I had weird behaviour (i.e., sometimes it didn’t work) when I directly extended each resource with the helper.
- #environment=(env) ⇒ Object
- #include?(parameter) ⇒ Boolean
-
#initialize(type, title = nil, attributes = {}) ⇒ Resource
constructor
Create our resource.
- #inspect ⇒ Object
- #key_attributes ⇒ Object
- #name ⇒ Object
- #prune_parameters(options = {}) ⇒ Object
- #ref ⇒ Object
-
#resolve ⇒ Object
Find our resource.
- #resource_type ⇒ Object
-
#to_hash ⇒ Object
Produce a simple hash of our parameters.
-
#to_manifest ⇒ Object
Convert our resource to Puppet code.
- #to_pson(*args) ⇒ Object
- #to_pson_data_hash ⇒ Object
-
#to_ral ⇒ Object
Convert our resource to a RAL resource instance.
- #to_ref ⇒ Object
- #to_resource ⇒ Object
- #to_s ⇒ Object
-
#to_trans ⇒ Object
Translate our object to a backward-compatible transportable object.
- #to_trans_ref ⇒ Object
-
#to_transobject ⇒ Object
Create an old-style TransObject instance, for builtin resource types.
- #uniqueness_key ⇒ Object
- #valid_parameter?(name) ⇒ Boolean
- #validate_parameter(name) ⇒ Object
- #yaml_property_munge(x) ⇒ Object
Methods included from Util::Pson
Methods included from Indirector
Methods included from TypeCollectionHelper
Methods included from Util::Tagging
Constructor Details
#initialize(type, title = nil, attributes = {}) ⇒ Resource
Create our resource.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/vendor/puppet/resource.rb', line 180 def initialize(type, title = nil, attributes = {}) @parameters = {} # Set things like strictness first. attributes.each do |attr, value| next if attr == :parameters send(attr.to_s + "=", value) end @type, @title = extract_type_and_title(type, title) @type = munge_type_name(@type) if @type == "Class" @title = :main if @title == "" @title = munge_type_name(@title) end if params = attributes[:parameters] extract_parameters(params) end tag(self.type) tag(self.title) if valid_tag?(self.title) @reference = self # for serialization compatibility with 0.25.x if strict? and ! resource_type if @type == 'Class' raise ArgumentError, "Could not find declared class #{title}" else raise ArgumentError, "Invalid resource type #{type}" end end end |
Instance Attribute Details
#catalog ⇒ Object
Returns the value of attribute catalog.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def catalog @catalog end |
#exported ⇒ Object
Returns the value of attribute exported.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def exported @exported end |
#file ⇒ Object
Returns the value of attribute file.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def file @file end |
#line ⇒ Object
Returns the value of attribute line.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def line @line end |
#strict ⇒ Object
Returns the value of attribute strict.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def strict @strict end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
22 23 24 |
# File 'lib/vendor/puppet/resource.rb', line 22 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
22 23 24 |
# File 'lib/vendor/puppet/resource.rb', line 22 def type @type end |
#validate_parameters ⇒ Object
Returns the value of attribute validate_parameters.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def validate_parameters @validate_parameters end |
#virtual ⇒ Object
Returns the value of attribute virtual.
21 22 23 |
# File 'lib/vendor/puppet/resource.rb', line 21 def virtual @virtual end |
Class Method Details
.from_pson(pson) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vendor/puppet/resource.rb', line 30 def self.from_pson(pson) raise ArgumentError, "No resource type provided in pson data" unless type = pson['type'] raise ArgumentError, "No resource title provided in pson data" unless title = pson['title'] resource = new(type, title) if params = pson['parameters'] params.each { |param, value| resource[param] = value } end if = pson['tags'] .each { |tag| resource.tag(tag) } end ATTRIBUTES.each do |a| if value = pson[a.to_s] resource.send(a.to_s + "=", value) end end resource.exported ||= false resource end |
Instance Method Details
#==(other) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/vendor/puppet/resource.rb', line 130 def ==(other) return false unless other.respond_to?(:title) and self.type == other.type and self.title == other.title return false unless to_hash == other.to_hash true end |
#[](param) ⇒ Object
Return a given parameter’s value. Converts all passed names to lower-case symbols.
126 127 128 |
# File 'lib/vendor/puppet/resource.rb', line 126 def [](param) parameters[parameter_name(param)] end |
#[]=(param, value) ⇒ Object
Set a given parameter. Converts all passed names to lower-case symbols.
119 120 121 122 |
# File 'lib/vendor/puppet/resource.rb', line 119 def []=(param, value) validate_parameter(param) if validate_parameters parameters[parameter_name(param)] = value end |
#builtin? ⇒ Boolean
Compatibility method.
138 139 140 |
# File 'lib/vendor/puppet/resource.rb', line 138 def builtin? builtin_type? end |
#builtin_type? ⇒ Boolean
Is this a builtin resource type?
143 144 145 |
# File 'lib/vendor/puppet/resource.rb', line 143 def builtin_type? resource_type.is_a?(Class) end |
#each ⇒ Object
Iterate over each param/value pair, as required for Enumerable.
148 149 150 |
# File 'lib/vendor/puppet/resource.rb', line 148 def each parameters.each { |p,v| yield p, v } end |
#environment ⇒ Object
These two methods are extracted into a Helper module, but file load order prevents me from including them in the class, and I had weird behaviour (i.e., sometimes it didn’t work) when I directly extended each resource with the helper.
161 162 163 |
# File 'lib/vendor/puppet/resource.rb', line 161 def environment Puppet::Node::Environment.new(@environment) end |
#environment=(env) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/vendor/puppet/resource.rb', line 165 def environment=(env) if env.is_a?(String) or env.is_a?(Symbol) @environment = env else @environment = env.name end end |
#include?(parameter) ⇒ Boolean
152 153 154 |
# File 'lib/vendor/puppet/resource.rb', line 152 def include?(parameter) super || parameters.keys.include?( parameter_name(parameter) ) end |
#inspect ⇒ Object
55 56 57 |
# File 'lib/vendor/puppet/resource.rb', line 55 def inspect "#{@type}[#{@title}]#{to_hash.inspect}" end |
#key_attributes ⇒ Object
250 251 252 |
# File 'lib/vendor/puppet/resource.rb', line 250 def key_attributes return(resource_type.respond_to? :key_attributes) ? resource_type.key_attributes : [:name] end |
#name ⇒ Object
336 337 338 339 340 341 |
# File 'lib/vendor/puppet/resource.rb', line 336 def name # this is potential namespace conflict # between the notion of an "indirector name" # and a "resource name" [ type, title ].join('/') end |
#prune_parameters(options = {}) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/vendor/puppet/resource.rb', line 355 def prune_parameters( = {}) properties = resource_type.properties.map(&:name) dup.collect do |attribute, value| if value.to_s.empty? or Array(value).empty? delete(attribute) elsif value.to_s == "absent" and attribute.to_s != "ensure" delete(attribute) end parameters_to_include = [:parameters_to_include] || [] delete(attribute) unless properties.include?(attribute) || parameters_to_include.include?(attribute) end self end |
#ref ⇒ Object
215 216 217 |
# File 'lib/vendor/puppet/resource.rb', line 215 def ref to_s end |
#resolve ⇒ Object
Find our resource.
220 221 222 |
# File 'lib/vendor/puppet/resource.rb', line 220 def resolve return(catalog ? catalog.resource(to_s) : nil) end |
#resource_type ⇒ Object
224 225 226 227 228 229 230 231 |
# File 'lib/vendor/puppet/resource.rb', line 224 def resource_type case type when "Class"; known_resource_types.hostclass(title == :main ? "" : title) when "Node"; known_resource_types.node(title) else Puppet::Type.type(type.to_s.downcase.to_sym) || known_resource_types.definition(type) end end |
#to_hash ⇒ Object
Produce a simple hash of our parameters.
234 235 236 |
# File 'lib/vendor/puppet/resource.rb', line 234 def to_hash parse_title.merge parameters end |
#to_manifest ⇒ Object
Convert our resource to Puppet code.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/vendor/puppet/resource.rb', line 255 def to_manifest # Collect list of attributes to align => and move ensure first attr = parameters.keys attr_max = attr.inject(0) { |max,k| k.to_s.length > max ? k.to_s.length : max } attr.sort! if attr.first != :ensure && attr.include?(:ensure) attr.delete(:ensure) attr.unshift(:ensure) end attributes = attr.collect { |k| v = parameters[k] " %-#{attr_max}s => %s,\n" % [k, Puppet::Parameter.format_value_for_display(v)] }.join "%s { '%s':\n%s}" % [self.type.to_s.downcase, self.title, attributes] end |
#to_pson(*args) ⇒ Object
105 106 107 |
# File 'lib/vendor/puppet/resource.rb', line 105 def to_pson(*args) to_pson_data_hash.to_pson(*args) end |
#to_pson_data_hash ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vendor/puppet/resource.rb', line 59 def to_pson_data_hash data = ([:type, :title, :tags] + ATTRIBUTES).inject({}) do |hash, param| next hash unless value = self.send(param) hash[param.to_s] = value hash end data["exported"] ||= false params = self.to_hash.inject({}) do |hash, ary| param, value = ary # Don't duplicate the title as the namevar next hash if param == namevar and value == title hash[param] = Puppet::Resource.value_to_pson_data(value) hash end data["parameters"] = params unless params.empty? data end |
#to_ral ⇒ Object
Convert our resource to a RAL resource instance. Creates component instances for resource types that don’t exist.
280 281 282 283 284 285 286 |
# File 'lib/vendor/puppet/resource.rb', line 280 def to_ral if typeklass = Puppet::Type.type(self.type) return typeklass.new(self) else return Puppet::Type::Component.new(self) end end |
#to_ref ⇒ Object
274 275 276 |
# File 'lib/vendor/puppet/resource.rb', line 274 def to_ref ref end |
#to_resource ⇒ Object
343 344 345 |
# File 'lib/vendor/puppet/resource.rb', line 343 def to_resource self end |
#to_s ⇒ Object
238 239 240 |
# File 'lib/vendor/puppet/resource.rb', line 238 def to_s "#{type}[#{title}]" end |
#to_trans ⇒ Object
Translate our object to a backward-compatible transportable object.
289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/vendor/puppet/resource.rb', line 289 def to_trans if builtin_type? and type.downcase.to_s != "stage" result = to_transobject else result = to_transbucket end result.file = self.file result.line = self.line result end |
#to_trans_ref ⇒ Object
302 303 304 |
# File 'lib/vendor/puppet/resource.rb', line 302 def to_trans_ref [type.to_s, title.to_s] end |
#to_transobject ⇒ Object
Create an old-style TransObject instance, for builtin resource types.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/vendor/puppet/resource.rb', line 307 def to_transobject # Now convert to a transobject result = Puppet::TransObject.new(title, type) to_hash.each do |p, v| if v.is_a?(Puppet::Resource) v = v.to_trans_ref elsif v.is_a?(Array) v = v.collect { |av| av = av.to_trans_ref if av.is_a?(Puppet::Resource) av } end # If the value is an array with only one value, then # convert it to a single value. This is largely so that # the database interaction doesn't have to worry about # whether it returns an array or a string. result[p.to_s] = if v.is_a?(Array) and v.length == 1 v[0] else v end end result. = self. result end |
#uniqueness_key ⇒ Object
242 243 244 245 246 247 248 |
# File 'lib/vendor/puppet/resource.rb', line 242 def uniqueness_key # Temporary kludge to deal with inconsistant use patters h = self.to_hash h[namevar] ||= h[:name] h[:name] ||= h[namevar] h.values_at(*key_attributes.sort_by { |k| k.to_s }) end |
#valid_parameter?(name) ⇒ Boolean
347 348 349 |
# File 'lib/vendor/puppet/resource.rb', line 347 def valid_parameter?(name) resource_type.valid_parameter?(name) end |
#validate_parameter(name) ⇒ Object
351 352 353 |
# File 'lib/vendor/puppet/resource.rb', line 351 def validate_parameter(name) raise ArgumentError, "Invalid parameter #{name}" unless valid_parameter?(name) end |