mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
(#33) Handle Auth class errors
This commit updates the Auth class to properly raise an error when something goes wrong. It also updates the vmpooler command class to handle when those errors get raised.
This commit is contained in:
parent
05e9d5a0cc
commit
8da1deaf6b
4 changed files with 85 additions and 45 deletions
|
|
@ -55,12 +55,16 @@ class Vmfloaty
|
||||||
raise "You did not provide a user to authenticate to vmpooler with"
|
raise "You did not provide a user to authenticate to vmpooler with"
|
||||||
end
|
end
|
||||||
pass = password "Enter your password please:", '*'
|
pass = password "Enter your password please:", '*'
|
||||||
|
begin
|
||||||
token = Auth.get_token(verbose, url, user, pass)
|
token = Auth.get_token(verbose, url, user, pass)
|
||||||
unless token.nil?
|
rescue => e
|
||||||
|
STDERR.puts e
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
puts "\nToken retrieved!"
|
puts "\nToken retrieved!"
|
||||||
puts token
|
puts token
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
response = Pooler.retrieve(verbose, os_types, token, url)
|
response = Pooler.retrieve(verbose, os_types, token, url)
|
||||||
puts Utils.format_hosts(response)
|
puts Utils.format_hosts(response)
|
||||||
|
|
@ -91,17 +95,19 @@ class Vmfloaty
|
||||||
|
|
||||||
if active
|
if active
|
||||||
# list active vms
|
# list active vms
|
||||||
|
begin
|
||||||
status = Auth.token_status(verbose, url, token)
|
status = Auth.token_status(verbose, url, token)
|
||||||
unless status.nil?
|
rescue => e
|
||||||
|
STDERR.puts e
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
# print vms
|
# print vms
|
||||||
vms = status[token]['vms']
|
vms = status[token]['vms']
|
||||||
if vms.nil?
|
if vms.nil?
|
||||||
STDERR.puts "You have no running vms"
|
STDERR.puts "You have no running vms"
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
else
|
|
||||||
STDERR.puts "Could not retrieve active vms"
|
|
||||||
end
|
|
||||||
|
|
||||||
running_vms = vms['running']
|
running_vms = vms['running']
|
||||||
|
|
||||||
|
|
@ -199,10 +205,10 @@ class Vmfloaty
|
||||||
|
|
||||||
if delete_all
|
if delete_all
|
||||||
# get vms with token
|
# get vms with token
|
||||||
|
begin
|
||||||
status = Auth.token_status(verbose, url, token)
|
status = Auth.token_status(verbose, url, token)
|
||||||
|
rescue => e
|
||||||
if status.nil?
|
STDERR.puts e
|
||||||
STDERR.puts "Could not retrieve status with token"
|
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -348,27 +354,33 @@ class Vmfloaty
|
||||||
case action
|
case action
|
||||||
when "get"
|
when "get"
|
||||||
pass = password "Enter your password please:", '*'
|
pass = password "Enter your password please:", '*'
|
||||||
|
begin
|
||||||
token = Auth.get_token(verbose, url, user, pass)
|
token = Auth.get_token(verbose, url, user, pass)
|
||||||
unless token.nil?
|
rescue => e
|
||||||
puts token
|
STDERR.puts e
|
||||||
else
|
exit 1
|
||||||
STDERR.puts 'Could not make a request for a token'
|
|
||||||
end
|
end
|
||||||
|
puts token
|
||||||
|
exit 0
|
||||||
when "delete"
|
when "delete"
|
||||||
pass = password "Enter your password please:", '*'
|
pass = password "Enter your password please:", '*'
|
||||||
|
begin
|
||||||
result = Auth.delete_token(verbose, url, user, pass, token)
|
result = Auth.delete_token(verbose, url, user, pass, token)
|
||||||
unless result.nil?
|
rescue => e
|
||||||
|
STDERR.puts e
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
puts result
|
puts result
|
||||||
else
|
exit 1
|
||||||
STDERR.puts 'Could not make a request to delete a token'
|
|
||||||
end
|
|
||||||
when "status"
|
when "status"
|
||||||
|
begin
|
||||||
status = Auth.token_status(verbose, url, token)
|
status = Auth.token_status(verbose, url, token)
|
||||||
unless status.nil?
|
rescue => e
|
||||||
puts status
|
STDERR.puts e
|
||||||
else
|
exit 1
|
||||||
STDERR.puts 'Could not make a request to get token status'
|
|
||||||
end
|
end
|
||||||
|
puts status
|
||||||
|
exit 0
|
||||||
when nil
|
when nil
|
||||||
STDERR.puts "No action provided"
|
STDERR.puts "No action provided"
|
||||||
else
|
else
|
||||||
|
|
@ -407,12 +419,14 @@ class Vmfloaty
|
||||||
raise "You did not provide a user to authenticate to vmpooler with"
|
raise "You did not provide a user to authenticate to vmpooler with"
|
||||||
end
|
end
|
||||||
pass = password "Enter your password please:", '*'
|
pass = password "Enter your password please:", '*'
|
||||||
|
begin
|
||||||
token = Auth.get_token(verbose, url, user, pass)
|
token = Auth.get_token(verbose, url, user, pass)
|
||||||
unless token.nil?
|
rescue => e
|
||||||
|
STDERR.puts e
|
||||||
|
STDERR.puts 'Could not get token...requesting vm without a token anyway...'
|
||||||
|
else
|
||||||
puts "\nToken retrieved!"
|
puts "\nToken retrieved!"
|
||||||
puts token
|
puts token
|
||||||
else
|
|
||||||
STDERR.puts 'Could not get token...requesting vm without a token anyway...'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
require 'faraday'
|
require 'faraday'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'vmfloaty/http'
|
require 'vmfloaty/http'
|
||||||
|
require 'vmfloaty/errors'
|
||||||
|
|
||||||
class Auth
|
class Auth
|
||||||
def self.get_token(verbose, url, user, password)
|
def self.get_token(verbose, url, user, password)
|
||||||
|
|
@ -12,15 +13,13 @@ 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:\n#{res_body}"
|
raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
|
||||||
return nil
|
|
||||||
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'
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
conn = Http.get_conn_with_auth(verbose, url, user, password)
|
conn = Http.get_conn_with_auth(verbose, url, user, password)
|
||||||
|
|
@ -30,15 +29,13 @@ 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:\n#{res_body}"
|
raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
|
||||||
return nil
|
|
||||||
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'
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
conn = Http.get_conn(verbose, url)
|
conn = Http.get_conn(verbose, url)
|
||||||
|
|
@ -49,8 +46,7 @@ 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:\n#{res_body}"
|
raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,9 @@ class AuthError < StandardError
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TokenError < StandardError
|
||||||
|
def initialize(msg="Could not do operation with token provided")
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,14 @@ describe Pooler do
|
||||||
token = Auth.get_token(false, @vmpooler_url, "first.last", "password")
|
token = Auth.get_token(false, @vmpooler_url, "first.last", "password")
|
||||||
expect(token).to eq @token
|
expect(token).to eq @token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "raises a token error if something goes wrong" do
|
||||||
|
stub_request(:post, "https://first.last:password@vmpooler.example.com/token").
|
||||||
|
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2'}).
|
||||||
|
to_return(:status => 500, :body => "{\"ok\":false}", :headers => {})
|
||||||
|
|
||||||
|
expect{ Auth.get_token(false, @vmpooler_url, "first.last", "password") }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#delete_token" do
|
describe "#delete_token" do
|
||||||
|
|
@ -35,6 +43,14 @@ describe Pooler do
|
||||||
|
|
||||||
expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq JSON.parse(@delete_token_response)
|
expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq JSON.parse(@delete_token_response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "raises a token error if something goes wrong" do
|
||||||
|
stub_request(:delete, "https://first.last:password@vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y").
|
||||||
|
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
|
||||||
|
to_return(:status => 500, :body => "{\"ok\":false}", :headers => {})
|
||||||
|
|
||||||
|
expect{ Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token) }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#token_status" do
|
describe "#token_status" do
|
||||||
|
|
@ -50,5 +66,13 @@ describe Pooler do
|
||||||
|
|
||||||
expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response)
|
expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "raises a token error if something goes wrong" do
|
||||||
|
stub_request(:get, "#{@vmpooler_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y").
|
||||||
|
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
|
||||||
|
to_return(:status => 500, :body => "{\"ok\":false}", :headers => {})
|
||||||
|
|
||||||
|
expect{ Auth.token_status(false, @vmpooler_url, @token) }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue