mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
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:
parent
837fa94af5
commit
50eeb8f265
3 changed files with 53 additions and 28 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue