Class: Chef::Knife::Cloud::ServerDeleteCommand

Inherits:
Command show all
Defined in:
lib/chef/knife/cloud/server/delete_command.rb

Instance Attribute Summary

Attributes inherited from Command

#custom_arguments, #service

Instance Method Summary collapse

Methods inherited from Command

#run

Methods included from Helpers

#after_exec_command, #before_exec_command, #create_service_instance, #msg_pair, #pretty_key, #set_default_config, #validate!, #validate_params!

Instance Method Details

#delete_from_chef(server_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/knife/cloud/server/delete_command.rb', line 36

def delete_from_chef(server_name)
  # delete the node from Chef if purge requested.
  if config[:purge]
    thing_to_delete = config[:chef_node_name] || server_name
    destroy_item(Chef::Node, thing_to_delete, "node")
    destroy_item(Chef::ApiClient, thing_to_delete, "client")
  else
    ui.warn("Corresponding node and client for the #{server_name} server were not deleted and remain registered with the Chef Server")
  end
end

#destroy_item(klass, name, type_name) ⇒ Object

Extracted from Chef::Knife.delete_object, because it has a confirmation step built in… By specifying the ‘–purge’ flag (and also explicitly confirming the server destruction!) the user is already making their intent known. It is not necessary to make them confirm two more times.



52
53
54
55
56
57
58
59
60
# File 'lib/chef/knife/cloud/server/delete_command.rb', line 52

def destroy_item(klass, name, type_name)
  object = klass.load(name)
  object.destroy
  ui.warn("Deleted #{type_name} #{name}")
rescue Net::HTTPServerException => e
  error_message = "#{e.message}. Could not find a #{type_name} named #{name} to delete!"
  ui.warn(error_message)
  raise CloudExceptions::ServerDeleteError, error_message
end

#execute_commandObject



29
30
31
32
33
34
# File 'lib/chef/knife/cloud/server/delete_command.rb', line 29

def execute_command
  @name_args.each do |server_name|
    service.delete_server(server_name)
    delete_from_chef(server_name)
  end
end