summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Kantrowitz <noah@coderanger.net>2018-06-05 21:02:41 -0700
committerNoah Kantrowitz <noah@coderanger.net>2018-06-05 21:02:41 -0700
commit28d097d48a346826806186a78b17f520c8533c1a (patch)
tree0d1f44bc2705ac7e1ff18a71d284984a3a9ca4ce
parente13dd0665df1f9c11674d5dc99de108dbacd2f2a (diff)
downloadchef-28d097d48a346826806186a78b17f520c8533c1a.tar.gz
Streamline the map locking process with a resource level flag.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r--lib/chef/resource.rb22
-rw-r--r--spec/unit/resource_spec.rb34
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 05349b80e7..239c4ddc68 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -1161,6 +1161,22 @@ class Chef
end
end
+ # Set or return if this resource is in preview mode.
+ #
+ # This is used in Chef core as part of the process of migrating resources
+ # from a cookbook into core. It should be set to `true` when a cookbook
+ # resource is added to core, and then removed (set to `false`) in the next
+ # major release.
+ #
+ # @param value [nil, Boolean] If nil, get the current value. If not nil, set
+ # the value of the flag.
+ # @return [Boolean]
+ def self.preview_resource(value=nil)
+ @preview_resource = false unless defined?(@preview_resource)
+ @preview_resource = value unless value.nil?
+ @preview_resource
+ end
+
#
# Internal Resource Interface (for Chef)
#
@@ -1305,6 +1321,12 @@ class Chef
remove_canonical_dsl
end
+ # If a resource is in preview mode, set allow_cookbook_override on all its
+ # mappings by default.
+ if preview_resource && !options.include?(:allow_cookbook_override)
+ options[:allow_cookbook_override] = true
+ end
+
result = Chef.resource_handler_map.set(name, self, options, &block)
Chef::DSL::Resources.add_resource_dsl(name)
result
diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb
index 67b8f59ff4..7dc2c83c5d 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -1144,4 +1144,38 @@ end
it { is_expected.to eq [:two, :one] }
end
end
+
+ describe ".preview_resource" do
+ let(:klass) { Class.new(Chef::Resource) }
+
+ before do
+ allow(Chef::DSL::Resources).to receive(:add_resource_dsl).with(:test_resource)
+ end
+
+ it "defaults to false" do
+ expect(klass.preview_resource).to eq false
+ end
+
+ it "can be set to true" do
+ klass.preview_resource(true)
+ expect(klass.preview_resource).to eq true
+ end
+
+ it "does not affect provides by default" do
+ expect(Chef.resource_handler_map).to receive(:set).with(:test_resource, klass, {canonical: true})
+ klass.resource_name(:test_resource)
+ end
+
+ it "adds allow_cookbook_override when true" do
+ expect(Chef.resource_handler_map).to receive(:set).with(:test_resource, klass, {canonical: true, allow_cookbook_override: true})
+ klass.preview_resource(true)
+ klass.resource_name(:test_resource)
+ end
+
+ it "allows manually overriding back to false" do
+ expect(Chef.resource_handler_map).to receive(:set).with(:test_resource, klass, {allow_cookbook_override: false})
+ klass.preview_resource(true)
+ klass.provides(:test_resource, allow_cookbook_override: false)
+ end
+ end
end