From b07139b64cb8318ac493ca4f587d28a469a43ba9 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 15 Sep 2016 08:47:38 -0700 Subject: [PATCH] (#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. --- lib/vmfloaty.rb | 52 +++++++++++++++++++++++++++++++---------- lib/vmfloaty/auth.rb | 19 +++++++-------- lib/vmfloaty/version.rb | 2 +- vmfloaty.gemspec | 2 +- 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index c46f701..c70bd82 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -56,8 +56,10 @@ class Vmfloaty end pass = password "Enter your password please:", '*' token = Auth.get_token(verbose, url, user, pass) - puts "\nToken retrieved!" - puts token + unless token.nil? + puts "\nToken retrieved!" + puts token + end end response = Pooler.retrieve(verbose, os_types, token, url) @@ -90,11 +92,15 @@ class Vmfloaty if active # list active vms status = Auth.token_status(verbose, url, token) - # print vms - vms = status[token]['vms'] - if vms.nil? - STDERR.puts "You have no running vms" - exit 0 + 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) - puts token + 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) - puts result + 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) - puts status + 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) - puts "\nToken retrieved!" - puts token + 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) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 2e74b9b..9ca9d4e 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -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 diff --git a/lib/vmfloaty/version.rb b/lib/vmfloaty/version.rb index f7e5cb8..b6aaa64 100644 --- a/lib/vmfloaty/version.rb +++ b/lib/vmfloaty/version.rb @@ -1,6 +1,6 @@ class Version - @version = '0.6.0' + @version = '0.6.1' def self.get @version diff --git a/vmfloaty.gemspec b/vmfloaty.gemspec index dd74f9d..25430c3 100644 --- a/vmfloaty.gemspec +++ b/vmfloaty.gemspec @@ -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'