class RuboCop::Cop::Chef::Deprecations::LegacyNotifySyntax

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

@example

#### 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

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# 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, message: MSG, 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