diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 5056d4f..9bbe782 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -35,6 +35,7 @@ class Vmfloaty c.option '--token STRING', String, 'Token for pooler service' c.option '--notoken', 'Makes a request without a token' c.option '--force', 'Forces vmfloaty to get requested vms' + c.option '--json', 'Prints retrieved vms in JSON format' c.action do |args, options| verbose = options.verbose || config['verbose'] service = Service.new(options, config) @@ -62,7 +63,12 @@ class Vmfloaty end response = service.retrieve(verbose, os_types, use_token) - puts Utils.format_hosts(response) + hosts = Utils.standardize_hostnames(response) + if options.json + puts JSON.pretty_generate(hosts) + else + puts Utils.format_host_output(hosts) + end end end diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index fe55511..b8e1a17 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -4,7 +4,7 @@ require 'vmfloaty/nonstandard_pooler' class Utils # TODO: Takes the json response body from an HTTP GET # request and "pretty prints" it - def self.format_hosts(response_body) + def self.standardize_hostnames(response_body) # vmpooler response body example when `floaty get` arguments are `ubuntu-1610-x86_64=2 centos-7-x86_64`: # { # "ok": true, @@ -32,31 +32,26 @@ class Utils raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" end - hostnames = [] - # vmpooler reports the domain separately from the hostname domain = response_body.delete('domain') - if domain - # vmpooler output - response_body.each do |os, hosts| - if hosts['hostname'].kind_of?(Array) - hosts['hostname'].map!{ |host| hostnames << host + "." + domain + " (#{os})"} - else - hostnames << hosts["hostname"] + ".#{domain} (#{os})" - end - end - else - response_body.each do |os, hosts| - if hosts['hostname'].kind_of?(Array) - hosts['hostname'].map!{ |host| hostnames << host + " (#{os})" } - else - hostnames << hosts['hostname'] + " (#{os})" - end + result = {} + + response_body.each do |os, value| + hostnames = Array(value['hostname']) + if domain + hostnames.map! {|host| "#{host}.#{domain}"} end + result[os] = hostnames end - hostnames.map { |hostname| puts "- #{hostname}" } + result + end + + def self.format_host_output(hosts) + hosts.flat_map do |os, names| + names.map { |name| "- #{name} (#{os})" } + end.join("\n") end def self.generate_os_hash(os_args) diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb index 47822ca..5a78a61 100644 --- a/spec/vmfloaty/utils_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -5,7 +5,7 @@ require_relative '../../lib/vmfloaty/utils' describe Utils do - describe "#format_hosts" do + describe "#standardize_hostnames" do before :each do @vmpooler_response_body ='{ "ok": true, @@ -26,24 +26,48 @@ describe Utils do "hostname": "power8-ubuntu16.04-6.delivery.mycompany.net" } }' - @vmpooler_output = <<-OUT + end + + it "formats a result from vmpooler into a hash of os to hostnames" do + result = Utils.standardize_hostnames(JSON.parse(@vmpooler_response_body)) + expect(result).to eq('centos-7-x86_64' => ["dlgietfmgeegry2.delivery.mycompany.net"], + 'ubuntu-1610-x86_64' => ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"]) + end + + it "formats a result from the nonstandard pooler into a hash of os to hostnames" do + result = Utils.standardize_hostnames(JSON.parse(@nonstandard_response_body)) + expect(result).to eq('solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'], + 'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net']) + end + end + + describe "#format_host_output" do + before :each do + @vmpooler_results = { + 'centos-7-x86_64' => ["dlgietfmgeegry2.delivery.mycompany.net"], + 'ubuntu-1610-x86_64' => ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"] + } + @nonstandard_results = { + 'solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'], + 'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net'] + } + @vmpooler_output = <<-OUT.chomp +- dlgietfmgeegry2.delivery.mycompany.net (centos-7-x86_64) - gdoy8q3nckuob0i.delivery.mycompany.net (ubuntu-1610-x86_64) - ctnktsd0u11p9tm.delivery.mycompany.net (ubuntu-1610-x86_64) -- dlgietfmgeegry2.delivery.mycompany.net (centos-7-x86_64) OUT - @nonstandard_output = <<-OUT + @nonstandard_output = <<-OUT.chomp - sol10-10.delivery.mycompany.net (solaris-10-sparc) - sol10-11.delivery.mycompany.net (solaris-10-sparc) - power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8) OUT end - it "formats a hostname hash from vmpooler into a list that includes the os" do - expect { Utils.format_hosts(JSON.parse(@vmpooler_response_body)) }.to output( @vmpooler_output).to_stdout_from_any_process + expect(Utils.format_host_output(@vmpooler_results)).to eq(@vmpooler_output) end it "formats a hostname hash from the nonstandard pooler into a list that includes the os" do - expect { Utils.format_hosts(JSON.parse(@nonstandard_response_body)) }.to output(@nonstandard_output).to_stdout_from_any_process + expect(Utils.format_host_output(@nonstandard_results)).to eq(@nonstandard_output) end end