class Service::Client

Public Class Methods

new(opts={}) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 321
def initialize(opts={})
    @username = opts[:username] || ENV['ONEFLOW_USER']
    @password = opts[:password] || ENV['ONEFLOW_PASSWORD']

    url = opts[:url] || ENV['ONEFLOW_URL'] || 'http://localhost:2474'

    if @username.nil? && @password.nil?
        if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"])
            one_auth = File.read(ENV["ONE_AUTH"])
        elsif ENV["HOME"] and File.file?(ENV["HOME"]+"/.one/one_auth")
            one_auth = File.read(ENV["HOME"]+"/.one/one_auth")
        elsif File.file?("/var/lib/one/.one/one_auth")
            one_auth = File.read("/var/lib/one/.one/one_auth")
        else
            raise "ONE_AUTH file not present"
        end

        one_auth = one_auth.rstrip

        @username, @password = one_auth.split(':')
    end

    @uri = URI.parse(url)

    @user_agent = "OpenNebula #{CloudClient::VERSION} " <<
        "(#{opts[:user_agent]||"Ruby"})"

    @host = nil
    @port = nil

    if ENV['http_proxy']
        uri_proxy  = URI.parse(ENV['http_proxy'])
        flag = false

        # Check if we need to bypass the proxy
        if ENV['no_proxy']
            ENV['no_proxy'].split(',').each do |item|
                item = item.rstrip.lstrip

                unless (IPAddress @uri.host rescue nil).nil?
                    unless (IPAddress item rescue nil).nil?
                        flag |= IPAddress(item).include? IPAddress(@uri.host)
                    end
                else
                    if (IPAddress(item) rescue nil).nil?
                        flag |= (item == @uri.host)
                    end
                end
            end
        end

        unless flag
            @host = uri_proxy.host
            @port = uri_proxy.port
        end
    end
end

Public Instance Methods

delete(path) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 389
def delete(path)
    req =Net::HTTP::Proxy(@host, @port)::Delete.new(path)

    do_request(req)
end
get(path) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 383
def get(path)
    req = Net::HTTP::Proxy(@host, @port)::Get.new(path)

    do_request(req)
end
login() click to toggle source
# File lib/opennebula/oneflow_client.rb, line 414
def login
    req = Net::HTTP::Proxy(@host, @port)::Post.new('/login')

    do_request(req)
end
logout() click to toggle source
# File lib/opennebula/oneflow_client.rb, line 420
def logout
    req = Net::HTTP::Proxy(@host, @port)::Post.new('/logout')

    do_request(req)
end
post(path, body) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 395
def post(path, body)
    req = Net::HTTP::Proxy(@host, @port)::Post.new(path)
    req.body = body

    if path.start_with?('/service_template')
        unless @content_type.nil?
            req.content_type = @content_type
        end
    end
    do_request(req)
end
put(path, body) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 407
def put(path, body)
    req = Net::HTTP::Proxy(@host, @port)::Put.new(path)
    req.body = body

    do_request(req)
end
set_content_type(content_type) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 379
def set_content_type(content_type)
    @content_type = content_type
end

Private Instance Methods

do_request(req) click to toggle source
# File lib/opennebula/oneflow_client.rb, line 428
def do_request(req)
    req.basic_auth @username, @password

    req['User-Agent'] = @user_agent

    res = CloudClient::http_start(@uri, @timeout) do |http|
        http.request(req)
    end

    res
end