(#29) Don't system exit in Auth class

This commit cleans up the Auth class so that it no longer system exits
if an error occurs. Instead it will return nil if it could not properly
make the request, and it's on the consumer of the class to handle
error cases.
This commit is contained in:
Brian Cain 2016-09-15 08:47:38 -07:00
parent 59eb5678ed
commit b07139b64c
4 changed files with 50 additions and 25 deletions

View file

@ -56,8 +56,10 @@ class Vmfloaty
end end
pass = password "Enter your password please:", '*' pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass) token = Auth.get_token(verbose, url, user, pass)
puts "\nToken retrieved!" unless token.nil?
puts token puts "\nToken retrieved!"
puts token
end
end end
response = Pooler.retrieve(verbose, os_types, token, url) response = Pooler.retrieve(verbose, os_types, token, url)
@ -90,11 +92,15 @@ class Vmfloaty
if active if active
# list active vms # list active vms
status = Auth.token_status(verbose, url, token) status = Auth.token_status(verbose, url, token)
# print vms unless status.nil?
vms = status[token]['vms'] # print vms
if vms.nil? vms = status[token]['vms']
STDERR.puts "You have no running vms" if vms.nil?
exit 0 STDERR.puts "You have no running vms"
exit 0
end
else
STDERR.puts "Could not retrieve active vms"
end end
running_vms = vms['running'] running_vms = vms['running']
@ -194,6 +200,12 @@ class Vmfloaty
if delete_all if delete_all
# get vms with token # get vms with token
status = Auth.token_status(verbose, url, token) status = Auth.token_status(verbose, url, token)
if status.nil?
STDERR.puts "Could not retrieve status with token"
exit 1
end
# print vms # print vms
vms = status[token]['vms'] vms = status[token]['vms']
if vms.nil? if vms.nil?
@ -333,14 +345,26 @@ class Vmfloaty
when "get" when "get"
pass = password "Enter your password please:", '*' pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass) token = Auth.get_token(verbose, url, user, pass)
puts token unless token.nil?
puts token
else
STDERR.puts 'Could not make a request for a token'
end
when "delete" when "delete"
pass = password "Enter your password please:", '*' pass = password "Enter your password please:", '*'
result = Auth.delete_token(verbose, url, user, pass, token) result = Auth.delete_token(verbose, url, user, pass, token)
puts result unless result.nil?
puts result
else
STDERR.puts 'Could not make a request to delete a token'
end
when "status" when "status"
status = Auth.token_status(verbose, url, token) status = Auth.token_status(verbose, url, token)
puts status unless status.nil?
puts status
else
STDERR.puts 'Could not make a request to get token status'
end
when nil when nil
STDERR.puts "No action provided" STDERR.puts "No action provided"
else else
@ -380,8 +404,12 @@ class Vmfloaty
end end
pass = password "Enter your password please:", '*' pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass) token = Auth.get_token(verbose, url, user, pass)
puts "\nToken retrieved!" unless token.nil?
puts token puts "\nToken retrieved!"
puts token
else
STDERR.puts 'Could not get token...requesting vm without a token anyway...'
end
end end
Ssh.ssh(verbose, host_os, token, url) Ssh.ssh(verbose, host_os, token, url)

View file

@ -12,16 +12,15 @@ class Auth
if res_body["ok"] if res_body["ok"]
return res_body["token"] return res_body["token"]
else else
STDERR.puts "There was a problem with your request:" STDERR.puts "There was a problem with your request:\n#{res_body}"
puts res_body return nil
exit 1
end end
end end
def self.delete_token(verbose, url, user, password, token) def self.delete_token(verbose, url, user, password, token)
if token.nil? if token.nil?
STDERR.puts 'You did not provide a token' STDERR.puts 'You did not provide a token'
exit 1 return nil
end end
conn = Http.get_conn_with_auth(verbose, url, user, password) conn = Http.get_conn_with_auth(verbose, url, user, password)
@ -31,16 +30,15 @@ class Auth
if res_body["ok"] if res_body["ok"]
return res_body return res_body
else else
STDERR.puts "There was a problem with your request:" STDERR.puts "There was a problem with your request:\n#{res_body}"
puts res_body return nil
exit 1
end end
end end
def self.token_status(verbose, url, token) def self.token_status(verbose, url, token)
if token.nil? if token.nil?
STDERR.puts 'You did not provide a token' STDERR.puts 'You did not provide a token'
exit 1 return nil
end end
conn = Http.get_conn(verbose, url) conn = Http.get_conn(verbose, url)
@ -51,9 +49,8 @@ class Auth
if res_body["ok"] if res_body["ok"]
return res_body return res_body
else else
STDERR.puts "There was a problem with your request:" STDERR.puts "There was a problem with your request:\n#{res_body}"
puts res_body return nil
exit 1
end end
end end
end end

View file

@ -1,6 +1,6 @@
class Version class Version
@version = '0.6.0' @version = '0.6.1'
def self.get def self.get
@version @version

View file

@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'vmfloaty' s.name = 'vmfloaty'
s.version = '0.6.0' s.version = '0.6.1'
s.authors = ['Brian Cain'] s.authors = ['Brian Cain']
s.email = ['brian.cain@puppetlabs.com'] s.email = ['brian.cain@puppetlabs.com']
s.license = 'Apache' s.license = 'Apache'