Class: Fog::XenServer::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/xenserver/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, timeout) ⇒ Connection

Returns a new instance of Connection.



26
27
28
29
30
# File 'lib/fog/xenserver/core.rb', line 26

def initialize(host, timeout)
  @factory = XMLRPC::Client.new(host, '/')
  @factory.set_parser(NokogiriStreamParser.new)
  @factory.timeout = timeout
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



24
25
26
# File 'lib/fog/xenserver/core.rb', line 24

def credentials
  @credentials
end

Instance Method Details

#authenticate(username, password) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/xenserver/core.rb', line 32

def authenticate( username, password )
  response = @factory.call('session.login_with_password', username.to_s, password.to_s)
  if response['Status'] == 'Failure'
    if response['ErrorDescription'][0] == 'HOST_IS_SLAVE'
      master_ip = response['ErrorDescription'][1]
      raise Fog::XenServer::HostIsSlave.new(master_ip)
    else
      raise Fog::XenServer::InvalidLogin.new
    end
  end
  @credentials = response["Value"]
end

#request(options, *params) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/xenserver/core.rb', line 45

def request(options, *params)
  begin
    parser   = options.delete(:parser)
    method   = options.delete(:method)

    if params.empty?
      response = @factory.call(method, @credentials)
    else
      if params.length.eql?(1) and params.first.is_a?(Hash)
        response = @factory.call(method, @credentials, params.first)
      elsif params.length.eql?(2) and params.last.is_a?(Array)
        response = @factory.call(method, @credentials, params.first, params.last)
      else
        response = eval("@factory.call('#{method}', '#{@credentials}', #{params.map {|p|  p.is_a?(String) ? "'#{p}'" : p}.join(',')})")
      end
    end
    raise RequestFailed.new("#{method}: " + response["ErrorDescription"].to_s) unless response["Status"].eql? "Success"
    if parser
      parser.parse( response["Value"] )
      response = parser.response
    end

    response
  end
end