summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2018-08-06 16:04:14 -0700
committerGitHub <noreply@github.com>2018-08-06 16:04:14 -0700
commita65a4636c6e736883603abc2526d796da1a73604 (patch)
tree53d2c223404e4c5a60e7ba027c27162eb5b21990 /lib
parent111bb019fed2eea16f43acd52635607f3a33373e (diff)
parenta2c1a49a7f7d7c712db2097a5bb581f956416ac2 (diff)
downloadchef-a65a4636c6e736883603abc2526d796da1a73604.tar.gz
Merge pull request #7524 from chef/lcg/provides-chef-version
add chef_version API to provides lines
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/node_map.rb7
-rw-r--r--lib/chef/resource.rb26
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb
index 62efbc12d6..a4891234f5 100644
--- a/lib/chef/node_map.rb
+++ b/lib/chef/node_map.rb
@@ -66,12 +66,13 @@ EOH
# override from cookbooks is allowed.
# @param __core_override__ [Boolean] Advanced-mode override to add to a key
# even in locked mode.
+ # @param chef_version [String] version constraint to match against the running Chef::VERSION
#
# @yield [node] Arbitrary node filter as a block which takes a node argument
#
# @return [NodeMap] Returns self for possible chaining
#
- def set(key, klass, platform: nil, platform_version: nil, platform_family: nil, os: nil, canonical: nil, override: nil, allow_cookbook_override: false, __core_override__: false, &block) # rubocop:disable Lint/UnderscorePrefixedVariableName
+ def set(key, klass, platform: nil, platform_version: nil, platform_family: nil, os: nil, canonical: nil, override: nil, allow_cookbook_override: false, __core_override__: false, chef_version: nil, &block) # rubocop:disable Lint/UnderscorePrefixedVariableName
new_matcher = { klass: klass }
new_matcher[:platform] = platform if platform
new_matcher[:platform_version] = platform_version if platform_version
@@ -83,6 +84,10 @@ EOH
new_matcher[:cookbook_override] = allow_cookbook_override
new_matcher[:core_override] = __core_override__
+ if chef_version && Chef::VERSION !~ chef_version
+ return map
+ end
+
# Check if the key is already present and locked, unless the override is allowed.
# The checks to see if we should reject, in order:
# 1. Core override mode is not set.
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index d341e401de..1440b2eb61 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -1305,6 +1305,28 @@ class Chef
end
#
+ # This API can be used for backcompat to do:
+ #
+ # chef_version_for_provides "< 14.0" if defined?(:chef_version_for_provides)
+ #
+ # For core chef versions that do not support chef_version: in provides lines.
+ #
+ # Since resource_name calls provides the generally correct way of doing this is
+ # to do `chef_version_for_provides` first, then `resource_name` and then
+ # any additional options `provides` lines. Calling `resource_name` is somewhat
+ # important to have the canonical_dsl removed or else that'll stick around
+ # and chef_version won't get applied to it.
+ #
+ # Once we no longer care about supporting chef < 14.4 then we can deprecate
+ # this API.
+ #
+ # @param arg [String] version constrant to match against (e.g. "> 14")
+ #
+ def self.chef_version_for_provides(constraint)
+ @chef_version_for_provides = constraint
+ end
+
+ #
# Mark this resource as providing particular DSL.
#
# Resources have an automatic DSL based on their resource_name, equivalent to
@@ -1327,6 +1349,10 @@ class Chef
options[:allow_cookbook_override] = true
end
+ if @chef_version_for_provides && !options.include?(:chef_version)
+ options[:chef_version] = @chef_version_for_provides
+ end
+
result = Chef.resource_handler_map.set(name, self, options, &block)
Chef::DSL::Resources.add_resource_dsl(name)
result