class RuboCop::Cop::Chef::Modernize::UseChefLanguageCloudHelpers

Chef Infra Client 15.5 and later include cloud helpers to make detecting instances that run on public and private clouds easier.

@example

#### incorrect
node['cloud']['provider'] == 'alibaba'
node['cloud']['provider'] == 'ec2'
node['cloud']['provider'] == 'gce'
node['cloud']['provider'] == 'rackspace'
node['cloud']['provider'] == 'eucalyptus'
node['cloud']['provider'] == 'linode'
node['cloud']['provider'] == 'openstack'
node['cloud']['provider'] == 'azure'
node['cloud']['provider'] == 'digital_ocean'
node['cloud']['provider'] == 'softlayer'

#### correct
alibaba?
ec2?
gce?
rackspace?
eucalyptus?
linode?
openstack?
azure?
digital_ocean?
softlayer?

Constants

MSG
RESTRICT_ON_SEND
VALID_CLOUDS

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/chef/modernize/use_chef_language_cloud_helpers.rb, line 76
def on_send(node)
  provider_comparison?(node) do |cloud_name|
    # skip it if someone was checking for a bogus cloud provider
    next unless VALID_CLOUDS.include?(cloud_name)

    # if they were checking for node['cloud'] and the provider replace it all
    node = node.parent if node.parent.and_type? && node_cloud?(node.left_sibling)

    add_offense(node, message: MSG, severity: :refactor) do |corrector|
      corrector.replace(node, "#{cloud_name}?")
    end
  end
end