From a0ba604006b6dd4294deef4c40b7335db6e41602 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 13 Nov 2015 11:52:01 -0800 Subject: [PATCH] (#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. --- lib/vmfloaty.rb | 33 ++++++++++++++++++++++++++++++++- lib/vmfloaty/auth.rb | 2 +- spec/vmfloaty/auth_spec.rb | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 3441c64..835b2cf 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -140,6 +140,7 @@ class Vmfloaty c.description = '' 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 '--all', 'Deletes all vms acquired by a token' c.option '--token STRING', String, 'Token for vmpooler' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| @@ -147,6 +148,36 @@ class Vmfloaty hostnames = args[0] token = options.token || config['token'] 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? STDERR.puts "You did not provide any hosts to delete" @@ -254,7 +285,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' Auth.delete_token(verbose, url, user, pass, token) when "status" - Auth.token_status(verbose, url, token) + puts Auth.token_status(verbose, url, token) when nil STDERR.puts "No action provided" else diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 97f83bb..7c823e5 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -49,7 +49,7 @@ class Auth res_body = JSON.parse(response.body) if res_body["ok"] - puts res_body + res_body else STDERR.puts "There was a problem with your request:" puts res_body diff --git a/spec/vmfloaty/auth_spec.rb b/spec/vmfloaty/auth_spec.rb index 955b855..b30a3ad 100644 --- a/spec/vmfloaty/auth_spec.rb +++ b/spec/vmfloaty/auth_spec.rb @@ -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'}). 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