Skip to content

Commit f8b9332

Browse files
Nuo YanSeth Falcon
authored andcommitted
knife node list -E ENV lists the nodes within an environment
1 parent 7c5039d commit f8b9332

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

chef/lib/chef/knife/node_list.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# Author:: Adam Jacob (<adam@opscode.com>)
3-
# Copyright:: Copyright (c) 2009 Opscode, Inc.
3+
# Copyright:: Copyright (c) 2010 Opscode, Inc.
44
# License:: Apache License, Version 2.0
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,8 +32,19 @@ class NodeList < Knife
3232
:description => "Show corresponding URIs"
3333

3434
def run
35-
output(format_list_for_display(Chef::Node.list))
35+
output(format_list_for_display(retrieve_node_list))
3636
end
37+
38+
def retrieve_node_list
39+
if config[:environment]
40+
response = Hash.new
41+
Chef::Search::Query.new.search(:node, "chef_environment:#{config[:environment]}") {|n| response[n.name] = "#{Chef::Config[:chef_server_url]}/nodes/#{n.name}" unless n.nil?}
42+
response
43+
else
44+
Chef::Node.list
45+
end
46+
end
47+
3748
end
3849
end
3950
end

chef/spec/unit/knife/node_list_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
"foo" => "http://example.com/foo",
2828
"bar" => "http://example.com/foo"
2929
}
30+
Chef::REST.stub!(:new).and_return("foo")
31+
@chef_search_query = Chef::Search::Query.new
3032
Chef::Node.stub!(:list).and_return(@list)
33+
Chef::Search::Query.stub!(:new).and_return(@chef_search_query)
3134
end
3235

3336
describe "run" do
34-
it "should list the nodes" do
37+
it "should list all of the nodes if -E is not specified" do
3538
Chef::Node.should_receive(:list).and_return(@list)
3639
@knife.run
3740
end
@@ -42,6 +45,13 @@
4245
@knife.run
4346
end
4447

48+
it "should list nodes in the specific environment if -E ENVIRONMENT is specified" do
49+
@knife.config = {:environment => "prod"}
50+
Chef::Search::Query.should_receive(:new).and_return(@chef_search_query)
51+
@chef_search_query.should_receive(:search).with(:node, "chef_environment:prod").and_return(["foo","bar"])
52+
@knife.run
53+
end
54+
4555
describe "with -w or --with-uri" do
4656
it "should pretty print the hash" do
4757
@knife.config[:with_uri] = true

0 commit comments

Comments
 (0)