(#33) Handle more exceptions in vmfloaty command class

This commit adds more handling to the vmfloaty command class that deals
with exceptions thrown from the Pooler class.
This commit is contained in:
Brian Cain 2016-09-24 13:29:43 -07:00
parent 478761023d
commit c62c1c7083
3 changed files with 35 additions and 10 deletions

View file

@ -45,7 +45,15 @@ class Vmfloaty
unless os_types.empty?
if no_token
begin
response = Pooler.retrieve(verbose, os_types, nil, url)
rescue MissingParamError
STDERR.puts e
STDERR.puts "See `floaty get --help` for more information on how to get VMs."
rescue AuthError => e
STDERR.puts e
exit 1
end
puts Utils.format_hosts(response)
exit 0
else
@ -58,7 +66,7 @@ class Vmfloaty
pass = password "Enter your password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue => e
rescue AuthError => e
STDERR.puts e
exit 1
end
@ -67,7 +75,15 @@ class Vmfloaty
puts token
end
begin
response = Pooler.retrieve(verbose, os_types, token, url)
rescue MissingParamError
STDERR.puts e
STDERR.puts "See `floaty get --help` for more information on how to get VMs."
rescue AuthError => e
STDERR.puts e
exit 1
end
puts Utils.format_hosts(response)
exit 0
end
@ -98,7 +114,7 @@ class Vmfloaty
# list active vms
begin
status = Auth.token_status(verbose, url, token)
rescue => e
rescue AuthError => e
STDERR.puts e
exit 1
end
@ -208,7 +224,7 @@ class Vmfloaty
# get vms with token
begin
status = Auth.token_status(verbose, url, token)
rescue => e
rescue AuthError => e
STDERR.puts e
exit 1
end
@ -357,7 +373,7 @@ class Vmfloaty
pass = password "Enter your password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue => e
rescue AuthError => e
STDERR.puts e
exit 1
end
@ -367,7 +383,7 @@ class Vmfloaty
pass = password "Enter your password please:", '*'
begin
result = Auth.delete_token(verbose, url, user, pass, token)
rescue => e
rescue AuthError => e
STDERR.puts e
exit 1
end
@ -376,7 +392,7 @@ class Vmfloaty
when "status"
begin
status = Auth.token_status(verbose, url, token)
rescue => e
rescue AuthError => e
STDERR.puts e
exit 1
end
@ -423,7 +439,7 @@ class Vmfloaty
pass = password "Enter your password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue => e
rescue AuthError => e
STDERR.puts e
STDERR.puts 'Could not get token...requesting vm without a token anyway...'
else

View file

@ -9,3 +9,9 @@ class TokenError < StandardError
super
end
end
class MissingParamError < StandardError
def initialize(msg="Argument provided to function is missing")
super
end
end

View file

@ -20,6 +20,9 @@ class Pooler
end
def self.retrieve(verbose, os_type, token, url)
# NOTE:
# Developers can use `Utils.generate_os_hash` to
# generate the os_type param.
conn = Http.get_conn(verbose, url)
if token
conn.headers['X-AUTH-TOKEN'] = token
@ -35,7 +38,7 @@ class Pooler
os_string = os_string.chomp("+")
if os_string.size == 0
raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
raise MissingParamError, "No operating systems provided to obtain."
end
response = conn.post "vm/#{os_string}"