Class: Proxmox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/proxmox/client.rb

Overview

Proxmox SDK Http Client

Constant Summary collapse

TOKEN_VALIDITY_SECONDS =

Konstanten für die Gültigkeit des Tokens Proxmox Tickets sind standardmäßig 2 Stunden (7200 Sekunden) gültig.

7200
RENEWAL_BUFFER_SECONDS =

Wir erneuern den Token 5 Minuten (300 Sekunden) vor Ablauf.

300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, username:, password:, realm: "pam", ignore_ssl: false) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/proxmox/client.rb', line 18

def initialize(base_url:, username:, password:, realm: "pam", ignore_ssl: false)
  @base_url = base_url
  @verify_ssl = !ignore_ssl

  # Anmeldedaten für die automatische Erneuerung speichern
  @username = username
  @password = password
  @realm = realm

  # Initiales Login
  
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



16
17
18
# File 'lib/proxmox/client.rb', line 16

def base_url
  @base_url
end

#csrf_tokenObject (readonly)

Returns the value of attribute csrf_token.



16
17
18
# File 'lib/proxmox/client.rb', line 16

def csrf_token
  @csrf_token
end

#ticketObject (readonly)

Returns the value of attribute ticket.



16
17
18
# File 'lib/proxmox/client.rb', line 16

def ticket
  @ticket
end

Instance Method Details

#clusterObject



31
32
33
# File 'lib/proxmox/client.rb', line 31

def cluster
  @cluster ||= Proxmox::Resources::Cluster.new(self)
end

#loginObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/proxmox/client.rb', line 45

def 
  resp = http.post("/api2/json/access/ticket",
                   { username: "#{@username}@#{@realm}", password: @password })

  raise "Login failed: #{resp.body}" unless resp.success?

  data = JSON.parse(resp.body)["data"]
  @ticket = data["ticket"]
  @csrf_token = data["CSRFPreventionToken"]
  # Zeitstempel der Ticketerstellung speichern
  @ticket_creation_time = Time.now
end

#node(name) ⇒ Object



35
36
37
# File 'lib/proxmox/client.rb', line 35

def node(name)
  Proxmox::Resources::Node.new(self, name)
end

#nodesObject



39
40
41
42
43
# File 'lib/proxmox/client.rb', line 39

def nodes
  request(:get, "/nodes").map do |node_data|
    Proxmox::Resources::Node.new(self, node_data)
  end
end

#request(method, path, params = {}, body = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/proxmox/client.rb', line 58

def request(method, path, params = {}, body = nil)
  # Vor jeder Anfrage die Gültigkeit des Tokens prüfen und ggf. erneuern
  ensure_token_validity

  response = perform_http_call(method, path, params, body)

  ensure_success!(response)
  extract_data(response)
end