Class: ProxmoxAPI

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

Overview

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

Author:

  • Eugene Lapeko

Defined Under Namespace

Classes: ApiException, ApiPath

Constant Summary collapse

AUTH_PARAMS =
%i[username realm password otp].freeze
RESOURCE_OPTIONS =
%i[headers].freeze
REST_METHODS =
%i[get post put delete].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, options) ⇒ ProxmoxAPI

Constructor method for ProxmoxAPI

You can also pass here all ssl options supported by rest-client gem

Parameters:

  • cluster (String)

    hostname/ip of cluster to control

  • options (Hash)

    cluster connection parameters

Options Hash (options):

  • :username (String)
    • username to be used for connection

  • :password (String)
    • password to be used for connection

  • :realm (String)
    • auth realm, can be given in :username (‘user@realm’)

  • :token (String)
    • token to be used instead of username, password, and realm

  • :secret (String)
    • secret to be used with token

  • :otp (String)
    • one-time password for two-factor auth

  • :verify_ssl (Boolean)
    • verify server certificate

See Also:



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/proxmox_api.rb', line 76

def initialize(cluster, options)
  if options.key?(:token) && options.key?(:secret)
    options[:headers] = { Authorization: "PVEAPIToken=#{options[:token]}=#{options[:secret]}" }
  end

  @connection = RestClient::Resource.new(
    "https://#{cluster}:#{options[:port] || 8006}/api2/json/",
    options.select { |k, _v| connection_options.include?(k.to_s) }
  )
  @auth_ticket = options.key?(:token) ? {} : create_auth_ticket(options.select { |k, _v| AUTH_PARAMS.include? k })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



92
93
94
# File 'lib/proxmox_api.rb', line 92

def method_missing(method, *args)
  ApiPath.new(self).__send__(method, *args)
end

Class Method Details

.connection_optionsObject

The list of options to be passed to RestClient object



101
102
103
# File 'lib/proxmox_api.rb', line 101

def self.connection_options
  RestClient::Request::SSLOptionList.unshift('verify_ssl') + RESOURCE_OPTIONS
end

Instance Method Details

#[](index) ⇒ Object



88
89
90
# File 'lib/proxmox_api.rb', line 88

def [](index)
  ApiPath.new(self)[index]
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/proxmox_api.rb', line 96

def respond_to_missing?(*)
  true
end