diff options
author | Daniel DeLeo <dan@opscode.com> | 2011-03-17 12:24:26 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2011-03-17 12:24:26 -0700 |
commit | a35ad6a7f3eb4b58d30661e477df60fff3505b8c (patch) | |
tree | 4f9c35371c3cb44d77a474c4d12d1c3e3a3a395f | |
parent | d65d7ea14d5a8191c693aff46985f49ff5db04cd (diff) | |
parent | e202effbd4de4e64aaab810fb56ec706c31f5567 (diff) | |
download | chef-a35ad6a7f3eb4b58d30661e477df60fff3505b8c.tar.gz |
Merge branch 'CHEF-1879'
-rw-r--r-- | chef/lib/chef/resource.rb | 91 |
1 files changed, 49 insertions, 42 deletions
diff --git a/chef/lib/chef/resource.rb b/chef/lib/chef/resource.rb index 2dfa5a7374..c1668cd739 100644 --- a/chef/lib/chef/resource.rb +++ b/chef/lib/chef/resource.rb @@ -7,9 +7,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -77,7 +77,7 @@ F include Chef::Mixin::Language include Chef::Mixin::ConvertToClassName include Chef::Mixin::Deprecation - + attr_accessor :params attr_accessor :provider attr_accessor :allowed_actions @@ -97,7 +97,7 @@ F # Struct with a #resource and #action member attr_reader :immediate_notifications attr_reader :delayed_notifications - + def initialize(name, run_context=nil) @name = name @run_context = run_context @@ -144,7 +144,7 @@ F raise NoMethodError, "undefined method `#{method_symbol.to_s}' for #{self.class.to_s}" end end - + def load_prior_resource begin prior_resource = run_context.resource_collection.lookup(self.to_s) @@ -159,7 +159,7 @@ F true end end - + def supports(args={}) if args.any? @supports = args @@ -167,7 +167,7 @@ F @supports end end - + def provider(arg=nil) klass = if arg.kind_of?(String) || arg.kind_of?(Symbol) lookup_provider_constant(arg) @@ -180,7 +180,7 @@ F :kind_of => [ Class ] ) end - + def action(arg=nil) if arg action_list = arg.kind_of?(Array) ? arg : [ arg ] @@ -200,21 +200,21 @@ F @action end end - + def name(name=nil) set_if_args(@name, name) do raise ArgumentError, "name must be a string!" unless name.kind_of?(String) @name = name end end - + def noop(tf=nil) - set_if_args(@noop, tf) do + set_if_args(@noop, tf) do raise ArgumentError, "noop must be true or false!" unless tf == true || tf == false @noop = tf end end - + def ignore_failure(arg=nil) set_or_return( :ignore_failure, @@ -222,7 +222,7 @@ F :kind_of => [ TrueClass, FalseClass ] ) end - + def epic_fail(arg=nil) ignore_failure(arg) end @@ -243,7 +243,7 @@ F Chef::Log.debug "adding notification from resource #{self} to `#{resource.inspect}' => `#{notification.inspect}'" add_notification(action, resource, timing) end - end + end end rescue NoMethodError Chef::Log.fatal("Error processing notifies(#{args.inspect}) on #{self}") @@ -286,7 +286,7 @@ F def resources(*args) run_context.resource_collection.find(*args) end - + def subscribes(action, resources, timing=:delayed) resources = [resources].flatten resources.each do |resource| @@ -302,7 +302,7 @@ F return *args end end - + def to_s "#{@resource_name}[#{@name}]" end @@ -318,13 +318,20 @@ F end text << "end\n" end - - # Serialize this object as a hash + + def inspect + ivars = instance_variables.map { |ivar| ivar.to_sym } - HIDDEN_IVARS + ivars.inject("<#{to_s}") do |str, ivar| + str << " #{ivar}: #{instance_variable_get(ivar).inspect}" + end << ">" + end + + # Serialize this object as a hash def to_json(*a) instance_vars = Hash.new self.instance_variables.each do |iv| unless iv == "@run_context" - instance_vars[iv] = self.instance_variable_get(iv) + instance_vars[iv] = self.instance_variable_get(iv) end end results = { @@ -333,7 +340,7 @@ F } results.to_json(*a) end - + def to_hash instance_vars = Hash.new self.instance_variables.each do |iv| @@ -342,7 +349,7 @@ F end instance_vars end - + def only_if(arg=nil, args = {}, &blk) if Kernel.block_given? @only_if = blk @@ -353,7 +360,7 @@ F end @only_if end - + def not_if(arg=nil, args = {}, &blk) if Kernel.block_given? @not_if = blk @@ -364,7 +371,7 @@ F end @not_if end - + def run_action(action) # ensure that we don't leave @updated_by_last_action set to true # on accident @@ -403,13 +410,13 @@ F def updated_by_last_action? @updated_by_last_action end - + def updated? updated end class << self - + def json_create(o) resource = self.new(o["instance_vars"]["@name"]) o["instance_vars"].each do |k,v| @@ -417,9 +424,9 @@ F end resource end - + include Chef::Mixin::ConvertToClassName - + def attribute(attr_name, validation_opts={}) # This atrocity is the only way to support 1.8 and 1.9 at the same time # When you're ready to drop 1.8 support, do this: @@ -431,12 +438,12 @@ F end SHIM class_eval(shim_method) - + define_method("_set_or_return_#{attr_name.to_s}".to_sym) do |arg| set_or_return(attr_name.to_sym, arg, validation_opts) end end - + def build_from_file(cookbook_name, filename) rname = filename_to_qualified_string(cookbook_name, filename) @@ -444,33 +451,33 @@ F class_name = convert_to_class_name(rname) overriding = Chef::Resource.const_defined?(class_name) Chef::Log.info("#{class_name} light-weight resource already initialized -- overriding!") if overriding - + new_resource_class = Class.new self do |cls| - + # default initialize method that ensures that when initialize is finally # wrapped (see below), super is called in the event that the resource # definer does not implement initialize def initialize(name, run_context) super(name, run_context) end - + @actions_to_create = [] - + class << cls include Chef::Mixin::FromFile - + def actions_to_create @actions_to_create end - + define_method(:actions) do |*action_names| actions_to_create.push(*action_names) end end - + # load resource definition from file cls.class_from_file(filename) - + # create a new constructor that wraps the old one and adds the actions # specified in the DSL old_init = instance_method(:initialize) @@ -482,16 +489,16 @@ F allowed_actions.push(self.class.actions_to_create).flatten! end end - + # register new class as a Chef::Resource class_name = convert_to_class_name(rname) Chef::Resource.const_set(class_name, new_resource_class) Chef::Log.debug("Loaded contents of #{filename} into a resource named #{rname} defined in Chef::Resource::#{class_name}") - + new_resource_class end - - # Resources that want providers namespaced somewhere other than + + # Resources that want providers namespaced somewhere other than # Chef::Provider can set the namespace with +provider_base+ # Ex: # class MyResource < Chef::Resource @@ -502,7 +509,7 @@ F @provider_base ||= arg @provider_base ||= Chef::Provider end - + end private |