Module: PrometheusExporterHcloudSd::HetznerServers

Defined in:
lib/prometheus_exporter_hcloud_sd/hetzner_servers.rb

Constant Summary collapse

SERVERS_URI =
URI.parse("https://api.hetzner.cloud/v1/servers").freeze

Class Method Summary collapse

Class Method Details

.private_ipsObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/prometheus_exporter_hcloud_sd/hetzner_servers.rb', line 9

def self.private_ips
  token = ENV.fetch("HCLOUD_READ_TOKEN")
  response = Net::HTTP.get(SERVERS_URI, { Authorization: "Bearer #{token}" })
  json_response = JSON.parse(response, symbolize_names: true)

  json_response.fetch(:servers, []).map do |server|
    server.fetch(:private_net, [{}]).first.fetch(:ip, nil)
  end.compact
rescue KeyError
  raise Thor::Error, "Cannot continue.\nPlease set the HCLOUD_READ_TOKEN environment variable."
end