Class: Chef::Knife::OneandoneSshKeyDelete

Inherits:
Chef::Knife
  • Object
show all
Includes:
OneandoneBase
Defined in:
lib/chef/knife/oneandone_ssh_key_delete.rb

Instance Method Summary collapse

Methods included from OneandoneBase

#formated_output, included, #init_client

Instance Method Details

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/knife/oneandone_ssh_key_delete.rb', line 15

def run
  $stdout.sync = true

  init_client

  name_args.each do |ssh_key_id|
    ssh_key = OneAndOne::SshKey.new

    begin
      ssh_key.get(ssh_key_id: ssh_key_id)
    rescue StandardError => e
      if e.message.include? 'NOT_FOUND'
        ui.error("SSH key ID #{ssh_key_id} not found. Skipping.")
      else
        ui.error(e.message)
      end
      next
    end

    ssh_key_name = ssh_key.specs['name']

    confirm("Do you really want to delete SSH key '#{ssh_key_name}'")

    ssh_key.delete

    if config[:wait]
      begin
        puts ui.color('Deleting, wait for the operation to complete...', :cyan).to_s
        ssh_key.wait_for
        puts "SSH key '#{ssh_key_name}' is #{ui.color('deleted', :bold)}"
      rescue StandardError => e
        if e.message.include? 'NOT_FOUND'
          puts "SSH key '#{ssh_key_name}' is #{ui.color('deleted', :bold)}"
        else
          ui.error(e.message)
        end
      end
    else
      puts "SSH key '#{ssh_key_name}' is #{ui.color('being deleted', :bold)}"
    end
  end
end