module Fog::Proxmox

Proxmox module

Constants

VERSION

Attributes

credentials[R]
version[R]

Public Class Methods

authenticate(options, connection_options = {}) click to toggle source
# File lib/fog/proxmox.rb, line 50
def self.authenticate(options, connection_options = {})
  get_credentials(options, connection_options)
  self
end
authenticated?() click to toggle source
# File lib/fog/proxmox.rb, line 55
def self.authenticated?
  !@credentials.empty?
end
clear_credentials() click to toggle source
# File lib/fog/proxmox.rb, line 46
def self.clear_credentials
  @credentials = {}
end
credentials_has_expired?() click to toggle source
# File lib/fog/proxmox.rb, line 59
def self.credentials_has_expired?
  authenticated? && @credentials[:deadline] < Time.now
end
extract_password(options) click to toggle source
# File lib/fog/proxmox.rb, line 63
def self.extract_password(options)
  ticket = options[:pve_ticket]
  ticket ? ticket : options[:pve_password].to_s
end
get_credentials(options, connection_options = {}) click to toggle source
# File lib/fog/proxmox.rb, line 68
def self.get_credentials(options, connection_options = {})
  pve_ticket_lifetime   = options[:pve_ticket_lifetime]
  # Default lifetime ticket is 2 hours
  ticket_lifetime = pve_ticket_lifetime ? pve_ticket_lifetime : 2 * 60 * 60
  username          = options[:pve_username].to_s
  password          = extract_password(options)
  url               = options[:pve_url]
  uri = URI.parse(url)
  @api_path = uri.path
  connection_options = connection_options.merge(path_prefix: @api_path)
  password = @credentials[:ticket] if credentials_has_expired?
  request_credentials(uri, connection_options, username, password, ticket_lifetime)
end
request_credentials(uri, connection_options, username, password, ticket_lifetime) click to toggle source
# File lib/fog/proxmox.rb, line 82
def self.request_credentials(uri, connection_options, username, password, ticket_lifetime)
  request = {
    expects: [200, 204],
    headers: { 'Accept' => 'application/json' },
    body: URI.encode_www_form(username: username, password: password),
    method: 'POST',
    path: 'access/ticket'
  }
  connection = Fog::Core::Connection.new(
    uri.to_s,
    false,
    connection_options
  )
  response  = connection.request(request)
  data      = Json.get_data(response)
  ticket    = data['ticket']
  username  = data['username']
  csrftoken = data['CSRFPreventionToken']
  epoch = Time.now.to_i + ticket_lifetime
  deadline = Time.at(epoch)
  save_credentials(username, ticket, csrftoken, deadline)
end
save_credentials(username, ticket, csrftoken, deadline) click to toggle source
# File lib/fog/proxmox.rb, line 105
def self.save_credentials(username, ticket, csrftoken, deadline)
  @credentials = {
    username: username,
    ticket: ticket,
    csrftoken: csrftoken,
    deadline: deadline
  }
end