/
collector.rb
196 lines (168 loc) · 6.03 KB
/
collector.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class ManageIQ::Providers::Redhat::Inventory::Collector < ManageIQ::Providers::Inventory::Collector
# TODO: review the changes here and find common parts with ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies::V4
require_nested :InfraManager
require_nested :TargetCollection
attr_reader :ems_clusters
attr_reader :networks
attr_reader :storagedomains
attr_reader :datacenters
attr_reader :hosts
attr_reader :vms
attr_reader :templates
VERSION_HASH = {:version => 4}.freeze
def initialize(manager, _target)
super
initialize_inventory_sources
end
def initialize_inventory_sources
@ems_clusters = []
@networks = []
@storagedomains = []
@datacenters = []
@hosts = []
@vms = []
@templates = []
end
def collect_ems_clusters
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.clusters_service.list
end
end
def collect_networks
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.networks_service.list
end
end
def collect_vnic_profiles
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.vnic_profiles_service.list
end
end
def collect_storagedomains
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.storage_domains_service.list
end
end
def collect_datacenters
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.data_centers_service.list
end
end
def collect_cluster_for_host(host)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(host.cluster)
end
end
def collect_host_stats(host)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.link?(host.statistics) ? connection.follow_link(host.statistics) : host.statistics
end
end
def collect_datacenter_for_cluster(cluster)
return unless cluster.data_center
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(cluster.data_center)
end
end
def collect_attached_disks(disks_owner)
manager.with_provider_connection(VERSION_HASH) do |connection|
AttachedDisksFetcher.collect_attached_disks(disks_owner, connection, preloaded_disks)
end
end
# This code is copied from redhat/infra_manager/inventory/strategies/v4.rb it is a part of other code
# which will should be extracted to a common place when the graph refresh is stabelized.
def preloaded_disks
@preloaded_disks ||= collect_disks_as_hash
end
# This code is copied from redhat/infra_manager/inventory/strategies/v4.rb it is a part of other code
# which will should be extracted to a common place when the graph refresh is stabelized.
def collect_disks_as_hash
manager.with_provider_connection(VERSION_HASH) do |connection|
Hash[connection.system_service.disks_service.list.collect { |d| [d.id, d] }]
end
end
# This code is copied from redhat/infra_manager/inventory/strategies/v4.rb it is a part of other code
# which will should be extracted to a common place when the graph refresh is stabelized.
class AttachedDisksFetcher
def self.collect_attached_disks(disks_owner, connection, preloaded_disks = nil)
attachments = connection.follow_link(disks_owner.disk_attachments)
attachments.map do |attachment|
res = disk_from_attachment(connection, attachment, preloaded_disks)
res.interface = attachment.interface
res.bootable = attachment.bootable
res.active = attachment.active
res
end
end
def self.disk_from_attachment(connection, attachment, preloaded_disks)
disk = preloaded_disks && preloaded_disks[attachment.disk.id]
disk || connection.follow_link(attachment.disk)
end
end
def collect_nics(nic_owner)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(nic_owner.nics)
end
end
def collect_vm_devices(vm)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(vm.reported_devices)
end
end
def collect_snapshots(vm)
manager.with_provider_connection(VERSION_HASH) do |connection|
snapshots = connection.follow_link(vm.snapshots)
self.class.add_snapshot_disks_total_size(connection, snapshots, vm.id)
end
end
def self.add_snapshot_disks_total_size(connection, snapshots, vm_id)
snapshots.each do |snapshot|
snapshot.extend(ManageIQ::Providers::Redhat::InfraManager::SnapshotDisksMixin)
total_size = snapshot.disks_total_size(connection, vm_id)
snapshot.instance_variable_set(:@total_size, total_size)
end
end
def collect_host_nics(host)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(host.nics)
end
end
def collect_dc_domains(dc)
return unless dc
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(dc.storage_domains)
end
end
def collect_vm_disks(vm)
manager.with_provider_connection(VERSION_HASH) do |connection|
disk_attachments = connection.follow_link(vm.disk_attachments)
disk_attachments.collect do |disk_attachment|
connection.follow_link(disk_attachment.disk)
end
end
end
def vm_or_template_by_path(path)
uuid = ::File.basename(path, '.*')
vm = vm_by_uuid(uuid)
vm = template_by_uuid(uuid) if vm.blank?
vm
end
def vm_by_uuid(uuid)
manager.with_provider_connection(VERSION_HASH) do |connection|
begin
connection.system_service.vms_service.vm_service(uuid).get
rescue OvirtSDK4::Error # when 404
nil
end
end
end
def template_by_uuid(uuid)
manager.with_provider_connection(VERSION_HASH) do |connection|
begin
connection.system_service.templates_service.template_service(uuid).get
rescue OvirtSDK4::Error # when 404
nil
end
end
end
end