Class: RuboCop::Cop::Chef::Modernize::UseChefLanguageCloudHelpers

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetChefVersion
Defined in:
lib/rubocop/cop/chef/modernize/use_chef_language_cloud_helpers.rb

Overview

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

Examples:


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

Constant Summary collapse

MSG =
'Chef Infra Client 15.5 and later include cloud helpers to make detecting instances that run on public and private clouds easier.'
RESTRICT_ON_SEND =
[:==, :[]].freeze
VALID_CLOUDS =
%w(alibaba ec2 gce rackspace eucalyptus linode openstack azure digital_ocean softlayer).freeze

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# 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, severity: :refactor) do |corrector|
      corrector.replace(node, "#{cloud_name}?")
    end
  end
end