mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-25 21:28:40 -05:00
(#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:
parent
59eb5678ed
commit
b07139b64c
4 changed files with 50 additions and 25 deletions
|
|
@ -56,9 +56,11 @@ class Vmfloaty
|
|||
end
|
||||
pass = password "Enter your password please:", '*'
|
||||
token = Auth.get_token(verbose, url, user, pass)
|
||||
unless token.nil?
|
||||
puts "\nToken retrieved!"
|
||||
puts token
|
||||
end
|
||||
end
|
||||
|
||||
response = Pooler.retrieve(verbose, os_types, token, url)
|
||||
puts Utils.format_hosts(response)
|
||||
|
|
@ -90,12 +92,16 @@ class Vmfloaty
|
|||
if active
|
||||
# list active vms
|
||||
status = Auth.token_status(verbose, url, token)
|
||||
unless status.nil?
|
||||
# print vms
|
||||
vms = status[token]['vms']
|
||||
if vms.nil?
|
||||
STDERR.puts "You have no running vms"
|
||||
exit 0
|
||||
end
|
||||
else
|
||||
STDERR.puts "Could not retrieve active vms"
|
||||
end
|
||||
|
||||
running_vms = vms['running']
|
||||
|
||||
|
|
@ -194,6 +200,12 @@ class Vmfloaty
|
|||
if delete_all
|
||||
# get vms with token
|
||||
status = Auth.token_status(verbose, url, token)
|
||||
|
||||
if status.nil?
|
||||
STDERR.puts "Could not retrieve status with token"
|
||||
exit 1
|
||||
end
|
||||
|
||||
# print vms
|
||||
vms = status[token]['vms']
|
||||
if vms.nil?
|
||||
|
|
@ -333,14 +345,26 @@ class Vmfloaty
|
|||
when "get"
|
||||
pass = password "Enter your password please:", '*'
|
||||
token = Auth.get_token(verbose, url, user, pass)
|
||||
unless token.nil?
|
||||
puts token
|
||||
else
|
||||
STDERR.puts 'Could not make a request for a token'
|
||||
end
|
||||
when "delete"
|
||||
pass = password "Enter your password please:", '*'
|
||||
result = Auth.delete_token(verbose, url, user, pass, token)
|
||||
unless result.nil?
|
||||
puts result
|
||||
else
|
||||
STDERR.puts 'Could not make a request to delete a token'
|
||||
end
|
||||
when "status"
|
||||
status = Auth.token_status(verbose, url, token)
|
||||
unless status.nil?
|
||||
puts status
|
||||
else
|
||||
STDERR.puts 'Could not make a request to get token status'
|
||||
end
|
||||
when nil
|
||||
STDERR.puts "No action provided"
|
||||
else
|
||||
|
|
@ -380,8 +404,12 @@ class Vmfloaty
|
|||
end
|
||||
pass = password "Enter your password please:", '*'
|
||||
token = Auth.get_token(verbose, url, user, pass)
|
||||
unless token.nil?
|
||||
puts "\nToken retrieved!"
|
||||
puts token
|
||||
else
|
||||
STDERR.puts 'Could not get token...requesting vm without a token anyway...'
|
||||
end
|
||||
end
|
||||
|
||||
Ssh.ssh(verbose, host_os, token, url)
|
||||
|
|
|
|||
|
|
@ -12,16 +12,15 @@ class Auth
|
|||
if res_body["ok"]
|
||||
return res_body["token"]
|
||||
else
|
||||
STDERR.puts "There was a problem with your request:"
|
||||
puts res_body
|
||||
exit 1
|
||||
STDERR.puts "There was a problem with your request:\n#{res_body}"
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.delete_token(verbose, url, user, password, token)
|
||||
if token.nil?
|
||||
STDERR.puts 'You did not provide a token'
|
||||
exit 1
|
||||
return nil
|
||||
end
|
||||
|
||||
conn = Http.get_conn_with_auth(verbose, url, user, password)
|
||||
|
|
@ -31,16 +30,15 @@ class Auth
|
|||
if res_body["ok"]
|
||||
return res_body
|
||||
else
|
||||
STDERR.puts "There was a problem with your request:"
|
||||
puts res_body
|
||||
exit 1
|
||||
STDERR.puts "There was a problem with your request:\n#{res_body}"
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.token_status(verbose, url, token)
|
||||
if token.nil?
|
||||
STDERR.puts 'You did not provide a token'
|
||||
exit 1
|
||||
return nil
|
||||
end
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
|
@ -51,9 +49,8 @@ class Auth
|
|||
if res_body["ok"]
|
||||
return res_body
|
||||
else
|
||||
STDERR.puts "There was a problem with your request:"
|
||||
puts res_body
|
||||
exit 1
|
||||
STDERR.puts "There was a problem with your request:\n#{res_body}"
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
class Version
|
||||
@version = '0.6.0'
|
||||
@version = '0.6.1'
|
||||
|
||||
def self.get
|
||||
@version
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Gem::Specification.new do |s|
|
||||
s.name = 'vmfloaty'
|
||||
s.version = '0.6.0'
|
||||
s.version = '0.6.1'
|
||||
s.authors = ['Brian Cain']
|
||||
s.email = ['brian.cain@puppetlabs.com']
|
||||
s.license = 'Apache'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue