class OCCIClient::Client

Client Library to interface with the OpenNebula OCCI Service

Attributes

endpoint[RW]

Public Class Methods

new(endpoint_str=nil, user=nil, pass=nil, timeout=nil, debug_flag=true) click to toggle source

Initialize client library

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 37
    def initialize(endpoint_str=nil, user=nil, pass=nil,
             timeout=nil, debug_flag=true)
      @debug   = debug_flag
      @timeout = timeout

      # Server location
      @endpoint = ENV["OCCI_URL"] || endpoint_str || Proc.new(raise "No OpenNebula Provider location configured! Client needs to set \'X-Deltacloud-Provider\' HTTP request header, OR, Deltacloud server administrator must set either the OCCI_URL or API_PROVIDER environment variables")
#"http://localhost:4567"

      # Autentication
      if user && pass
        @occiauth = [user, pass]
      else
        @occiauth = CloudClient::get_one_auth
      end

      if !@occiauth
        raise "No authorization data present"
      end

      @occiauth[1] = Digest::SHA1.hexdigest(@occiauth[1])
    end

Public Instance Methods

delete_image(id) click to toggle source

:id VM identifier

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 272
def delete_image(id)
  delete('/storage/'+id.to_s)
end
delete_network(id) click to toggle source

:id VM identifier

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 249
def delete_network(id)
  delete('/network/'+id.to_s)
end
delete_vm(id) click to toggle source

:id Compute identifier

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 226
def delete_vm(id)
  delete('/compute/'+id.to_s)
end
get_image(id) click to toggle source

Retieves an Image :image_uuid Image identifier

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 257
def get_image(id)
  get('/storage/'+id.to_s)
end
get_images(verbose=false) click to toggle source

Retieves the pool of Images owned by the user

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 199
def get_images(verbose=false)
  get('/storage', verbose)
end
get_instance_types() click to toggle source

Retieves the available Instance types

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 71
def get_instance_types
  get('/instance_type?verbose=yes')
end
get_network(id) click to toggle source

Retrieves a Virtual Network :id Virtual Network identifier

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 234
def get_network(id)
  get('/network/'+id.to_s)
end
get_networks() click to toggle source

Retieves the pool of Virtual Networks

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 101
def get_networks
  get('/network')
end
get_root() click to toggle source

Pool Resource Request Methods #

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 64
def get_root
  get('/')
end
get_vm(id) click to toggle source

:id VM identifier

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 211
def get_vm(id)
  get('/compute/'+id.to_s)
end
get_vms(verbose=false) click to toggle source

Retieves the pool of Virtual Machines

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 86
def get_vms(verbose=false)
  get('/compute', verbose)
end
post_image(xmlfile, curb=true) click to toggle source

Post a new Image to the Image Pool :xmlfile

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 109
def post_image(xmlfile, curb=true)
  xml        = File.read(xmlfile)

  begin
    image_info = REXML::Document.new(xml).root
  rescue Exception => e
    error = CloudClient::Error.new(e.message)
    return error
  end

  if image_info.elements['URL']
    file_path = image_info.elements['URL'].text

    m = file_path.match(%r^\w+:\/\/(.*)$/)

    if m
      file_path="/"+m[1]
    end
  elsif !image_info.elements['TYPE'] == "DATABLOCK"
    return CloudClient::Error.new("Can not find URL")
  end

  if curb
    if !CURL_LOADED
      error_msg = "curb gem not loaded"
      error = CloudClient::Error.new(error_msg)
      return error
    end

    curl=Curl::Easy.new(@endpoint+"/storage")

    curl.http_auth_types     = Curl::CURLAUTH_BASIC
    curl.userpwd             = "#{@occiauth[0]}:#{@occiauth[1]}"
    curl.verbose             = true if @debug
    curl.multipart_form_post = true

    begin
      postfields = Array.new
      postfields << Curl::PostField.content('occixml', xml)

      if file_path
        postfields << Curl::PostField.file('file', file_path)
      end

      curl.http_post(*postfields)
    rescue Exception => e
      return CloudClient::Error.new(e.message)
    end

    return curl.body_str
  else
    if !MULTIPART_LOADED
      error_msg = "multipart-post gem not loaded"
      error = CloudClient::Error.new(error_msg)
      return error
    end

    params=Hash.new

    if file_path
      file=File.open(file_path)
      params["file"]=UploadIO.new(file,
        'application/octet-stream', file_path)
    end

    params['occixml'] = xml

    url = URI.parse(@endpoint+"/storage")

    req = Net::HTTP::Post::Multipart.new(url.path, params)

    req.basic_auth @occiauth[0], @occiauth[1]

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

    file.close if file_path

    if CloudClient::is_error?(res)
      return res
    else
      return res.body
    end
  end
end
post_network(xmlfile) click to toggle source

Post a new Network to the VN Pool :xmlfile xml description of the Virtual Network

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 94
def post_network(xmlfile)
  post('/network', xmlfile)
end
post_vms(xmlfile) click to toggle source

Post a new VM to the VM Pool :xmlfile

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 79
def post_vms(xmlfile)
  post('/compute', xmlfile)
end
put_image(xmlfile) click to toggle source

Puts a new Storage representation in order to change its state :xmlfile Storage OCCI xml representation

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 265
def put_image(xmlfile)
  put('/storage/', xmlfile)
end
put_network(xmlfile) click to toggle source

Puts a new Network representation in order to change its state :xmlfile Network OCCI xml representation

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 242
def put_network(xmlfile)
  put('/network/', xmlfile)
end
put_vm(xmlfile) click to toggle source

Puts a new Compute representation in order to change its state :xmlfile Compute OCCI xml representation

# File lib/deltacloud/drivers/opennebula/occi_client.rb, line 219
def put_vm(xmlfile)
  put('/compute/', xmlfile)
end