module Fog::Proxmox::Core

Core module

Attributes

deadline[R]
principal[R]
pve_csrftoken[R]
pve_ticket[RW]
pve_username[R]

Public Class Methods

not_found_class() click to toggle source

fallback

# File lib/fog/proxmox/core.rb, line 33
def self.not_found_class
  Fog::Compute::Proxmox::NotFound
end

Public Instance Methods

credentials() click to toggle source
# File lib/fog/proxmox/core.rb, line 54
def credentials
  options = {
    provider: 'proxmox',
    pve_url: @pve_uri.to_s,
    pve_ticket: @pve_ticket,
    pve_csrftoken: @pve_csrftoken,
    pve_username: @pve_username
  }
  pve_options.merge options
end
initialize_identity(options) click to toggle source
# File lib/fog/proxmox/core.rb, line 37
def initialize_identity(options)
  @principal = nil
  @pve_must_reauthenticate = true
  @pve_ticket = nil
  Fog::Proxmox::Variables.to_variables(self, options, 'pve')
  @pve_uri = URI.parse(@pve_url)
  @pve_must_reauthenticate = true unless @pve_ticket
  missing_credentials = []
  missing_credentials << :pve_username unless @pve_username

  unless @pve_ticket
    missing_credentials << :pve_password unless @pve_password
  end

  raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
end
reload() click to toggle source
# File lib/fog/proxmox/core.rb, line 65
def reload
  @connection.reset
end

Private Instance Methods

authenticate() click to toggle source
# File lib/fog/proxmox/core.rb, line 115
def authenticate
  unless @principal
    options = pve_options
    options[:pve_ticket] = @pve_must_reauthenticate ? nil : @pve_ticket
    credentials = Fog::Proxmox.authenticate(options, @connection_options)
    @principal = credentials
    @pve_username = credentials[:username]
    @pve_ticket = credentials[:ticket]
    @pve_deadline = credentials[:deadline]
    @pve_csrftoken = credentials[:csrftoken]
    @pve_must_reauthenticate = false
  end

  @host       = @pve_uri.host
  @api_path   = @pve_uri.path
  @api_path.sub!(%r{/$}, '')
  @port       = @pve_uri.port
  @scheme     = @pve_uri.scheme

  true
end
headers(method, additional_headers) click to toggle source
# File lib/fog/proxmox/core.rb, line 98
def headers(method, additional_headers)
  additional_headers ||= {}
  headers_hash = { 'Accept' => 'application/json' }
  # CSRF token is required to PUT, POST and DELETE http requests
  if %w[PUT POST DELETE].include? method
    headers_hash.store('CSRFPreventionToken', @pve_csrftoken)
  end
  # ticket must be present in cookie
  headers_hash.store('Cookie', "PVEAuthCookie=#{@pve_ticket}") if @pve_ticket
  headers_hash.merge additional_headers
  headers_hash
end
pve_options() click to toggle source
# File lib/fog/proxmox/core.rb, line 111
def pve_options
  Fog::Proxmox::Variables.to_hash(self, 'pve')
end
request(params) click to toggle source
# File lib/fog/proxmox/core.rb, line 71
def request(params)
  retried = false
  begin
    response = @connection.request(params.merge(
                                     headers: headers(params[:method], params[:headers])
    ))
  rescue Excon::Errors::Unauthorized => error
    # token expiration and token renewal possible
    if error.response.body != 'Bad username or password' && @pve_can_reauthenticate && !retried
      authenticate
      retried = true
      retry
    # bad credentials or token renewal not possible
    else
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
          when Excon::Errors::NotFound
            self.class.not_found_class.slurp(error)
          else
            error
          end
  end
  response
end