Class: Puppet::Parser::Resource
- Includes:
- FileCollection::Lookup, YamlTrimmer, Resource::TypeCollectionHelper, Util, Util::Errors, Util::Logging, Util::MethodHelper, Util::Tagging
- Defined in:
- lib/puppet/parser/resource.rb
Overview
The primary difference between this class and its parent is that this class has rules on who can set parameters
Defined Under Namespace
Classes: Param
Constant Summary
Constants included from YamlTrimmer
Constants inherited from Resource
Resource::ATTRIBUTES, Resource::Reference
Instance Attribute Summary collapse
-
#catalog ⇒ Object
Returns the value of attribute catalog.
-
#evaluated ⇒ Object
Returns the value of attribute evaluated.
-
#exported ⇒ Object
Returns the value of attribute exported.
-
#override ⇒ Object
Returns the value of attribute override.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#rails_id ⇒ Object
Returns the value of attribute rails_id.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#source ⇒ Object
Returns the value of attribute source.
-
#translated ⇒ Object
Returns the value of attribute translated.
-
#virtual ⇒ Object
Returns the value of attribute virtual.
Attributes included from FileCollection::Lookup
Attributes inherited from Resource
#file, #line, #strict, #title, #type, #validate_parameters
Class Method Summary collapse
-
.relationship_parameter?(name) ⇒ Boolean
Determine whether the provided parameter name is a relationship parameter.
Instance Method Summary collapse
- #[](param) ⇒ Object
- #[]=(param, value) ⇒ Object
-
#add_edge_to_stage ⇒ Object
Process the stage metaparameter for a class.
- #eachparam ⇒ Object
- #environment ⇒ Object
-
#evaluate ⇒ Object
Retrieve the associated definition and evaluate it.
- #evaluated? ⇒ Boolean
-
#finish ⇒ Object
Do any finishing work on this object, called before evaluation or before storage/translation.
-
#finished? ⇒ Boolean
Has this resource already been finished?.
-
#initialize(*args) ⇒ Resource
constructor
A new instance of Resource.
-
#isomorphic? ⇒ Boolean
Is this resource modeling an isomorphic resource type?.
-
#merge(resource) ⇒ Object
Merge an override resource in.
-
#metaparam_compatibility_mode? ⇒ Boolean
Unless we’re running >= 0.25, we’re in compat mode.
- #name ⇒ Object
- #override? ⇒ Boolean
-
#path ⇒ Object
A temporary occasion, until I get paths in the scopes figured out.
-
#set_parameter(param, value = nil) ⇒ Object
Define a parameter in our resource.
- #to_hash ⇒ Object
-
#to_ral ⇒ Object
Convert this resource to a RAL resource.
-
#to_resource ⇒ Object
Create a Puppet::Resource instance from this parser resource.
-
#to_trans ⇒ Object
Translate our object to a transportable object.
-
#translated? ⇒ Boolean
Set up some boolean test methods.
Methods included from YamlTrimmer
Methods included from Util::Tagging
Methods included from Util::Logging
Methods included from Util::Errors
#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail
Methods included from Util::MethodHelper
#requiredopts, #set_options, #symbolize_options
Methods included from Util
activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, which, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Resource::TypeCollectionHelper
Methods included from FileCollection::Lookup
#file, #file=, #file_collection
Methods inherited from Resource
#==, #builtin?, #builtin_type?, #each, #environment=, from_pson, #include?, #inspect, #key_attributes, #ref, #resolve, #resource_type, #to_manifest, #to_pson, #to_pson_data_hash, #to_ref, #to_s, #to_trans_ref, #to_transobject, #uniqueness_key, #valid_parameter?, #validate_parameter, value_to_pson_data, #yaml_property_munge
Methods included from Util::Pson
Methods included from Indirector
Constructor Details
#initialize(*args) ⇒ Resource
Returns a new instance of Resource.
123 124 125 126 127 128 |
# File 'lib/puppet/parser/resource.rb', line 123 def initialize(*args) raise ArgumentError, "Resources require a scope" unless args.last[:scope] super @source ||= scope.source end |
Instance Attribute Details
#catalog ⇒ Object
Returns the value of attribute catalog.
24 25 26 |
# File 'lib/puppet/parser/resource.rb', line 24 def catalog @catalog end |
#evaluated ⇒ Object
Returns the value of attribute evaluated.
24 25 26 |
# File 'lib/puppet/parser/resource.rb', line 24 def evaluated @evaluated end |
#exported ⇒ Object
Returns the value of attribute exported.
26 27 28 |
# File 'lib/puppet/parser/resource.rb', line 26 def exported @exported end |
#override ⇒ Object
Returns the value of attribute override.
24 25 26 |
# File 'lib/puppet/parser/resource.rb', line 24 def override @override end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
26 27 28 |
# File 'lib/puppet/parser/resource.rb', line 26 def parameters @parameters end |
#rails_id ⇒ Object
Returns the value of attribute rails_id.
23 24 25 |
# File 'lib/puppet/parser/resource.rb', line 23 def rails_id @rails_id end |
#scope ⇒ Object
Returns the value of attribute scope.
23 24 25 |
# File 'lib/puppet/parser/resource.rb', line 23 def scope @scope end |
#source ⇒ Object
Returns the value of attribute source.
23 24 25 |
# File 'lib/puppet/parser/resource.rb', line 23 def source @source end |
#translated ⇒ Object
Returns the value of attribute translated.
24 25 26 |
# File 'lib/puppet/parser/resource.rb', line 24 def translated @translated end |
#virtual ⇒ Object
Returns the value of attribute virtual.
24 25 26 |
# File 'lib/puppet/parser/resource.rb', line 24 def virtual @virtual end |
Class Method Details
.relationship_parameter?(name) ⇒ Boolean
Determine whether the provided parameter name is a relationship parameter.
29 30 31 32 |
# File 'lib/puppet/parser/resource.rb', line 29 def self.relationship_parameter?(name) @relationship_names ||= Puppet::Type.relationship_params.collect { |p| p.name } @relationship_names.include?(name) end |
Instance Method Details
#[](param) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/puppet/parser/resource.rb', line 39 def [](param) param = symbolize(param) if param == :title return self.title end if @parameters.has_key?(param) @parameters[param].value else nil end end |
#[]=(param, value) ⇒ Object
51 52 53 |
# File 'lib/puppet/parser/resource.rb', line 51 def []=(param, value) set_parameter(param, value) end |
#add_edge_to_stage ⇒ Object
Process the stage metaparameter for a class. A containment edge is drawn from the class to the stage. The stage for containment defaults to main, if none is specified.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/puppet/parser/resource.rb', line 68 def add_edge_to_stage return unless self.type.to_s.downcase == "class" unless stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main) raise ArgumentError, "Could not find stage #{self[:stage] || :main} specified by #{self}" end self[:stage] ||= stage.title unless stage.title == :main catalog.add_edge(stage, self) end |
#eachparam ⇒ Object
55 56 57 58 59 |
# File 'lib/puppet/parser/resource.rb', line 55 def eachparam @parameters.each do |name, param| yield param end end |
#environment ⇒ Object
61 62 63 |
# File 'lib/puppet/parser/resource.rb', line 61 def environment scope.environment end |
#evaluate ⇒ Object
Retrieve the associated definition and evaluate it.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/puppet/parser/resource.rb', line 80 def evaluate return if evaluated? @evaluated = true if klass = resource_type and ! builtin_type? finish evaluated_code = klass.evaluate_code(self) add_edge_to_stage return evaluated_code elsif builtin? devfail "Cannot evaluate a builtin type (#{type})" else self.fail "Cannot find definition #{type}" end end |
#evaluated? ⇒ Boolean
37 |
# File 'lib/puppet/parser/resource.rb', line 37 def evaluated?; !!@evaluated; end |
#finish ⇒ Object
Do any finishing work on this object, called before evaluation or before storage/translation.
109 110 111 112 113 114 115 116 |
# File 'lib/puppet/parser/resource.rb', line 109 def finish return if finished? @finished = true add_defaults validate end |
#finished? ⇒ Boolean
Has this resource already been finished?
119 120 121 |
# File 'lib/puppet/parser/resource.rb', line 119 def finished? @finished end |
#isomorphic? ⇒ Boolean
Is this resource modeling an isomorphic resource type?
131 132 133 134 135 136 137 |
# File 'lib/puppet/parser/resource.rb', line 131 def isomorphic? if builtin_type? return resource_type.isomorphic? else return true end end |
#merge(resource) ⇒ Object
Merge an override resource in. This will throw exceptions if any overrides aren’t allowed.
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/puppet/parser/resource.rb', line 141 def merge(resource) # Test the resource scope, to make sure the resource is even allowed # to override. unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source) raise Puppet::ParseError.new("Only subclasses can override parameters", resource.line, resource.file) end # Some of these might fail, but they'll fail in the way we want. resource.parameters.each do |name, param| override_parameter(param) end end |
#metaparam_compatibility_mode? ⇒ Boolean
Unless we’re running >= 0.25, we’re in compat mode.
154 155 156 |
# File 'lib/puppet/parser/resource.rb', line 154 def ! (catalog and ver = (catalog.client_version||'0.0.0').split(".") and (ver[0] > "0" or ver[1].to_i >= 25)) end |
#name ⇒ Object
158 159 160 |
# File 'lib/puppet/parser/resource.rb', line 158 def name self[:name] || self.title end |
#override? ⇒ Boolean
36 |
# File 'lib/puppet/parser/resource.rb', line 36 def override?; !!@override; end |
#path ⇒ Object
A temporary occasion, until I get paths in the scopes figured out.
163 164 165 |
# File 'lib/puppet/parser/resource.rb', line 163 def path to_s end |
#set_parameter(param, value = nil) ⇒ Object
Define a parameter in our resource. if we ever receive a parameter named ‘tag’, set the resource tags with its value.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/puppet/parser/resource.rb', line 170 def set_parameter(param, value = nil) if ! value.nil? param = Puppet::Parser::Resource::Param.new( :name => param, :value => value, :source => self.source ) elsif ! param.is_a?(Puppet::Parser::Resource::Param) raise ArgumentError, "Must pass a parameter or all necessary values" end tag(*param.value) if param.name == :tag # And store it in our parameter hash. @parameters[param.name] = param end |
#to_hash ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/puppet/parser/resource.rb', line 185 def to_hash @parameters.inject({}) do |hash, ary| param = ary[1] # Skip "undef" values. hash[param.name] = param.value if param.value != :undef hash end end |
#to_ral ⇒ Object
Convert this resource to a RAL resource. We hackishly go via the transportable stuff.
242 243 244 |
# File 'lib/puppet/parser/resource.rb', line 242 def to_ral to_resource.to_ral end |
#to_resource ⇒ Object
Create a Puppet::Resource instance from this parser resource. We plan, at some point, on not needing to do this conversion, but it’s sufficient for now.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/puppet/parser/resource.rb', line 198 def to_resource result = Puppet::Resource.new(type, title) to_hash.each do |p, v| if v.is_a?(Puppet::Resource) v = Puppet::Resource.new(v.type, v.title) elsif v.is_a?(Array) # flatten resource references arrays v = v.flatten if v.flatten.find { |av| av.is_a?(Puppet::Resource) } v = v.collect do |av| av = Puppet::Resource.new(av.type, av.title) if av.is_a?(Puppet::Resource) av end 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] = if v.is_a?(Array) and v.length == 1 v[0] else v end end result.file = self.file result.line = self.line result.exported = self.exported result.virtual = self.virtual result.tag(*self.) result end |
#to_trans ⇒ Object
Translate our object to a transportable object.
234 235 236 237 238 |
# File 'lib/puppet/parser/resource.rb', line 234 def to_trans return nil if virtual? to_resource.to_trans end |
#translated? ⇒ Boolean
Set up some boolean test methods
35 |
# File 'lib/puppet/parser/resource.rb', line 35 def translated?; !!@translated; end |