diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index f3c4719..bafef52 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -101,7 +101,7 @@ def run # rubocop:disable Metrics/AbcSize running_vms = service.list_active(verbose) host = URI.parse(service.url).host if running_vms.empty? - if option.json + if options.json puts {}.to_json else puts "You have no running VMs on #{host}" @@ -210,6 +210,7 @@ def run # rubocop:disable Metrics/AbcSize c.option '--all', 'Deletes all vms acquired by a token' c.option '--json', 'Outputs list of returned hosts as JSON' c.option '-f', 'Does not prompt user when deleting all vms' + c.option '--json', 'Outputs hosts scheduled for deletion as JSON' c.option '--token STRING', String, 'Token for pooler service' c.option '--url STRING', String, 'URL of pooler service' c.action do |args, options| diff --git a/lib/vmfloaty/abs.rb b/lib/vmfloaty/abs.rb index 9a6358a..8c37072 100644 --- a/lib/vmfloaty/abs.rb +++ b/lib/vmfloaty/abs.rb @@ -229,10 +229,10 @@ def self.retrieve(verbose, os_types, token, url, user, options, _ondemand = nil) retries = 360 - raise AuthError, "HTTP #{res.status}: The token provided could not authenticate to the pooler.\n#{res_body}" if res.status == 401 + validate_queue_status_response(res.status, res.body, "Initial request", verbose) (1..retries).each do |i| - queue_place, res_body = check_queue(conn, saved_job_id, req_obj) + queue_place, res_body = check_queue(conn, saved_job_id, req_obj, verbose) return translated(res_body) if res_body sleep_seconds = 10 if i >= 10 @@ -262,11 +262,12 @@ def self.translated(res_body) vmpooler_formatted_body end - def self.check_queue(conn, job_id, req_obj) + def self.check_queue(conn, job_id, req_obj, verbose) queue_info_res = conn.get "status/queue/info/#{job_id}" queue_info = JSON.parse(queue_info_res.body) res = conn.post 'request', req_obj.to_json + validate_queue_status_response(res.status, res.body, "Check queue request", verbose) unless res.body.empty? res_body = JSON.parse(res.body) @@ -315,4 +316,21 @@ def self.disk(_verbose, _url, _hostname, _token, _disk) def self.revert(_verbose, _url, _hostname, _token, _snapshot_sha) raise NoMethodError, 'revert is not defined for ABS' end + + # Validate the http code returned during a queue status request. + # + # Return a success message that can be displayed if the status code is + # success, otherwise raise an error. + def self.validate_queue_status_response(status_code, body, request_name, verbose) + case status_code + when 200 + "#{request_name} returned success (Code 200)" if verbose + when 202 + "#{request_name} returned accepted, processing (Code 202)" if verbose + when 401 + raise AuthError, "HTTP #{status_code}: The token provided could not authenticate.\n#{body}" + else + raise "HTTP #{status_code}: #{request_name} request to ABS failed!\n#{body}" + end + end end