(#11) Provide a way to delete all vms acquired by token

This commit adds a way for a user to delete all vms that were obtained
by a specific token. It will ask the user if they are sure before
deleting everything.
This commit is contained in:
Brian Cain 2015-11-13 11:52:01 -08:00
parent bc4cff5363
commit a0ba604006
3 changed files with 34 additions and 3 deletions

View file

@ -140,6 +140,7 @@ class Vmfloaty
c.description = '' c.description = ''
c.example 'Schedules the deletion of a host or hosts', 'floaty delete myhost1,myhost2 --url http://vmpooler.example.com' c.example 'Schedules the deletion of a host or hosts', 'floaty delete myhost1,myhost2 --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output' c.option '--verbose', 'Enables verbose output'
c.option '--all', 'Deletes all vms acquired by a token'
c.option '--token STRING', String, 'Token for vmpooler' c.option '--token STRING', String, 'Token for vmpooler'
c.option '--url STRING', String, 'URL of vmpooler' c.option '--url STRING', String, 'URL of vmpooler'
c.action do |args, options| c.action do |args, options|
@ -147,6 +148,36 @@ class Vmfloaty
hostnames = args[0] hostnames = args[0]
token = options.token || config['token'] token = options.token || config['token']
url = options.url ||= config['url'] url = options.url ||= config['url']
delete_all = options.all
if delete_all
# get vms with token
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
end
running_vms = vms['running']
if ! running_vms.nil?
puts "Running VMs:"
running_vms.each do |vm|
puts "- #{vm}"
end
# query y/n
puts ""
ans = agree("Delete all VMs associated with token #{token}? [y/N]")
if ans
# delete vms
Pooler.delete(verbose, url, running_vms, token)
end
end
exit 0
end
if hostnames.nil? if hostnames.nil?
STDERR.puts "You did not provide any hosts to delete" STDERR.puts "You did not provide any hosts to delete"
@ -254,7 +285,7 @@ class Vmfloaty
pass = password "Enter your password please:", '*' pass = password "Enter your password please:", '*'
Auth.delete_token(verbose, url, user, pass, token) Auth.delete_token(verbose, url, user, pass, token)
when "status" when "status"
Auth.token_status(verbose, url, token) puts Auth.token_status(verbose, url, token)
when nil when nil
STDERR.puts "No action provided" STDERR.puts "No action provided"
else else

View file

@ -49,7 +49,7 @@ class Auth
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
if res_body["ok"] if res_body["ok"]
puts res_body res_body
else else
STDERR.puts "There was a problem with your request:" STDERR.puts "There was a problem with your request:"
puts res_body puts res_body

View file

@ -48,7 +48,7 @@ describe Pooler do
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.1'}). with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.1'}).
to_return(:status => 200, :body => @token_status_response, :headers => {}) to_return(:status => 200, :body => @token_status_response, :headers => {})
expect(Auth.token_status(false, @vmpooler_url, @token)).to eq @get_token_response expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response)
end end
end end
end end