Add --json option for floaty get

This option will return structured output from `floaty get`, which is
easier to parse in some cases.

Example output:

```json
{
  "centos-7-x86_64": [
    "hpfhhf5aqxowfd8.delivery.puppetlabs.net",
    "k65euywltpz9fz0.delivery.puppetlabs.net"
  ],
  "centos-6-x86_64": [
    "oahunrurl7xis05.delivery.puppetlabs.net"
  ]
}
```
This commit is contained in:
Nick Lewis 2017-12-12 11:22:10 -08:00
parent 837fa94af5
commit 50eeb8f265
3 changed files with 53 additions and 28 deletions

View file

@ -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)