Files
ubicloud/cli-commands/vm/post/show.rb
Jeremy Evans ea3f64ca9d Improve display of option values in CLI
Show available option values in separate section after options display.
In cases where Options can be taken from elsewhere in the application,
do that instead of hard coding the options.  This allows burstable-1
and burstable-2 to show up as VM/PG size options, and show more
available VM/PG storage sizes. This doesn't show which storage sizes
are valid for a given VM/PG size.  That's possible but would clutter
the display.

Update to a later rodish checkout, which moved some of the features
UbiCli uses to plugins, and load those plugins.
2025-03-19 14:08:49 -07:00

58 lines
2.2 KiB
Ruby

# frozen_string_literal: true
UbiCli.on("vm").run_on("show") do
desc "Show details for a virtual machine"
fields = %w[id name state location size unix-user storage-size-gib ip6 ip4-enabled ip4 private-ipv4 private-ipv6 subnet firewalls].freeze.each(&:freeze)
firewall_fields = %w[id name description location path firewall-rules].freeze.each(&:freeze)
firewall_rule_fields = %w[id cidr port-range].freeze.each(&:freeze)
options("ubi vm (location/vm-name | vm-id) show [options]", key: :vm_show) do
on("-f", "--fields=fields", "show specific fields (comma separated)")
on("-r", "--rule-fields=fields", "show specific firewall rule fields (comma separated)")
on("-w", "--firewall-fields=fields", "show specific firewall fields (comma separated)")
end
help_option_values("Fields:", fields)
help_option_values("Firewall Rule Fields:", firewall_rule_fields)
help_option_values("Firewall Fields:", firewall_fields)
run do |opts|
get(vm_path) do |data|
opts = opts[:vm_show]
keys = check_fields(opts[:fields], fields, "vm show -f option")
firewall_keys = check_fields(opts[:"firewall-fields"], firewall_fields, "vm show -w option")
firewall_rule_keys = check_fields(opts[:"rule-fields"], firewall_rule_fields, "vm show -r option")
body = []
firewall_keys = underscore_keys(firewall_keys)
firewall_rule_keys = underscore_keys(firewall_rule_keys)
underscore_keys(keys).each do |key|
if key == "firewalls"
data[key].each_with_index do |firewall, i|
body << "firewall " << (i + 1).to_s << ":\n"
firewall_keys.each do |fw_key|
if fw_key == "firewall_rules"
body << " rules:\n"
firewall[fw_key].each_with_index do |rule, i|
body << " " << (i + 1).to_s << ": "
firewall_rule_keys.each do |fwr_key|
body << rule[fwr_key].to_s << " "
end
body << "\n"
end
else
body << " " << fw_key << ": " << firewall[fw_key].to_s << "\n"
end
end
end
else
body << key << ": " << data[key].to_s << "\n"
end
end
body
end
end
end