From 83ab18515911416ae3622dbcfd44c2ce56b76546 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Fri, 3 Aug 2018 14:38:03 -0700 Subject: add chef_version API to provides lines also adds the chef_version_for_provides helper method Signed-off-by: Lamont Granquist --- lib/chef/node_map.rb | 6 +++++- lib/chef/resource.rb | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb index 62efbc12d6..9c3d223181 100644 --- a/lib/chef/node_map.rb +++ b/lib/chef/node_map.rb @@ -71,7 +71,7 @@ EOH # # @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 +83,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..f65af0c7eb 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1304,6 +1304,26 @@ class Chef end 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. + # + def self.chef_version_for_provides(arg) + @chef_version_for_provides = arg + end + # # Mark this resource as providing particular DSL. # @@ -1327,6 +1347,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 -- cgit v1.2.1 From a2c1a49a7f7d7c712db2097a5bb581f956416ac2 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 6 Aug 2018 15:13:23 -0700 Subject: add rdoc and tests Signed-off-by: Lamont Granquist --- lib/chef/node_map.rb | 1 + lib/chef/resource.rb | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb index 9c3d223181..a4891234f5 100644 --- a/lib/chef/node_map.rb +++ b/lib/chef/node_map.rb @@ -66,6 +66,7 @@ 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 # diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index f65af0c7eb..1440b2eb61 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1320,8 +1320,10 @@ class Chef # Once we no longer care about supporting chef < 14.4 then we can deprecate # this API. # - def self.chef_version_for_provides(arg) - @chef_version_for_provides = arg + # @param arg [String] version constrant to match against (e.g. "> 14") + # + def self.chef_version_for_provides(constraint) + @chef_version_for_provides = constraint end # -- cgit v1.2.1