diff options
author | Daniel DeLeo <dan@opscode.com> | 2011-04-12 09:28:04 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2011-04-12 09:28:04 -0700 |
commit | 11198fe9b641127084ce3c062a54d8c3842f19b5 (patch) | |
tree | 434784a9fa31c42353a6f63805cb0cb69adf730e | |
parent | 571dd9c7a0f09bf291d3396e77afa0533528f934 (diff) | |
download | chef-11198fe9b641127084ce3c062a54d8c3842f19b5.tar.gz |
print the list of nodes to be deleted in columns
-rw-r--r-- | chef/lib/chef/knife/node_bulk_delete.rb | 40 | ||||
-rw-r--r-- | chef/spec/unit/knife/node_bulk_delete_spec.rb | 68 |
2 files changed, 73 insertions, 35 deletions
diff --git a/chef/lib/chef/knife/node_bulk_delete.rb b/chef/lib/chef/knife/node_bulk_delete.rb index dd2ddbfdff..89b2abe6ae 100644 --- a/chef/lib/chef/knife/node_bulk_delete.rb +++ b/chef/lib/chef/knife/node_bulk_delete.rb @@ -30,13 +30,47 @@ class Chef banner "knife node bulk delete REGEX (options)" def run - if @name_args.length < 1 + if name_args.length < 1 ui.fatal("You must supply a regular expression to match the results against") exit 42 - else - bulk_delete(Chef::Node, "node", nil, nil, @name_args[0]) + end + + + nodes_to_delete = {} + matcher = /#{name_args[0]}/ + + all_nodes.each do |name, node| + next unless name =~ matcher + nodes_to_delete[name] = node + end + + if nodes_to_delete.empty? + ui.msg "No nodes match the expression /#{name_args[0]}/" + exit 0 + end + + ui.msg("The following nodes will be deleted:") + ui.msg("") + ui.msg(ui.list(nodes_to_delete.keys.sort, :columns_down)) + ui.msg("") + ui.confirm("Are you sure you want to delete these nodes") + + + nodes_to_delete.sort.each do |name, node| + node.destroy + ui.msg("Deleted node #{name}") end end + + def all_nodes + node_uris_by_name = Chef::Node.list + + node_uris_by_name.keys.inject({}) do |nodes_by_name, name| + nodes_by_name[name] = Chef::Node.new.tap {|n| n.name(name)} + nodes_by_name + end + end + end end end diff --git a/chef/spec/unit/knife/node_bulk_delete_spec.rb b/chef/spec/unit/knife/node_bulk_delete_spec.rb index 59d387a9df..d14a01fa21 100644 --- a/chef/spec/unit/knife/node_bulk_delete_spec.rb +++ b/chef/spec/unit/knife/node_bulk_delete_spec.rb @@ -6,9 +6,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,41 +24,54 @@ describe Chef::Knife::NodeBulkDelete do Chef::Config[:node_name] = "webmonkey.example.com" @knife = Chef::Knife::NodeBulkDelete.new - @knife.config = { - :print_after => nil - } - @knife.name_args = ["."] - @knife.stub!(:output).and_return(true) - @knife.stub!(:confirm).and_return(true) + @knife.name_args = ["."] + @stdout = StringIO.new + @knife.ui.stub!(:stdout).and_return(@stdout) + @knife.ui.stub!(:confirm).and_return(true) @nodes = Hash.new %w{adam brent jacob}.each do |node_name| - node = Chef::Node.new() - node.name(node_name) - node.stub!(:destroy).and_return(true) - @nodes[node_name] = node + @nodes[node_name] = "http://localhost:4000/nodes/#{node_name}" end - Chef::Node.stub!(:list).and_return(@nodes) end - describe "run" do - - it "should get the list of inflated nodes" do + describe "when creating the list of nodes" do + it "fetches the node list" do + expected = @nodes.inject({}) do |inflatedish, (name, uri)| + inflatedish[name] = Chef::Node.new.tap {|n| n.name(name)} + inflatedish + end Chef::Node.should_receive(:list).and_return(@nodes) - @knife.run + # I hate not having == defined for anything :( + actual = @knife.all_nodes + actual.keys.should =~ expected.keys + actual.values.map {|n| n.name }.should =~ %w[adam brent jacob] + end + end + + describe "run" do + before do + @inflatedish_list = @nodes.keys.inject({}) do |nodes_by_name, name| + node = Chef::Node.new() + node.name(name) + node.stub!(:destroy).and_return(true) + nodes_by_name[name] = node + nodes_by_name + end + @knife.stub!(:all_nodes).and_return(@inflatedish_list) end it "should print the nodes you are about to delete" do - @knife.should_receive(:output).with(@knife.format_list_for_display(@nodes)) @knife.run + @stdout.string.should match(/#{@knife.ui.list(@nodes.keys.sort, :columns_down)}/) end it "should confirm you really want to delete them" do - @knife.should_receive(:confirm) + @knife.ui.should_receive(:confirm) @knife.run end it "should delete each node" do - @nodes.each_value do |n| + @inflatedish_list.each_value do |n| n.should_receive(:destroy) end @knife.run @@ -66,9 +79,9 @@ describe Chef::Knife::NodeBulkDelete do it "should only delete nodes that match the regex" do @knife.name_args = ['adam'] - @nodes['adam'].should_receive(:destroy) - @nodes['brent'].should_not_receive(:destroy) - @nodes['jacob'].should_not_receive(:destroy) + @inflatedish_list['adam'].should_receive(:destroy) + @inflatedish_list['brent'].should_not_receive(:destroy) + @inflatedish_list['jacob'].should_not_receive(:destroy) @knife.run end @@ -77,15 +90,6 @@ describe Chef::Knife::NodeBulkDelete do lambda { @knife.run }.should raise_error(SystemExit) end - describe "with -p or --print-after" do - it "should pretty print the node, formatted for display" do - @knife.config[:print_after] = true - @nodes.each_value do |n| - @knife.should_receive(:output).with(@knife.format_for_display(n)) - end - @knife.run - end - end end end |