module CloudClient

The CloudClient module contains general functionality to implement a Cloud Client

Constants

DEFAULT_AUTH_FILE

######################################################################### Default location for the authentication file #########################################################################

Public Class Methods

get_one_auth() click to toggle source

######################################################################### Gets authorization credentials from ONE_AUTH or default auth file.

Raises an error if authorization is not found #########################################################################

# File lib/deltacloud/drivers/opennebula/cloud_client.rb, line 63
def self.get_one_auth
  if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and
     File.file?(ENV["ONE_AUTH"])
    one_auth=File.read(ENV["ONE_AUTH"]).strip.split(':')
  elsif File.file?(DEFAULT_AUTH_FILE)
    one_auth=File.read(DEFAULT_AUTH_FILE).strip.split(':')
  else
    raise "No authorization data present"
  end

  raise "Authorization data malformed" if one_auth.length < 2

  one_auth
end
http_start(url, timeout, &block) click to toggle source

######################################################################### Starts an http connection and calls the block provided. SSL flag is set if needed. #########################################################################

# File lib/deltacloud/drivers/opennebula/cloud_client.rb, line 82
def self.http_start(url, timeout, &block)
  http = Net::HTTP.new(url.host, url.port)

  if timeout
    http.read_timeout = timeout.to_i
  end

  if url.scheme=='https'
    http.use_ssl = true
    http.verify_mode=OpenSSL::SSL::VERIFY_NONE
  end

  begin
    res = http.start do |connection|
      block.call(connection)
    end
  rescue Errno::ECONNREFUSED => e
    str =  "Error connecting to server (#{e.to_s}).\n"
    str << "Server: #{url.host}:#{url.port}"

    return CloudClient::Error.new(str,"503")
  rescue Errno::ETIMEDOUT => e
    str =  "Error timeout connecting to server (#{e.to_s}).\n"
    str << "Server: #{url.host}:#{url.port}"

    return CloudClient::Error.new(str,"504")
  rescue Timeout::Error => e
    str =  "Error timeout while connected to server (#{e.to_s}).\n"
    str << "Server: #{url.host}:#{url.port}"

    return CloudClient::Error.new(str,"504")
  end

  if res.is_a?(Net::HTTPSuccess)
    res
  else
    CloudClient::Error.new(res.body, res.code)
  end
end
is_error?(value) click to toggle source

######################################################################### Returns true if the object returned by a method of the OpenNebula library is an Error #########################################################################

# File lib/deltacloud/drivers/opennebula/cloud_client.rb, line 145
def self.is_error?(value)
  value.class==CloudClient::Error
end