Skip to content

Commit

Permalink
Identify a VM by references
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Mar 16, 2018
1 parent 2adc8e1 commit e54935a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def self.validate_config_info(options)

vms = if config_info[:vm_ids]
VmOrTemplate.find(config_info[:vm_ids])
elsif config_info[:vm_refs]
validate_and_find_by_refs(mapping, config_info[:vm_refs])
else
config_info[:vms]
end
Expand All @@ -88,4 +90,25 @@ def self.validate_config_info(options)
}
end
private_class_method :validate_config_info

# refs_config is an array of hash
# each hash can have keys: name, host, uid_ems
def self.validate_and_find_by_refs(_mapping, refs_array)
refs_array.collect {|hash| vm_by_ref(hash) }
end
private_class_method :validate_and_find_by_refs

def self.vm_by_ref(ref_hash)
vm_name = ref_hash['name']
host = Host.find_by(:name => ref_hash['host'])
raise _("Can't find host for VM(#{vm_name})") unless host

conditions = {:name => vm_name}
conditions[:uid_ems] = ref_hash['uid_ems'] if ref_hash['uid_ems'].present?
vms = host.vms.where(conditions)
raise _("VM(#{vm_name} does not exist in inventory)") if vms.empty?
raise _("Multiple VMs with name(#{vm_name}) in inventory") if vms.size > 1
vms.first
end
private_class_method :vm_by_ref
end

0 comments on commit e54935a

Please sign in to comment.