Class: RuboCop::Cop::Chef::Deprecations::DeprecatedSudoActions

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetChefVersion
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/deprecation/deprecated_sudo_actions.rb

Overview

The ‘sudo` resource in the sudo cookbook 5.0 (2018) or Chef Infra Client 14 and later have replaced the existing `:install` and `:remove` actions with `:create` and `:delete` actions to better match other resources in Chef Infra.

Examples:


### incorrect
sudo 'admins' do
  users 'bob'
  groups 'sysadmins, superusers'
  action :remove
end

### correct
sudo 'admins' do
  users 'bob'
  groups 'sysadmins, superusers'
  action :delete
end

Constant Summary collapse

MSG =
'The `sudo` resource in the sudo cookbook 5.0 (2018) or Chef Infra Client 14 and later have replaced the existing `:install` and `:remove` actions with `:create` and `:delete` actions to better match other resources in Chef Infra.'

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/chef/deprecation/deprecated_sudo_actions.rb', line 49

def on_block(node)
  match_property_in_resource?(:sudo, 'action', node) do |prop_node|
    next unless prop_node.arguments.first.sym_type?
    next unless [s(:sym, :install), s(:sym, :remove)].include?(prop_node.arguments.first)

    add_offense(prop_node, severity: :warning) do |corrector|
      corrector.replace(prop_node, prop_node.source
        .gsub('install', 'create')
        .gsub('remove', 'delete'))
    end
  end
end