Class: VcData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, save_data) ⇒ VcData

Returns a new instance of VcData.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/VcData.rb', line 15

def initialize , save_data
  # get rid of certificate warning, why does it have to be on stdout anyway?
  $stderr.reopen '/dev/null', 'w'
  url = "https://#{login['host']}/sdk"
  @connection = $mock || VMware::Connection.new(url, ['username'], ['password'], false)
  @session = $mock || VMware::Session.new(@connection)
  @vm_folder = $mock || @session.root_folder.children[0].vm_folder
  @refs = [] 
  @entities = {}
  @save_data = save_data
  @tree_changed = false
end

Instance Attribute Details

#tree_changedObject (readonly)

Returns the value of attribute tree_changed.



14
15
16
# File 'lib/VcData.rb', line 14

def tree_changed
  @tree_changed
end

Instance Method Details

#check_task(index, task) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/VcData.rb', line 59

def check_task index, task
  item = @refs[index]
  t = task['action'] || task
  return false unless item.has_task? t
  return true if task.kind_of? String or task['check'].nil?
  task['check'].each_pair do |k,v|
    next unless item.respond_to? k.to_sym
    return false if item.send(k.to_sym) != v
  end
  true
end

#collect(folder, save_data, indent = '') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/VcData.rb', line 33

def collect folder, save_data, indent=''
  if folder.class.to_s == "VMware::Folder"
    save_data = { :collapsed => false } if !save_data.kind_of? Hash
    i = 0
    result = VMFolder.new folder, @session, indent+folder['name'], save_data[:collapsed] == true
    folder.children.each { |c| i+=1; result.add_child collect c, save_data[c['name']], indent+'  ' }
    return result
  end
  if folder.kind_of? $mock.class and indent == ''
    res = VMFolder.new folder, @session, indent+folder['name']
    res.add_child collect $mock.class.new, [], '  '
    res.add_child collect $mock.class.new, [], '  '
    return res
  end
  VM.new folder, @session, indent+folder['name']
end

#execute_task(index, task, *args) ⇒ Object



53
54
55
# File 'lib/VcData.rb', line 53

def execute_task index, task, *args
  @tree_changed = @refs[index].execute_task task, *args
end

#find_entities(type) ⇒ Object



27
28
29
30
31
32
# File 'lib/VcData.rb', line 27

def find_entities type
  return @entities[type] unless @entities[type].nil?
  @entities[type] = @session.find_entities(type).collect do |ref|
    { :ref => ref, :name => @session.wrap(ref)['name'].to_s }
  end
end

#get(sym, index) ⇒ Object



49
50
51
52
# File 'lib/VcData.rb', line 49

def get sym, index
  i = @refs[index]
  i.send sym if i.respond_to? sym
end

#get_colors(cached = nil) ⇒ Object



70
71
72
# File 'lib/VcData.rb', line 70

def get_colors cached=nil
  @refs.collect {|i| i.color cached }
end

#get_tasks(index, tasks) ⇒ Object



56
57
58
# File 'lib/VcData.rb', line 56

def get_tasks index, tasks
  tasks.keys.collect { |t| (check_task index, tasks[t]) ? t : nil }.compact
end

#recent_eventsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/VcData.rb', line 86

def recent_events
  @session.task_manager.recent_tasks.collect do |t|
    desc = t['name'] || t['descriptionId'] # TODO use task descriptions form TaskManager
    target = t['entityName']
    cancelled = t['cancelled'].to_s
    status = t['state'].to_s
    status = t['error']['localizedMessage'].to_s if status == 'error'
    status = t['progress'].to_s+'%' if status == 'running'
    status = 'running' if status == '%'
    user = t['reason']
    user = user['userName'] || user['name'] || user['alarmName'] || '<System>'
    start = t['startTime']
    queue = t['queueTime']
    complete = t['completeTime']
    [desc,target,cancelled,status,user,start.to_s]
  end
end

#refresh_list(dummy = nil) ⇒ Object



73
74
75
76
# File 'lib/VcData.rb', line 73

def refresh_list dummy=nil
  @save_data = @folders.save_data if !@folders.nil?
  @folders = collect @vm_folder, @save_data
end

#save_dataObject



83
84
85
# File 'lib/VcData.rb', line 83

def save_data
  @folders.save_data
end

#vm_listObject



77
78
79
80
81
82
# File 'lib/VcData.rb', line 77

def vm_list
  @refs.clear
  list = []
  @folders.draw { |i| list << i[0]; @refs << i[1] }
  list
end