Class: Hetzner::LoadBalancer

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

Instance Method Summary collapse

Constructor Details

#initialize(hetzner_client:, cluster_name:) ⇒ LoadBalancer

Returns a new instance of LoadBalancer.



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

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

Instance Method Details

#create(location:, network_id:) ⇒ 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/load_balancer.rb', line 10

def create(location:, network_id:)
  @location = location
  @network_id = network_id

  puts

  if (load_balancer = find_load_balancer)
    puts 'API load balancer already exists, skipping.'
    puts
    return load_balancer['id']
  end

  puts 'Creating API load_balancer...'

  response = hetzner_client.post('/load_balancers', create_load_balancer_config).body
  puts '...API load balancer created.'
  puts

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

#delete(high_availability:) ⇒ Object



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

def delete(high_availability:)
  if (load_balancer = find_load_balancer)
    puts 'Deleting API load balancer...' unless high_availability

    hetzner_client.post("/load_balancers/#{load_balancer['id']}/actions/remove_target", remove_targets_config)
    hetzner_client.delete('/load_balancers', load_balancer['id'])

    puts '...API load balancer deleted.' unless high_availability
  elsif high_availability
    puts 'API load balancer no longer exists, skipping.'
  end

  puts
end