Class: Hetzner::SSHKey

Inherits:
Object
  • Object
show all
Defined in:
lib/hetzner/infra/ssh_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(hetzner_client:, cluster_name:) ⇒ SSHKey

Returns a new instance of SSHKey.



5
6
7
8
# File 'lib/hetzner/infra/ssh_key.rb', line 5

def initialize(hetzner_client:, cluster_name:)
  @hetzner_client = hetzner_client
  @cluster_name = cluster_name
end

Instance Method Details

#create(public_ssh_key_path:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hetzner/infra/ssh_key.rb', line 10

def create(public_ssh_key_path:)
  @public_ssh_key_path = public_ssh_key_path

  puts

  if (public_ssh_key = find_public_ssh_key)
    puts 'SSH key already exists, skipping.'
    puts
    return public_ssh_key['id']
  end

  puts 'Creating SSH key...'

  response = hetzner_client.post('/ssh_keys', ssh_key_config).body

  puts '...SSH key created.'
  puts

  JSON.parse(response)['ssh_key']['id']
end

#delete(public_ssh_key_path:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hetzner/infra/ssh_key.rb', line 31

def delete(public_ssh_key_path:)
  @public_ssh_key_path = public_ssh_key_path

  if (public_ssh_key = find_public_ssh_key)
    if public_ssh_key['name'] == cluster_name
      puts 'Deleting ssh_key...'
      hetzner_client.delete('/ssh_keys', public_ssh_key['id'])
      puts '...ssh_key deleted.'
    else
      puts "The SSH key existed before creating the cluster, so I won't delete it."
    end
  else
    puts 'SSH key no longer exists, skipping.'
  end

  puts
end