class ProxmoxAPI

This class is wrapper for Proxmox PVE APIv2. See README for usage examples.

@author Eugene Lapeko

Constants

AUTH_PARAMS
REST_METHODS

Public Class Methods

new(cluster, options) click to toggle source

Constructor method for ProxmoxAPI

@param [String] cluster hostname/ip of cluster to control @param [Hash] options cluster connection parameters

@option options [String] :username - username to be used for connection @option options [String] :password - password to be used for connection @option options [String] :realm - auth realm, can be given in :username ('user@realm') @option options [String] :otp - one-time password for two-factor auth

@option options [Boolean] :verify_ssl - verify server certificate

You can also pass here all ssl options supported by rest-client gem @see github.com/rest-client/rest-client

# File lib/proxmox_api.rb, line 73
def initialize(cluster, options)
  @connection = RestClient::Resource.new(
    "https://#{cluster}:#{options[:port] || 8006}/api2/json/",
    options.select { |k, _v| RestClient::Request::SSLOptionList.unshift('verify_ssl').include? k.to_s }
  )
  @auth_ticket = create_auth_ticket(options.select { |k, _v| AUTH_PARAMS.include? k })
end

Public Instance Methods

[](index) click to toggle source
# File lib/proxmox_api.rb, line 81
def [](index)
  ApiPath.new(self)[index]
end
method_missing(method, *args) click to toggle source
# File lib/proxmox_api.rb, line 85
def method_missing(method, *args)
  ApiPath.new(self).__send__(method, *args)
end
respond_to_missing?(*) click to toggle source
# File lib/proxmox_api.rb, line 89
def respond_to_missing?(*)
  true
end

Private Instance Methods

create_auth_ticket(options) click to toggle source
# File lib/proxmox_api.rb, line 101
def create_auth_ticket(options)
  @connection['access/ticket'].post options do |response, _request, _result, &_block|
    raise_on_failure(response, 'Proxmox authentication failure')

    data = JSON.parse(response.body, symbolize_names: true)[:data]
    {
      cookies: { PVEAuthCookie: data[:ticket] },
      CSRFPreventionToken: data[:CSRFPreventionToken]
    }
  end
end
prepare_options(method, data) click to toggle source
# File lib/proxmox_api.rb, line 113
def prepare_options(method, data)
  case method
  when :post, :put
    [data, @auth_ticket]
  when :delete
    [@auth_ticket]
  when :get
    [@auth_ticket.merge(data)]
  end
end
raise_on_failure(response, message = 'Proxmox API request failed') click to toggle source
# File lib/proxmox_api.rb, line 95
def raise_on_failure(response, message = 'Proxmox API request failed')
  return unless response.code.to_i >= 400

  raise ApiException.new(response, message)
end
submit(method, url, data = {}) click to toggle source
# File lib/proxmox_api.rb, line 124
def submit(method, url, data = {})
  if /!$/.match? method
    method = method.to_s.tr('!', '').to_sym
    skip_raise = true
  end

  @connection[url].__send__(method, *prepare_options(method, data)) do |response|
    raise_on_failure(response) unless skip_raise

    JSON.parse(response.body, symbolize_names: true)[:data]
  end
end