Class: Hetzner::Firewall

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

Instance Method Summary collapse

Constructor Details

#initialize(hetzner_client:, cluster_name:) ⇒ Firewall

Returns a new instance of Firewall.



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

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

Instance Method Details

#create(high_availability:, ssh_networks:, api_networks:) ⇒ 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/firewall.rb', line 10

def create(high_availability:, ssh_networks:, api_networks:)
  @high_availability = high_availability
  @ssh_networks = ssh_networks
  @api_networks = api_networks
  puts

  if (firewall = find_firewall)
    puts 'Firewall already exists, skipping.'
    puts
    return firewall['id']
  end

  puts 'Creating firewall...'

  response = hetzner_client.post('/firewalls', create_firewall_config).body
  puts '...firewall created.'
  puts

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

#delete(servers) ⇒ Object



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

def delete(servers)
  if (firewall = find_firewall)
    puts 'Deleting firewall...'

    servers.each do |server|
      hetzner_client.post("/firewalls/#{firewall['id']}/actions/remove_from_resources", remove_targets_config(server['id']))
    end

    hetzner_client.delete('/firewalls', firewall['id'])
    puts '...firewall deleted.'
  else
    puts 'Firewall no longer exists, skipping.'
  end

  puts
end