Class: RuboCop::Cop::Chef::Deprecations::HWRPWithoutProvides

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/deprecation/hwrp_without_provides.rb

Overview

Chef Infra Client 16 and later a legacy HWRP resource must use ‘provides` to define how the resource is called in recipes or other resources. To maintain compatibility with Chef Infra Client < 16 use both `resource_name` and `provides`.

### correct when Chef Infra Client < 15 (but compatible with 16+ as well)
 class Chef
   class Resource
     class UlimitRule < Chef::Resource
       resource_name :ulimit_rule
       provides :ulimit_rule

       property :type, [Symbol, String], required: true
       property :item, [Symbol, String], required: true

       # additional resource code
     end
   end
 end

### correct when Chef Infra Client 16+
 class Chef
   class Resource
     class UlimitRule < Chef::Resource
       provides :ulimit_rule

       property :type, [Symbol, String], required: true
       property :item, [Symbol, String], required: true

       # additional resource code
     end
   end
 end

# better
Convert your legacy HWRPs to custom resources

Examples:


### incorrect
class Chef
  class Resource
    class UlimitRule < Chef::Resource
      property :type, [Symbol, String], required: true
      property :item, [Symbol, String], required: true

      # additional resource code
    end
  end
end

### incorrect
class Chef
  class Resource
    class UlimitRule < Chef::Resource
      resource_name :ulimit_rule

      property :type, [Symbol, String], required: true
      property :item, [Symbol, String], required: true

      # additional resource code
    end
  end
end

Constant Summary collapse

MSG =
'In Chef Infra Client 16 and later a legacy HWRP resource must use `provides` to define how the resource is called in recipes or other resources. To maintain compatibility with Chef Infra Client < 16 use both `resource_name` and `provides`.'

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#has_provides?Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubocop/cop/chef/deprecation/hwrp_without_provides.rb', line 118

def has_provides?
  provides_ast = provides(processed_source.ast)
  return false if provides_ast.count == 0

  resource_ast = resource_name(processed_source.ast)

  if resource_ast.count == 0
    true # no resource_name, but provides
  else
    # since we have a resource and provides make sure the there is a provides that
    # matches the resource name
    provides_ast.include?(resource_ast.first)
  end
end

#indentation(node) ⇒ Object



133
134
135
# File 'lib/rubocop/cop/chef/deprecation/hwrp_without_provides.rb', line 133

def indentation(node)
  node.source_range.source_line =~ /\S/
end

#on_class(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubocop/cop/chef/deprecation/hwrp_without_provides.rb', line 105

def on_class(node)
  return if has_provides?
  HWRP?(node) do |inherit|
    add_offense(inherit, severity: :warning) do |corrector|
      resource_name_ast(node) do |ast_match|
        # build a new string to add after that includes the new line and the proper indentation
        new_string = "\n" + ast_match.source.dup.gsub('resource_name', 'provides').prepend(' ' * indentation(ast_match))
        corrector.insert_after(ast_match.source_range, new_string)
      end
    end
  end
end