Class: Archipel::Api::HypervisorApi

Inherits:
Internal::Api show all
Defined in:
lib/archipel/api/hypervisor_api.rb

Instance Method Summary collapse

Methods inherited from Internal::Api

#call, #in_connection, #wait_for_reply

Constructor Details

#initialize(login: nil, password: nil, server: nil, hypervisor: nil) ⇒ HypervisorApi

Returns a new instance of HypervisorApi.



5
6
7
8
9
10
11
# File 'lib/archipel/api/hypervisor_api.rb', line 5

def initialize login: nil, password: nil, server: nil, hypervisor: nil
  @api = Archipel::Api::Internal::HypervisorXmlMessages.new
  super({login: ,
      password: password,
      server: server,
      hypervisor: hypervisor})
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



65
66
67
68
# File 'lib/archipel/api/hypervisor_api.rb', line 65

def method_missing symbol, *args
  xml = @api.send symbol, *args
  call xml
end

Instance Method Details

#create_vm(name, user_jid) ⇒ Object



13
14
15
16
17
18
# File 'lib/archipel/api/hypervisor_api.rb', line 13

def create_vm name, user_jid
  resp = method_missing :create_vm, name, user_jid
  vm_jid = resp['query'][0]['virtualmachine'][0]['jid']
  hypervisor_name = resp['from'].split('@')[0]
  vm_jid + '/' + hypervisor_name
end

#delete_vm(jid_or_name) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/archipel/api/hypervisor_api.rb', line 20

def delete_vm jid_or_name
  if jid_or_name.include? '@'
    jid = jid_or_name
  else
    name = jid_or_name
    jid = get_vm_jid_by_name name
  end
  method_missing :delete_vm, jid
end

#get_vm_jid_by_name(name) ⇒ Object



40
41
42
43
44
# File 'lib/archipel/api/hypervisor_api.rb', line 40

def get_vm_jid_by_name name
  jid = list_vm[name]
  raise "VM #{name} not found" if jid.nil?
  jid
end

#list_users(human_only = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/archipel/api/hypervisor_api.rb', line 46

def list_users human_only = true
  all_users = []
  page = 0
  loop do
    resp = method_missing :list_users, page, human_only
    users = resp['query'][0]['user']
    break if users.blank?

    all_users += users
    page += 1
  end

  all_users.map { |e| e['jid'].downcase }
end

#list_vmObject



30
31
32
33
34
35
36
37
38
# File 'lib/archipel/api/hypervisor_api.rb', line 30

def list_vm
  resp = method_missing :list_vm
  vms_resp = resp['query'][0]['item']
  vms = {}
  vms_resp.each do |vm|
    vms[vm['name']] = vm['content']
  end
  vms
end

#user_exist?(jid) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/archipel/api/hypervisor_api.rb', line 61

def user_exist? jid
  list_users.include? jid.downcase
end