Class: RuboCop::Cop::Chef::Deprecations::LegacyNotifySyntax

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

Overview

Use the new-style notification syntax which allows you to notify resources defined later in a recipe or resource.

Examples:


### incorrect
template '/etc/www/configures-apache.conf' do
  notifies :restart, resources(service: 'apache')
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, resources(service: 'apache'), :immediately
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, resources(service: service_name_variable), :immediately
end

template '/etc/www/configures-apache.conf' do
  subscribes :restart, resources(service: service_name_variable), :immediately
end

### correct
template '/etc/www/configures-apache.conf' do
  notifies :restart, 'service[apache]'
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, 'service[apache]', :immediately
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, "service[#{service_name_variable}]", :immediately
end

template '/etc/www/configures-apache.conf' do
  subscribes :restart, "service[#{service_name_variable}]", :immediately
end

Constant Summary collapse

MSG =
'Use the new-style notification syntax which allows you to notify resources defined later in a recipe or resource.'
RESTRICT_ON_SEND =
[:notifies, :subscribes].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubocop/cop/chef/deprecation/legacy_notify_syntax.rb', line 70

def on_send(node)
  legacy_notify?(node) do |notify_type, action, type, name, timing|
    add_offense(node, severity: :warning) do |corrector|
      service_value = case name.type
                      when :str
                        "'#{type.source}[#{name.value}]'"
                      when :dstr
                        "\"#{type.source}[#{name.value}]\""
                      else
                        "\"#{type.source}[\#{#{name.source}}]\""
                      end
      new_val = +"#{notify_type} #{action.source}, #{service_value}"
      new_val << ", #{timing.first.source}" unless timing.empty?
      corrector.replace(node, new_val)
    end
  end
end