-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathtransformation_mapping.rb
137 lines (112 loc) · 4.22 KB
/
transformation_mapping.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
class TransformationMapping < ApplicationRecord
VM_CONFLICT = "conflict".freeze
VM_EMPTY_NAME = "empty_name".freeze
VM_IN_OTHER_PLAN = "in_other_plan".freeze
VM_MIGRATED = "migrated".freeze
VM_NOT_EXIST = "not_exist".freeze
VM_VALID = "ok".freeze
has_many :transformation_mapping_items, :dependent => :destroy
has_many :service_resources, :as => :resource, :dependent => :nullify
has_many :service_templates, :through => :service_resources
validates :name, :presence => true, :uniqueness => true
def destination(source)
transformation_mapping_items.find_by(:source => source).try(:destination)
end
# vm_list: collection of hashes, each descriping a VM.
def validate_vms(vm_list = nil)
vm_list.present? ? identify_vms(vm_list) : select_vms
end
private
def select_vms
valid_list = []
transformation_mapping_items.where(:source_type => EmsCluster).collect(&:source).each do |cluster|
cluster.vms.each do |vm|
reason = validate_vm(vm, true)
valid_list << describe_vm(vm, reason) if reason == VM_VALID
end
end
{"valid_vms" => valid_list}
end
def identify_vms(vm_list)
valid_list = []
invalid_list = []
conflict_list = []
vm_list.each do |row|
vm_name = row['name']
if vm_name.blank?
invalid_list << describe_non_vm(vm_name)
next
end
query = Vm.where(:name => vm_name)
query = query.where(:uid_ems => row['uid_ems']) if row['uid_ems'].present?
query = query.joins(:host).where(:hosts => {:name => row['host']}) if row['host'].present?
query = query.joins(:ext_management_system).where(:ext_management_systems => {:name => row['provider']}) if row['provider'].present?
vms = query.to_a
if vms.size.zero?
invalid_list << describe_non_vm(vm_name)
elsif vms.size == 1
reason = validate_vm(vms.first, false)
(reason == VM_VALID ? valid_list : invalid_list) << describe_vm(vms.first, reason)
else
vms.each { |vm| conflict_list << describe_vm(vm, VM_CONFLICT) }
end
end
{
"valid_vms" => valid_list,
"invalid_vms" => invalid_list,
"conflict_vms" => conflict_list
}
end
def describe_non_vm(vm_name)
{
"name" => vm_name,
"reason" => vm_name.blank? ? VM_EMPTY_NAME : VM_NOT_EXIST
}
end
def describe_vm(vm, reason)
{
"name" => vm.name,
"cluster" => vm.ems_cluster.name,
"path" => "#{vm.ext_management_system.name}/#{vm.parent_blue_folder_path(:exclude_non_display_folders => true)}",
"allocated_size" => vm.allocated_disk_storage,
"id" => vm.id,
"reason" => reason
}
end
def validate_vm(vm, quick = true)
# a valid vm must find all resources in the mapping and has never been migrated
invalid_list = []
unless valid_cluster?(vm)
invalid_list << "cluster: %{name}" % {:name => vm.ems_cluster.name}
return no_mapping_msg(invalid_list) if quick
end
invalid_storages = unmapped_storages(vm)
if invalid_storages.present?
invalid_list << "storages: %{list}" % {:list => invalid_storages.collect(&:name).join(", ")}
return no_mapping_msg(invalid_list) if quick
end
invalid_lans = unmapped_lans(vm)
if invalid_lans.present?
invalid_list << "lans: %{list}" % {:list => invalid_lans.collect(&:name).join(', ')}
return no_mapping_msg(invalid_list) if quick
end
return no_mapping_msg(invalid_list) if invalid_list.present?
vm.validate_v2v_migration
end
def no_mapping_msg(list)
"Mapping source not found - %{list}" % {:list => list.join('. ')}
end
def valid_cluster?(vm)
transformation_mapping_items.where(:source => vm.ems_cluster).exists?
end
# return an empty array if all storages are valid for transformation
# otherwise return an array of invalid datastores
def unmapped_storages(vm)
vm.datastores - transformation_mapping_items.where(:source => vm.datastores).collect(&:source)
end
# return an empty array if all lans are valid for transformation
# otherwise return an array of invalid lans
def unmapped_lans(vm)
vm.lans - transformation_mapping_items.where(:source => vm.lans).collect(&:source)
end
end