(rubocop) Fix Style/IfUnlessModifier

This commit is contained in:
Tim Sharpe 2019-02-03 15:40:17 +11:00
parent 426e9ce96b
commit 6e1f626d7b
4 changed files with 19 additions and 57 deletions

View file

@ -300,9 +300,7 @@ class Vmfloaty
hostname = args[0]
snapshot_sha = args[1] || options.snapshot
if args[1] && options.snapshot
STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}"
end
STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}" if args[1] && options.snapshot
begin
revert_req = service.revert(verbose, hostname, snapshot_sha)
@ -419,9 +417,7 @@ class Vmfloaty
host_os = args.first
if args.length > 1
STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..."
end
STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1
service.ssh(verbose, host_os, use_token)
exit 0

View file

@ -27,9 +27,7 @@ class NonstandardPooler
conn.headers['X-AUTH-TOKEN'] = token if token
os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
if os_string.empty?
raise MissingParamError, 'No operating systems provided to obtain.'
end
raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
response = conn.post "host/#{os_string}"
@ -45,14 +43,10 @@ class NonstandardPooler
end
def self.modify(verbose, url, hostname, token, modify_hash)
if token.nil?
raise TokenError, 'Token provided was nil; Request cannot be made to modify VM'
end
raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' if token.nil?
modify_hash.each do |key, _value|
unless %i[reason reserved_for_reason].include? key
raise ModifyError, "Configured service type does not support modification of #{key}"
end
raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason reserved_for_reason].include? key
end
if modify_hash[:reason]
@ -84,9 +78,7 @@ class NonstandardPooler
end
def self.delete(verbose, url, hosts, token)
if token.nil?
raise TokenError, 'Token provided was nil; Request cannot be made to delete VM'
end
raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' if token.nil?
conn = Http.get_conn(verbose, url)

View file

@ -24,9 +24,7 @@ class Pooler
def self.list_active(verbose, url, token)
status = Auth.token_status(verbose, url, token)
vms = []
if status[token] && status[token]['vms']
vms = status[token]['vms']['running']
end
vms = status[token]['vms']['running'] if status[token] && status[token]['vms']
vms
end
@ -38,9 +36,7 @@ class Pooler
conn.headers['X-AUTH-TOKEN'] = token if token
os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
if os_string.empty?
raise MissingParamError, 'No operating systems provided to obtain.'
end
raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
response = conn.post "vm/#{os_string}"
@ -56,14 +52,10 @@ class Pooler
end
def self.modify(verbose, url, hostname, token, modify_hash)
if token.nil?
raise TokenError, 'Token provided was nil. Request cannot be made to modify vm'
end
raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil?
modify_hash.keys.each do |key|
unless %i[tags lifetime disk].include? key
raise ModifyError, "Configured service type does not support modification of #{key}."
end
raise ModifyError, "Configured service type does not support modification of #{key}." unless %i[tags lifetime disk].include? key
end
conn = Http.get_conn(verbose, url)
@ -84,9 +76,7 @@ class Pooler
end
def self.disk(verbose, url, hostname, token, disk)
if token.nil?
raise TokenError, 'Token provided was nil. Request cannot be made to modify vm'
end
raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil?
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token
@ -98,9 +88,7 @@ class Pooler
end
def self.delete(verbose, url, hosts, token)
if token.nil?
raise TokenError, 'Token provided was nil. Request cannot be made to delete vm'
end
raise TokenError, 'Token provided was nil. Request cannot be made to delete vm' if token.nil?
conn = Http.get_conn(verbose, url)
@ -143,9 +131,7 @@ class Pooler
end
def self.snapshot(verbose, url, hostname, token)
if token.nil?
raise TokenError, 'Token provided was nil. Request cannot be made to snapshot vm'
end
raise TokenError, 'Token provided was nil. Request cannot be made to snapshot vm' if token.nil?
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token
@ -156,16 +142,12 @@ class Pooler
end
def self.revert(verbose, url, hostname, token, snapshot_sha)
if token.nil?
raise TokenError, 'Token provided was nil. Request cannot be made to revert vm'
end
raise TokenError, 'Token provided was nil. Request cannot be made to revert vm' if token.nil?
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token
if snapshot_sha.nil?
raise "Snapshot SHA provided was nil, could not revert #{hostname}"
end
raise "Snapshot SHA provided was nil, could not revert #{hostname}" if snapshot_sha.nil?
response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}"
res_body = JSON.parse(response.body)

View file

@ -30,9 +30,7 @@ class Utils
# }
# }
unless response_body.delete('ok')
raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}"
end
raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" unless response_body.delete('ok')
# vmpooler reports the domain separately from the hostname
domain = response_body.delete('domain')
@ -81,18 +79,14 @@ class Utils
case service.type
when 'Pooler'
tag_pairs = []
unless host_data['tags'].nil?
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" }
end
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil?
duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
metadata = [host_data['template'], duration, *tag_pairs]
puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})"
when 'NonstandardPooler'
line = "- #{host_data['fqdn']} (#{host_data['os_triple']}"
line += ", #{host_data['hours_left_on_reservation']}h remaining"
unless host_data['reserved_for_reason'].empty?
line += ", reason: #{host_data['reserved_for_reason']}"
end
line += ", reason: #{host_data['reserved_for_reason']}" unless host_data['reserved_for_reason'].empty?
line += ')'
puts line
else
@ -185,9 +179,7 @@ class Utils
service_config.merge! values
else
# If the user provided a service name at the command line, use that service if posible, or fail
unless config['services'][options.service]
raise ArgumentError, "Could not find a configured service named '#{options.service}' in ~/.vmfloaty.yml"
end
raise ArgumentError, "Could not find a configured service named '#{options.service}' in ~/.vmfloaty.yml" unless config['services'][options.service]
# If the service is configured but some values are missing, use the top-level defaults to fill them in
service_config.merge! config['services'][options.service]