Merge pull request #47 from nicklewis/json-output-for-get

🎂🎂🎂 Add --json option for `floaty get`
This commit is contained in:
Brian Cain 2018-01-04 16:53:14 -08:00 committed by GitHub
commit 5837351dd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 28 deletions

View file

@ -35,6 +35,7 @@ class Vmfloaty
c.option '--token STRING', String, 'Token for pooler service' c.option '--token STRING', String, 'Token for pooler service'
c.option '--notoken', 'Makes a request without a token' c.option '--notoken', 'Makes a request without a token'
c.option '--force', 'Forces vmfloaty to get requested vms' c.option '--force', 'Forces vmfloaty to get requested vms'
c.option '--json', 'Prints retrieved vms in JSON format'
c.action do |args, options| c.action do |args, options|
verbose = options.verbose || config['verbose'] verbose = options.verbose || config['verbose']
service = Service.new(options, config) service = Service.new(options, config)
@ -62,7 +63,12 @@ class Vmfloaty
end end
response = service.retrieve(verbose, os_types, use_token) 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
end end

View file

@ -4,7 +4,7 @@ require 'vmfloaty/nonstandard_pooler'
class Utils class Utils
# TODO: Takes the json response body from an HTTP GET # TODO: Takes the json response body from an HTTP GET
# request and "pretty prints" it # 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`: # vmpooler response body example when `floaty get` arguments are `ubuntu-1610-x86_64=2 centos-7-x86_64`:
# { # {
# "ok": true, # "ok": true,
@ -32,31 +32,26 @@ class Utils
raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}"
end end
hostnames = []
# vmpooler reports the domain separately from the hostname # vmpooler reports the domain separately from the hostname
domain = response_body.delete('domain') domain = response_body.delete('domain')
result = {}
response_body.each do |os, value|
hostnames = Array(value['hostname'])
if domain if domain
# vmpooler output hostnames.map! {|host| "#{host}.#{domain}"}
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
end end
result[os] = hostnames
end 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 end
def self.generate_os_hash(os_args) def self.generate_os_hash(os_args)

View file

@ -5,7 +5,7 @@ require_relative '../../lib/vmfloaty/utils'
describe Utils do describe Utils do
describe "#format_hosts" do describe "#standardize_hostnames" do
before :each do before :each do
@vmpooler_response_body ='{ @vmpooler_response_body ='{
"ok": true, "ok": true,
@ -26,24 +26,48 @@ describe Utils do
"hostname": "power8-ubuntu16.04-6.delivery.mycompany.net" "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) - gdoy8q3nckuob0i.delivery.mycompany.net (ubuntu-1610-x86_64)
- ctnktsd0u11p9tm.delivery.mycompany.net (ubuntu-1610-x86_64) - ctnktsd0u11p9tm.delivery.mycompany.net (ubuntu-1610-x86_64)
- dlgietfmgeegry2.delivery.mycompany.net (centos-7-x86_64)
OUT OUT
@nonstandard_output = <<-OUT @nonstandard_output = <<-OUT.chomp
- sol10-10.delivery.mycompany.net (solaris-10-sparc) - sol10-10.delivery.mycompany.net (solaris-10-sparc)
- sol10-11.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) - power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8)
OUT OUT
end end
it "formats a hostname hash from vmpooler into a list that includes the os" do 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 end
it "formats a hostname hash from the nonstandard pooler into a list that includes the os" do 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
end end