Class: Service::Client

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

Overview

Interface to OneFlow REST API through a Ruby client

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

The options are read from ENV and FS if not passed

Parameters:

  • opts (Hash) (defaults to: {})

    Required configuration to interact with OneFlow

Options Hash (opts):

  • :url (String)

    Endpoint where OneFlow is running. Defaults to ‘localhost:2474

  • :username (String)

    OpenNebula user

  • :password (String)

    OpenNebula user password

  • :user_agent (String)

    Defaults to Ruby. Oneflow will behave accordingly.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/opennebula/oneflow_client.rb', line 342

def initialize(opts = {})
    endpoint  = '/.one/oneflow_endpoint'
    @username = opts[:username] || ENV['ONEFLOW_USER']
    @password = opts[:password] || ENV['ONEFLOW_PASSWORD']

    if opts[:url]
        url = opts[:url]
    elsif ENV['ONEFLOW_URL']
        url = ENV['ONEFLOW_URL']
    elsif ENV['HOME'] && File.exist?(ENV['HOME'] + endpoint)
        url = File.read(ENV['HOME'] + endpoint).strip
    elsif File.exist?('/var/lib/one/.one/oneflow_endpoint')
        url = File.read('/var/lib/one/.one/oneflow_endpoint').strip
    else
        url = 'http://localhost:2474'
    end

    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

    return unless 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.strip

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

    return if flag

    @host = uri_proxy.host
    @port = uri_proxy.port
end

Instance Method Details

#delete(path, body = nil) ⇒ Object



421
422
423
424
425
426
# File 'lib/opennebula/oneflow_client.rb', line 421

def delete(path, body = nil)
    req = Net::HTTP::Proxy(@host, @port)::Delete.new(path)
    req.body = body if body

    do_request(req)
end

#get(path) ⇒ Object



415
416
417
418
419
# File 'lib/opennebula/oneflow_client.rb', line 415

def get(path)
    req = Net::HTTP::Proxy(@host, @port)::Get.new(path)

    do_request(req)
end

#loginObject



445
446
447
448
449
# File 'lib/opennebula/oneflow_client.rb', line 445

def 
    req = Net::HTTP::Proxy(@host, @port)::Post.new('/login')

    do_request(req)
end

#logoutObject



451
452
453
454
455
# File 'lib/opennebula/oneflow_client.rb', line 451

def logout
    req = Net::HTTP::Proxy(@host, @port)::Post.new('/logout')

    do_request(req)
end

#post(path, body) ⇒ Object



428
429
430
431
432
433
434
435
436
# File 'lib/opennebula/oneflow_client.rb', line 428

def post(path, body)
    req = Net::HTTP::Proxy(@host, @port)::Post.new(path)
    req.body = body

    if path.start_with?('/service_template') && !@content_type.nil?
        req.content_type = @content_type
    end
    do_request(req)
end

#put(path, body) ⇒ Object



438
439
440
441
442
443
# File 'lib/opennebula/oneflow_client.rb', line 438

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) ⇒ Object



411
412
413
# File 'lib/opennebula/oneflow_client.rb', line 411

def set_content_type(content_type)
    @content_type = content_type
end