Skip to content

Commit b9781e5

Browse files
author
Brian Cain
committed
Merge pull request #23 from justinstoller/show-tags
Show tag values in `list --active`
2 parents 155aa0e + 1dd8230 commit b9781e5

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ gem 'commander'
44
gem 'faraday'
55

66
gem 'rspec'
7-
gem 'webmock'
7+
gem 'webmock', '1.21.0'
88
gem 'rake'
99

1010
gemspec

lib/vmfloaty/utils.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def self.get_vm_info(hosts, verbose, url)
5151
vms[host]['template'] = vm_info[host]['template']
5252
vms[host]['lifetime'] = vm_info[host]['lifetime']
5353
vms[host]['running'] = vm_info[host]['running']
54+
vms[host]['tags'] = vm_info[host]['tags']
5455
end
5556
end
5657
vms
@@ -64,8 +65,13 @@ def self.prettyprint_hosts(hosts, verbose, url)
6465
template = info['template']
6566
lifetime = info['lifetime']
6667
running = info['running']
68+
tags = info['tags'] || {}
6769

68-
puts "- #{vm}.#{domain} (#{template}, #{running}/#{lifetime} hours)"
70+
tag_pairs = tags.map {|key,value| "#{key}: #{value}" }
71+
duration = "#{running}/#{lifetime} hours"
72+
metadata = [template, duration, *tag_pairs]
73+
74+
puts "- #{vm}.#{domain} (#{metadata.join(", ")})"
6975
end
7076
end
7177
end

spec/vmfloaty/utils_spec.rb

+57
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,61 @@
3131
expect(Utils.generate_os_hash(host_arg)).to be_empty
3232
end
3333
end
34+
35+
describe '#prettyprint_hosts' do
36+
let(:host_without_tags) { 'mcpy42eqjxli9g2' }
37+
let(:host_with_tags) { 'aiydvzpg23r415q' }
38+
let(:url) { 'http://pooler.example.com' }
39+
40+
let(:host_info_with_tags) do
41+
{
42+
host_with_tags => {
43+
"template" => "redhat-7-x86_64",
44+
"lifetime" => 48,
45+
"running" => 7.67,
46+
"tags" => {
47+
"user" => "bob",
48+
"role" => "agent"
49+
},
50+
"domain" => "delivery.puppetlabs.net"
51+
}
52+
}
53+
end
54+
55+
let(:host_info_without_tags) do
56+
{
57+
host_without_tags => {
58+
"template" => "ubuntu-1604-x86_64",
59+
"lifetime" => 12,
60+
"running" => 9.66,
61+
"domain" => "delivery.puppetlabs.net"
62+
}
63+
}
64+
end
65+
66+
let(:output_with_tags) { "- #{host_with_tags}.delivery.puppetlabs.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)" }
67+
let(:output_without_tags) { "- #{host_without_tags}.delivery.puppetlabs.net (ubuntu-1604-x86_64, 9.66/12 hours)" }
68+
69+
it 'prints an output with host fqdn, template and duration info' do
70+
allow(Utils).to receive(:get_vm_info).
71+
with(host_without_tags, false, url).
72+
and_return(host_info_without_tags)
73+
74+
expect(Utils).to receive(:puts).with("Running VMs:")
75+
expect(Utils).to receive(:puts).with(output_without_tags)
76+
77+
Utils.prettyprint_hosts(host_without_tags, false, url)
78+
end
79+
80+
it 'prints an output with host fqdn, template, duration info, and tags when supplied' do
81+
allow(Utils).to receive(:get_vm_info).
82+
with(host_with_tags, false, url).
83+
and_return(host_info_with_tags)
84+
85+
expect(Utils).to receive(:puts).with("Running VMs:")
86+
expect(Utils).to receive(:puts).with(output_with_tags)
87+
88+
Utils.prettyprint_hosts(host_with_tags, false, url)
89+
end
90+
end
3491
end

0 commit comments

Comments
 (0)