module Fog::Proxmox::Core

Core module

Attributes

auth_method[R]
current_user[R]
expires[R]
token[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/fog/proxmox/core.rb, line 40
def initialize(options = {})
  setup(options)
  authenticate
  @auth_token.missing_credentials(options)
  @connection = Fog::Core::Connection.new(@proxmox_url, @persistent, @connection_options)
end
not_found_class() click to toggle source

fallback

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

Public Instance Methods

credentials() click to toggle source
# File lib/fog/proxmox/core.rb, line 68
def credentials
  options = {
    provider: 'proxmox',
    proxmox_auth_method: @auth_method,
    proxmox_url: @proxmox_uri.to_s,
    current_user: @current_user
  }
  proxmox_options.merge options
end
reload() click to toggle source
# File lib/fog/proxmox/core.rb, line 78
def reload
  @connection.reset
end
setup(options) click to toggle source
# File lib/fog/proxmox/core.rb, line 47
def setup(options)
  if options.respond_to?(:config_service?) && options.config_service?
    configure(options)
    return
  end
  Fog::Proxmox::Variables.to_variables(self, options, 'proxmox')
  @connection_options = options[:connection_options] || {}
  @connection_options[:disable_proxy] = true if ENV['DISABLE_PROXY'] == 'true'
  @connection_options[:ssl_verify_peer] = false if ENV['SSL_VERIFY_PEER'] == 'false'
  @connection_options[:ignore_unexpected_eof] = true
  @proxmox_must_reauthenticate = true
  @persistent = options[:persistent] || false
  @token ||= options[:proxmox_token]
  @auth_method ||= options[:proxmox_auth_method]
  @proxmox_can_reauthenticate = if @token
                                  false
                                else
                                  true
                                end
end
user_token?() click to toggle source
# File lib/fog/proxmox/core.rb, line 31
def user_token?
  @auth_token == 'user_token'
end

Private Instance Methods

authenticate() click to toggle source
# File lib/fog/proxmox/core.rb, line 126
def authenticate
  if @proxmox_must_reauthenticate
    @token = nil if @proxmox_must_reauthenticate
    @auth_token = Fog::Proxmox::Auth::Token.build(proxmox_options, @connection_options)
    @current_user = proxmox_options[:proxmox_userid] || proxmox_options[:proxmox_username]
    @token = @auth_token.token
    @expires = @auth_token.expires
    @proxmox_must_reauthenticate = false
  else
    @token = @proxmox_token
  end
  uri = URI.parse(@proxmox_url)
  @path = uri.path
  true
end
authenticate!() click to toggle source
# File lib/fog/proxmox/core.rb, line 142
def authenticate!
  @proxmox_must_reauthenticate = true
  authenticate
end
expired?() click to toggle source
# File lib/fog/proxmox/core.rb, line 84
def expired?
  return false if @expires.nil?
  return false if @expires == 0

  @expires - Time.now.utc.to_i < 60
end
proxmox_options() click to toggle source
# File lib/fog/proxmox/core.rb, line 122
def proxmox_options
  Fog::Proxmox::Variables.to_hash(self, 'proxmox')
end
request(params) click to toggle source
# File lib/fog/proxmox/core.rb, line 91
def request(params)
  retried = false
  begin
    authenticate! if expired?
    request_options = params.merge(path: "#{@path}/#{params[:path]}",
                                   headers: @auth_token.headers(
                                     params[:method], params.respond_to?(:headers) ? params[:headers] : {}, {}
                                   ))
    response = @connection.request(request_options)
  rescue Excon::Errors::Unauthorized => e
    # token expiration and token renewal possible
    if !%w[Bad username or password, invalid token
           value!].include?(e.response.body) && @proxmox_can_reauthenticate && !retried
      authenticate!
      retried = true
      retry
    # bad credentials or token renewal not possible
    else
      raise e
    end
  rescue Excon::Errors::HTTPStatusError => e
    raise case e
          when Excon::Errors::NotFound
            self.class.not_found_class.slurp(e)
          else
            e
          end
  end
  Fog::Proxmox::Json.get_data(response)
end