diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-01-05 12:03:07 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-01-05 12:03:07 -0800 |
commit | 52344a469bafbd3530d3ef225c5cf444e09ef956 (patch) | |
tree | 6e53923f9cbabcdd967a7d3c431fbbdf97bca21d | |
parent | 010392858c2a3a036578b681085704ed1971ab21 (diff) | |
download | chef-lcg/resource-sugar.tar.gz |
-rw-r--r-- | lib/chef/dsl/declare_resource.rb | 109 | ||||
-rw-r--r-- | lib/chef/resource_collection.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource_collection/resource_list.rb | 2 | ||||
-rw-r--r-- | spec/unit/recipe_spec.rb | 14 |
4 files changed, 120 insertions, 7 deletions
diff --git a/lib/chef/dsl/declare_resource.rb b/lib/chef/dsl/declare_resource.rb index 52b0cb715d..bd30f50241 100644 --- a/lib/chef/dsl/declare_resource.rb +++ b/lib/chef/dsl/declare_resource.rb @@ -22,6 +22,102 @@ require 'chef/exceptions' class Chef module DSL module DeclareResource + # + # Lookup a resource in the resource collection by name and remove it. This + # does not raise Chef::Exceptions::ResourceNotFound. + # + # @param type [Symbol] The type of resource (e.g. `:file` or `:package`) + # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2') + # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on + # + # @return [Chef::Resource] The resource + # + # @example + # remove_resource(:template, '/x/y.txy') + # + def remove_resource!(type, name, run_context: self.run_context) + resource = edit_resource(type, name, run_context: run_context) + #run_context.resource_collection.delete + #run_context.resource_collection.resource_list.delete + #run_context.resource_collection.resource_set.delete + pp run_context.resource_collection + pp run_context.resource_collection.resource_set.resources_by_key # Hash + pp run_context.resource_collection.resource_list.resources # Array + end + + # + # Lookup a resource in the resource collection by name and remove it. This + # does not raise Chef::Exceptions::ResourceNotFound. + # + # @param type [Symbol] The type of resource (e.g. `:file` or `:package`) + # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2') + # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on + # + # @return [Chef::Resource] The resource + # + # @example + # remove_resource(:template, '/x/y.txy') + # + def remove_resource(type, name, run_context: self.run_context) + remove_resource!(type, name, run_context: run_context) + rescue Chef::Exceptions::ResourceNotFound + # ignore + end + + # + # Lookup a resource in the resource collection by name. If it exists, + # return it. If it does not exist, create it. This is a useful function + # for accumulator patterns. + # + # @param type [Symbol] The type of resource (e.g. `:file` or `:package`) + # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2') + # @param created_at [String] The caller of the resource. Use `caller[0]` + # to get the caller of your function. Defaults to the caller of this + # function. + # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on + # @param resource_attrs_block A block that lets you set attributes of the + # resource (it is instance_eval'd on the resource instance). + # + # @return [Chef::Resource] The resource + # + # @example + # resource = declare_or_edit_resource(:template, '/x/y.txy') do + # source "y.txy.erb" + # variables {} + # end + # resource.variables.merge!({ home: "/home/klowns" }) + # + def declare_or_edit_resource(type, name, created_at=nil, run_context: self.run_context, &resource_attrs_block) + begin + resource = edit_resource(type, name, run_context: run_context, &resource_attrs_block) + return resource + rescue Chef::Exceptions::ResourceNotFound + end + declare_resource(type, name, created_at, run_context, &resource_attrs_block) + end + + # + # Lookup a resource in the resource collection by name and return it. This is + # functionally identical to "chef_rewind", plus you can pass it a different + # run_context if you like. May raise Chef::Exceptions::ResourceNotFound. + # + # @param type [Symbol] The type of resource (e.g. `:file` or `:package`) + # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2') + # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on + # @param resource_attrs_block A block that lets you set attributes of the + # resource (it is instance_eval'd on the resource instance). + # + # @return [Chef::Resource] The resource + # + # @example + # edit_resource(:template, '/x/y.txy') do + # cookbook_name: cookbook_name + # end + # + def edit_resource(type, name, run_context: self.run_context, &resource_attrs_block) + resource = run_context.resource_collection.find(type => name) + return resource + end # # Instantiates a resource (via #build_resource), then adds it to the @@ -34,6 +130,7 @@ class Chef # @param created_at [String] The caller of the resource. Use `caller[0]` # to get the caller of your function. Defaults to the caller of this # function. + # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on # @param resource_attrs_block A block that lets you set attributes of the # resource (it is instance_eval'd on the resource instance). # @@ -52,11 +149,9 @@ class Chef created_at ||= caller[0] if create_if_missing - begin - resource = run_context.resource_collection.find(type => name) - return resource - rescue Chef::Exceptions::ResourceNotFound - end + Chef::Log.deprecation "build_resource with a create_if_missing flag is deprecated, use declare_or_edit_resource instead" + # midly goofy since we call this only to re-call ourselves, but that's why its deprecated... + return declare_or_edit_resource(type, name, created_at, run_context: run_context, &resource_attrs_block) end resource = build_resource(type, name, created_at, &resource_attrs_block) @@ -76,6 +171,7 @@ class Chef # @param created_at [String] The caller of the resource. Use `caller[0]` # to get the caller of your function. Defaults to the caller of this # function. + # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on # @param resource_attrs_block A block that lets you set attributes of the # resource (it is instance_eval'd on the resource instance). # @@ -88,6 +184,9 @@ class Chef # def build_resource(type, name, created_at=nil, run_context: self.run_context, &resource_attrs_block) created_at ||= caller[0] + + # this needs to be lazy in order to avoid circular dependencies since ResourceBuilder + # will requires the entire provider+resolver universe Thread.exclusive do require 'chef/resource_builder' unless defined?(Chef::ResourceBuilder) end diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb index 4fd6fcad24..1e131197e9 100644 --- a/lib/chef/resource_collection.rb +++ b/lib/chef/resource_collection.rb @@ -33,7 +33,7 @@ class Chef extend Forwardable attr_reader :resource_set, :resource_list - private :resource_set, :resource_list + #private :resource_set, :resource_list def initialize @resource_set = ResourceSet.new diff --git a/lib/chef/resource_collection/resource_list.rb b/lib/chef/resource_collection/resource_list.rb index a26bd347aa..110c255c63 100644 --- a/lib/chef/resource_collection/resource_list.rb +++ b/lib/chef/resource_collection/resource_list.rb @@ -33,7 +33,7 @@ class Chef attr_reader :iterator attr_reader :resources - private :resources + # private :resources # Delegate direct access methods to the @resources array # 4 extra methods here are not included in the Enumerable's instance methods direct_access_methods = Enumerable.instance_methods + [ :[], :each, :each_index, :empty? ] diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 34f852e2ce..4e7e1df87c 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -277,6 +277,19 @@ describe Chef::Recipe do end + describe "deleting resources" do + before do + recipe.declare_resource(:zen_master, "klopp") do + something "bvb" + end + end + + it "deletes an existing resource via remove_resource!" do + recipe.remove_resource(:zen_master, "klopp") + expect(run_context.resource_collection.count).to eql(0) + end + end + describe "creating resources via declare_resource" do let(:zm_resource) do @@ -308,6 +321,7 @@ describe Chef::Recipe do it "does not insert two resources if create_if_missing is used" do zm_resource + expect(Chef::Log).to receive(:deprecation) recipe.declare_resource(:zen_master, "klopp", create_if_missing: true) expect(run_context.resource_collection.count).to eql(1) end |