Skip to content

Commit

Permalink
Cookstyle fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Jan 22, 2017
1 parent 1a953d8 commit b177d7f
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 118 deletions.
24 changes: 17 additions & 7 deletions .rubocop.yml
@@ -1,10 +1,4 @@
# rubocop todo:
#
Lint/AmbiguousRegexpLiteral:
Exclude:
- 'test/integration/dynamodb_table_create/rspec/dynamodb_table_create_spec.rb'
- 'test/integration/dynamodb_table_update/rspec/dynamodb_table_update_spec.rb'
- 'test/integration/s3_file/default_spec.rb'
# rubocop todo

Lint/Loop:
Exclude:
Expand All @@ -19,6 +13,22 @@ Lint/UselessAssignment:
Exclude:
- 'libraries/ec2.rb'

Metrics/BlockNesting:
Max: 7

# Configuration parameters: CountKeywordArgs.
Metrics/ParameterLists:
Max: 15

Style/EndOfLine:
Exclude:
- 'libraries/matchers.rb'
- 'metadata.rb'

# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'providers/cloudformation_stack.rb'
- 'providers/cloudwatch.rb'
- 'providers/ebs_volume.rb'
- 'providers/elastic_ip.rb'
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -21,7 +21,7 @@ namespace :style do
FoodCritic::Rake::LintTask.new(:chef) do |t|
t.options = {
fail_tags: ['any'],
progress: true
progress: true,
}
end
rescue LoadError
Expand Down
2 changes: 1 addition & 1 deletion libraries/ec2.rb
Expand Up @@ -42,7 +42,7 @@ def find_snapshot_id(volume_id = '', find_most_recent = false)
response = ec2.describe_snapshots(
filters: [
{ name: 'volume-id', values: [volume_id] },
{ name: 'status', values: ['completed'] }
{ name: 'status', values: ['completed'] },
]
)
snapshots = if find_most_recent
Expand Down
2 changes: 1 addition & 1 deletion providers/cloudformation_stack.rb
Expand Up @@ -15,7 +15,7 @@ def build_cfn_options
# make sure you call this after you save the file
template_body: ::IO.read(@template_path),
parameters: new_resource.parameters,
disable_rollback: new_resource.disable_rollback
disable_rollback: new_resource.disable_rollback,
}
unless new_resource.stack_policy_body.nil?
options[:stack_policy_body] = new_resource.stack_policy_body
Expand Down
100 changes: 50 additions & 50 deletions providers/cloudwatch.rb
Expand Up @@ -12,26 +12,26 @@ def build_cwh_options
alarm_name: new_resource.alarm_name,
period: new_resource.period,
evaluation_periods: new_resource.evaluation_periods,
threshold: new_resource.threshold.to_f ,
threshold: new_resource.threshold.to_f,
comparison_operator: new_resource.comparison_operator,
metric_name: new_resource.metric_name,
namespace: new_resource.namespace
namespace: new_resource.namespace,
}
options[:alarm_description] = new_resource.alarm_description if new_resource.alarm_description
if new_resource.actions_enabled
if new_resource.ok_actions.empty? && new_resource.alarm_actions.empty? && new_resource.insufficient_data_actions.empty?
raise "No actions provided for any of OK/ALARM/INSUFFICIENT"
else
options[:actions_enabled] = true if new_resource.actions_enabled
options[:ok_actions] = new_resource.ok_actions if new_resource.ok_actions.any?
options[:alarm_actions] = new_resource.alarm_actions if new_resource.alarm_actions.any?
options[:insufficient_data_actions] = new_resource.insufficient_data_actions if new_resource.insufficient_data_actions.any?
end
if new_resource.ok_actions.empty? && new_resource.alarm_actions.empty? && new_resource.insufficient_data_actions.empty?
raise 'No actions provided for any of OK/ALARM/INSUFFICIENT'
else
options[:actions_enabled] = true if new_resource.actions_enabled
options[:ok_actions] = new_resource.ok_actions if new_resource.ok_actions.any?
options[:alarm_actions] = new_resource.alarm_actions if new_resource.alarm_actions.any?
options[:insufficient_data_actions] = new_resource.insufficient_data_actions if new_resource.insufficient_data_actions.any?
end
end
options[:statistic] = new_resource.statistic if new_resource.statistic
options[:dimensions] = new_resource.dimensions if new_resource.dimensions
options[:extended_statistic] = new_resource.extended_statistic if new_resource.extended_statistic
options[:unit] = new_resource.unit if new_resource.unit
options[:statistic] = new_resource.statistic if new_resource.statistic
options[:dimensions] = new_resource.dimensions if new_resource.dimensions
options[:extended_statistic] = new_resource.extended_statistic if new_resource.extended_statistic
options[:unit] = new_resource.unit if new_resource.unit
options
end

Expand All @@ -40,38 +40,38 @@ def build_cwh_options
# type - any, alarm_action
# p - boolen option for alarm_action
def cwh_if_changed(type, *p)
options = { alarm_names: [ new_resource.alarm_name ], max_records: 1 }
options = { alarm_names: [new_resource.alarm_name], max_records: 1 }
resp = cwh.describe_alarms(options)
if !resp.metric_alarms.empty?
if type == "any"
if type == 'any'
new_params = build_cwh_options
new_params.each_key { |k|
if resp.metric_alarms[0][k].nil? || resp.metric_alarms[0][k].to_s.empty?
new_params.each_key do |k|
if resp.metric_alarms[0][k].nil? || resp.metric_alarms[0][k].to_s.empty?
return true
elsif new_params[k].is_a?(Array) || resp.metric_alarms[0][k].is_a?(Array)
if new_params[k].length != resp.metric_alarms[0][k].length
return true
elsif new_params[k].is_a?(Array) || resp.metric_alarms[0][k].is_a?(Array)
if new_params[k].length != resp.metric_alarms[0][k].length
return true
else
new_params[k].each_index { |n|
if resp.metric_alarms[0][k][n].nil? || resp.metric_alarms[0][k][n].to_s.empty?
else
new_params[k].each_index do |n|
if resp.metric_alarms[0][k][n].nil? || resp.metric_alarms[0][k][n].to_s.empty?
return true
elsif new_params[k][n].is_a?(Hash) || resp.metric_alarms[0][k][n].is_a?(Hash)
if new_params[k][n].length != resp.metric_alarms[0][k][n].length
return true
elsif new_params[k][n].is_a?(Hash) || resp.metric_alarms[0][k][n].is_a?(Hash)
if new_params[k][n].length != resp.metric_alarms[0][k][n].length
return true
else
new_params[k][n].each_key { |m| return true unless resp.metric_alarms[0][k][n][m].nil? || resp.metric_alarms[0][k][n][m].to_s.empty? || new_params[k][n][m] == resp.metric_alarms[0][k][n][m]}
end
else
return true unless resp.metric_alarms[0][k][n].nil? || resp.metric_alarms[0][k][n].to_s.empty? || new_params[k][n] == resp.metric_alarms[0][k][n]
new_params[k][n].each_key { |m| return true unless resp.metric_alarms[0][k][n][m].nil? || resp.metric_alarms[0][k][n][m].to_s.empty? || new_params[k][n][m] == resp.metric_alarms[0][k][n][m] }
end
}
else
return true unless resp.metric_alarms[0][k][n].nil? || resp.metric_alarms[0][k][n].to_s.empty? || new_params[k][n] == resp.metric_alarms[0][k][n]
end
end
else
return true unless new_params[k] == resp.metric_alarms[0][k]
end
}
elsif type == "alarm_action"
return true unless resp.metric_alarms[0].actions_enabled.to_s == p.join()
else
return true unless new_params[k] == resp.metric_alarms[0][k]
end
end
elsif type == 'alarm_action'
return true unless resp.metric_alarms[0].actions_enabled.to_s == p.join
end
false
else
Expand All @@ -81,13 +81,13 @@ def cwh_if_changed(type, *p)

# Create action will fire put_metric_alarm of cloudwatch API, will update the alarm if found changes in parameters.
action :create do
if cwh_if_changed("any")
converge_by("create/update metric #{new_resource.alarm_name}") do
Chef::Log.debug("create/update metric #{new_resource.alarm_name}")
options = build_cwh_options
cwh.put_metric_alarm(options)
new_resource.updated_by_last_action(true)
end
if cwh_if_changed('any')
converge_by("create/update metric #{new_resource.alarm_name}") do
Chef::Log.debug("create/update metric #{new_resource.alarm_name}")
options = build_cwh_options
cwh.put_metric_alarm(options)
new_resource.updated_by_last_action(true)
end
else
Chef::Log.debug("No action required for metric #{new_resource.alarm_name}")
end
Expand All @@ -97,18 +97,18 @@ def cwh_if_changed(type, *p)
action :delete do
converge_by("delete metric #{new_resource.alarm_name}") do
Chef::Log.debug("delete metric #{new_resource.alarm_name}")
options = { alarm_names: [ new_resource.alarm_name ] }
options = { alarm_names: [new_resource.alarm_name] }
cwh.delete_alarms(options)
new_resource.updated_by_last_action(true)
end
end

# Disable action will fire disable_alarm_actions of cloudwatch API. if not disabled already.
action :disable_action do
if cwh_if_changed("alarm_action","false")
if cwh_if_changed('alarm_action', 'false')
converge_by("disable alarm action metric #{new_resource.alarm_name}") do
Chef::Log.debug("disable alarm action metric #{new_resource.alarm_name}")
options = { alarm_names: [ new_resource.alarm_name ] }
options = { alarm_names: [new_resource.alarm_name] }
cwh.disable_alarm_actions(options)
new_resource.updated_by_last_action(true)
end
Expand All @@ -119,14 +119,14 @@ def cwh_if_changed(type, *p)

# Enable action will fire disable_alarm_actions of cloudwatch API. if not enabled already.
action :enable_action do
if cwh_if_changed("alarm_action","true")
if cwh_if_changed('alarm_action', 'true')
converge_by("enable alarm action metric #{new_resource.alarm_name}") do
Chef::Log.debug("enable alarm action metric #{new_resource.alarm_name}")
options = { alarm_names: [ new_resource.alarm_name ] }
options = { alarm_names: [new_resource.alarm_name] }
cwh.enable_alarm_actions(options)
new_resource.updated_by_last_action(true)
end
else
Chef::Log.debug("No action required for metric #{new_resource.alarm_name}")
end
end
end
2 changes: 1 addition & 1 deletion providers/dynamodb_table.rb
Expand Up @@ -133,7 +133,7 @@ def load_gsi_updates(api_indexes, res_indexes)
updates.push(
update: {
index_name: gsi.index_name,
provisioned_throughput: res_index[:provisioned_throughput]
provisioned_throughput: res_index[:provisioned_throughput],
}
) if throughput_changed?(
api_index.provisioned_throughput, res_index[:provisioned_throughput]
Expand Down
2 changes: 1 addition & 1 deletion providers/ebs_volume.rb
Expand Up @@ -154,7 +154,7 @@ def currently_attached_volume(instance_id, device)
ec2.describe_volumes(
filters: [
{ name: 'attachment.device', values: [device] },
{ name: 'attachment.instance-id', values: [instance_id] }
{ name: 'attachment.instance-id', values: [instance_id] },
]
).volumes[0]
end
Expand Down
2 changes: 1 addition & 1 deletion providers/elastic_ip.rb
Expand Up @@ -10,7 +10,7 @@ def whyrun_supported?
ip = new_resource.ip || node['aws']['elastic_ip'][new_resource.name]['ip']
addr = address(ip)

if addr.nil? # rubocop: disable Style/GuardClause
if addr.nil?
raise "Elastic IP #{ip} does not exist"
elsif addr[:instance_id] == instance_id
Chef::Log.debug("Elastic IP #{ip} is already attached to the instance")
Expand Down
14 changes: 7 additions & 7 deletions resources/cloudwatch.rb
Expand Up @@ -9,19 +9,19 @@
attribute :insufficient_data_actions, kind_of: Array, default: []
attribute :metric_name, kind_of: String
attribute :namespace, kind_of: String
attribute :statistic, equal_to: [ 'SampleCount', 'Average', 'Sum', 'Minimum', 'Maximum' ]
attribute :statistic, equal_to: %w(SampleCount Average Sum Minimum Maximum)
attribute :extended_statistic, kind_of: String
attribute :dimensions, Array, default: []
attribute :period , kind_of: Integer
attribute :unit , kind_of: String
attribute :evaluation_periods , kind_of: Integer
attribute :threshold , kind_of: [Float, Integer]
attribute :comparison_operator , equal_to: [ 'GreaterThanOrEqualToThreshold', 'GreaterThanThreshold', 'LessThanThreshold', 'LessThanOrEqualToThreshold' ]
attribute :period, kind_of: Integer
attribute :unit, kind_of: String
attribute :evaluation_periods, kind_of: Integer
attribute :threshold, kind_of: [Float, Integer]
attribute :comparison_operator, equal_to: %w(GreaterThanOrEqualToThreshold GreaterThanThreshold LessThanThreshold LessThanOrEqualToThreshold)

# aws credential/connection attributes
attribute :region, kind_of: String
attribute :aws_access_key, kind_of: String
attribute :aws_secret_access_key, kind_of: String
attribute :aws_session_token, kind_of: String
attribute :aws_assume_role_arn, kind_of: String
attribute :aws_role_session_name, kind_of: String
attribute :aws_role_session_name, kind_of: String
4 changes: 1 addition & 3 deletions test/fixtures/cookbooks/aws_test/recipes/cloudwatch_alarm.rb
Expand Up @@ -14,8 +14,6 @@
action :create
end



aws_cloudwatch 'kitchen_test_alarm' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
Expand All @@ -32,4 +30,4 @@
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
action :delete
end
end
30 changes: 15 additions & 15 deletions test/fixtures/cookbooks/aws_test/recipes/dynamodb_table_create.rb
Expand Up @@ -6,52 +6,52 @@
{ attribute_name: 'Id', attribute_type: 'N' },
{ attribute_name: 'Foo', attribute_type: 'N' },
{ attribute_name: 'Bar', attribute_type: 'N' },
{ attribute_name: 'Baz', attribute_type: 'S' }
{ attribute_name: 'Baz', attribute_type: 'S' },
]
key_schema [
{ attribute_name: 'Id', key_type: 'HASH' },
{ attribute_name: 'Foo', key_type: 'RANGE' }
{ attribute_name: 'Foo', key_type: 'RANGE' },
]
local_secondary_indexes [
{
index_name: 'BarIndex',
key_schema: [
{
attribute_name: 'Id',
key_type: 'HASH'
key_type: 'HASH',
},
{
attribute_name: 'Bar',
key_type: 'RANGE'
}
key_type: 'RANGE',
},
],
projection: {
projection_type: 'ALL'
}
}
projection_type: 'ALL',
},
},
]
global_secondary_indexes [
{
index_name: 'BazIndex',
key_schema: [{
attribute_name: 'Baz',
key_type: 'HASH'
key_type: 'HASH',
}],
projection: {
projection_type: 'ALL'
projection_type: 'ALL',
},
provisioned_throughput: {
read_capacity_units: 2,
write_capacity_units: 2
}
}
write_capacity_units: 2,
},
},
]
provisioned_throughput ({
read_capacity_units: 2,
write_capacity_units: 2
write_capacity_units: 2,
})
stream_specification ({
stream_enabled: true,
stream_view_type: 'KEYS_ONLY'
stream_view_type: 'KEYS_ONLY',
})
end

0 comments on commit b177d7f

Please sign in to comment.