diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-11-29 11:23:12 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-29 11:23:12 -0800 |
commit | caca6b6ee91ccf27aeb13877113338ec42736361 (patch) | |
tree | 0b61f823d2e783cb1ffea2e2e57de64c86735678 | |
parent | eb34e8c9ceaaa6d648be21ff353010e45769bdc8 (diff) | |
parent | b2cec008b3abdc06d8edbef8500a43956ab749b5 (diff) | |
download | chef-caca6b6ee91ccf27aeb13877113338ec42736361.tar.gz |
Merge pull request #5572 from jeunito/5436_multiple_deletef
Add ability to pass multiple nodes to knife node/client delete
-rw-r--r-- | lib/chef/knife/client_delete.rb | 21 | ||||
-rw-r--r-- | lib/chef/knife/node_delete.rb | 12 | ||||
-rw-r--r-- | spec/spec_helper.rb | 2 | ||||
-rw-r--r-- | spec/unit/knife/client_delete_spec.rb | 16 | ||||
-rw-r--r-- | spec/unit/knife/node_delete_spec.rb | 29 |
5 files changed, 55 insertions, 25 deletions
diff --git a/lib/chef/knife/client_delete.rb b/lib/chef/knife/client_delete.rb index 08cdf6c7dd..38da5c2a73 100644 --- a/lib/chef/knife/client_delete.rb +++ b/lib/chef/knife/client_delete.rb @@ -32,29 +32,32 @@ class Chef :long => "--delete-validators", :description => "Force deletion of client if it's a validator" - banner "knife client delete CLIENT (options)" + banner "knife client delete [CLIENT[,CLIENT]] (options)" def run - @client_name = @name_args[0] - - if @client_name.nil? + if @name_args.length == 0 show_usage - ui.fatal("You must specify a client name") + ui.fatal("You must specify at least one client name") exit 1 end - delete_object(Chef::ApiClientV1, @client_name, "client") do - object = Chef::ApiClientV1.load(@client_name) + @name_args.each do |client_name| + delete_client(client_name) + end + end + + def delete_client(client_name) + delete_object(Chef::ApiClientV1, client_name, "client") do + object = Chef::ApiClientV1.load(client_name) if object.validator unless config[:delete_validators] - ui.fatal("You must specify --delete-validators to delete the validator client #{@client_name}") + ui.fatal("You must specify --delete-validators to delete the validator client #{client_name}") exit 2 end end object.destroy end end - end end end diff --git a/lib/chef/knife/node_delete.rb b/lib/chef/knife/node_delete.rb index 4dd7d764a1..b03569cded 100644 --- a/lib/chef/knife/node_delete.rb +++ b/lib/chef/knife/node_delete.rb @@ -27,18 +27,18 @@ class Chef require "chef/json_compat" end - banner "knife node delete NODE (options)" + banner "knife node delete [NODE[,NODE]] (options)" def run - @node_name = @name_args[0] - - if @node_name.nil? + if @name_args.length == 0 show_usage - ui.fatal("You must specify a node name") + ui.fatal("You must specify at least one node name") exit 1 end - delete_object(Chef::Node, @node_name) + @name_args.each do |node_name| + delete_object(Chef::Node, node_name) + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index aac5a82f2a..dff2450dd8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -68,6 +68,8 @@ require "chef/config" require "chef/chef_fs/file_system_cache" +require "chef/api_client_v1" + if ENV["CHEF_FIPS"] == "1" Chef::Config.init_openssl end diff --git a/spec/unit/knife/client_delete_spec.rb b/spec/unit/knife/client_delete_spec.rb index 82ef902e09..f20b25f5e6 100644 --- a/spec/unit/knife/client_delete_spec.rb +++ b/spec/unit/knife/client_delete_spec.rb @@ -34,6 +34,22 @@ describe Chef::Knife::ClientDelete do @knife.run end + context "receives multiple clients" do + let(:clients) { %w{ "adam", "ben", "charlie" } } + + before(:each) do + @knife.name_args = clients + end + + it "deletes all clients" do + clients.each do |client| + expect(@knife).to receive(:delete_object).with(Chef::ApiClientV1, client, "client") + end + + @knife.run + end + end + it "should print usage and exit when a client name is not provided" do @knife.name_args = [] expect(@knife).to receive(:show_usage) diff --git a/spec/unit/knife/node_delete_spec.rb b/spec/unit/knife/node_delete_spec.rb index d4ef32bccf..1a6b277dbb 100644 --- a/spec/unit/knife/node_delete_spec.rb +++ b/spec/unit/knife/node_delete_spec.rb @@ -25,12 +25,17 @@ describe Chef::Knife::NodeDelete do @knife.config = { :print_after => nil, } - @knife.name_args = [ "adam" ] + @knife.name_args = %w{ adam ben } allow(@knife).to receive(:output).and_return(true) allow(@knife).to receive(:confirm).and_return(true) - @node = Chef::Node.new() - allow(@node).to receive(:destroy).and_return(true) - allow(Chef::Node).to receive(:load).and_return(@node) + + @adam_node = Chef::Node.new() + @ben_node = Chef::Node.new() + allow(@ben_node).to receive(:destroy).and_return(true) + allow(@adam_node).to receive(:destroy).and_return(true) + allow(Chef::Node).to receive(:load).with("adam").and_return(@adam_node) + allow(Chef::Node).to receive(:load).with("ben").and_return(@ben_node) + @stdout = StringIO.new allow(@knife.ui).to receive(:stdout).and_return(@stdout) end @@ -41,13 +46,15 @@ describe Chef::Knife::NodeDelete do @knife.run end - it "should load the node" do - expect(Chef::Node).to receive(:load).with("adam").and_return(@node) + it "should load the nodes" do + expect(Chef::Node).to receive(:load).with("adam").and_return(@adam_node) + expect(Chef::Node).to receive(:load).with("ben").and_return(@ben_node) @knife.run end - it "should delete the node" do - expect(@node).to receive(:destroy).and_return(@node) + it "should delete the nodes" do + expect(@adam_node).to receive(:destroy).and_return(@adam_node) + expect(@ben_node).to receive(:destroy).and_return(@ben_node) @knife.run end @@ -59,8 +66,10 @@ describe Chef::Knife::NodeDelete do describe "with -p or --print-after" do it "should pretty print the node, formatted for display" do @knife.config[:print_after] = true - expect(@knife).to receive(:format_for_display).with(@node).and_return("poop") - expect(@knife).to receive(:output).with("poop") + expect(@knife).to receive(:format_for_display).with(@adam_node).and_return("adam") + expect(@knife).to receive(:format_for_display).with(@ben_node).and_return("ben") + expect(@knife).to receive(:output).with("adam") + expect(@knife).to receive(:output).with("ben") @knife.run end end |