class RuboCop::Cop::Chef::Deprecations::ResourceUsesOnlyResourceName
Starting with Chef
Infra Client 16, using ‘resource_name` without also using `provides` will result in resource failures. Make sure to use both `resource_name` and `provides` to change the name of the resource. You can also omit `resource_name` entirely if the value set matches the name Chef
Infra Client automatically assigns based on COOKBOOKNAME_FILENAME.
@example
#### incorrect mycookbook/resources/myresource.rb: resource_name :mycookbook_myresource
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
cookbook_name()
click to toggle source
determine the cookbook name either by parsing metadata.rb or by parsing metadata.json
@return [String] the cookbook name
# File lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb, line 47 def cookbook_name cb_path = File.expand_path(File.join(processed_source.file_path, '../..')) if File.exist?(File.join(cb_path, 'metadata.rb')) cb_metadata_ast = ProcessedSource.from_file(File.join(cb_path, 'metadata.rb'), @config.target_ruby_version).ast cb_name_match(cb_metadata_ast).first elsif File.exist?(File.join(cb_path, 'metadata.json')) # this exists only for supermarket files that lack metadata.rb JSON.parse(File.read(File.join(cb_path, 'metadata.json')))['name'] end end
on_send(node)
click to toggle source
# File lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb, line 68 def on_send(node) resource_name?(node) do |name| return if valid_provides?(name) add_offense(node, severity: :warning) do |corrector| if name.to_s == "#{cookbook_name}_#{File.basename(processed_source.path, '.rb')}" corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left)) else corrector.insert_after(node.source_range, "\n#{node.source.gsub('resource_name', 'provides')}") end end end end
valid_provides?(resource_name)
click to toggle source
given a resource name make sure there’s a provides that matches that name
@return [TrueClass, FalseClass]
# File lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb, line 61 def valid_provides?(resource_name) provides_ast = provides(processed_source.ast) return false unless provides_ast provides_ast.include?(resource_name) end