Revision 7875f359
Added by Tom Caspy about 9 years ago
app/models/compute_resource.rb | ||
---|---|---|
|
||
def save_vm(uuid, attr)
|
||
vm = find_vm_by_uuid(uuid)
|
||
vm.attributes.merge!(attr.symbolize_keys)
|
||
vm.attributes.merge!(attr.deep_symbolize_keys)
|
||
vm.save
|
||
end
|
||
|
||
... | ... | |
# returns a new fog server instance
|
||
def new_vm(attr = {})
|
||
test_connection
|
||
client.servers.new vm_instance_defaults.merge(attr.to_hash.symbolize_keys) if errors.empty?
|
||
client.servers.new vm_instance_defaults.merge(attr.to_hash.deep_symbolize_keys) if errors.empty?
|
||
end
|
||
|
||
# return fog new interface ( network adapter )
|
||
... | ... | |
end
|
||
|
||
def create_vm(args = {})
|
||
options = vm_instance_defaults.merge(args.to_hash.symbolize_keys)
|
||
options = vm_instance_defaults.merge(args.to_hash.deep_symbolize_keys)
|
||
logger.debug("creating VM with the following options: #{options.inspect}")
|
||
client.servers.create options
|
||
end
|
||
... | ... | |
def nested_attributes_for(type, opts)
|
||
return [] unless opts
|
||
opts = opts.dup #duplicate to prevent changing the origin opts.
|
||
opts.delete("new_#{type}") # delete template
|
||
opts.delete("new_#{type}") || opts.delete("new_#{type}".to_sym) # delete template
|
||
# convert our options hash into a sorted array (e.g. to preserve nic / disks order)
|
||
opts = opts.sort { |l, r| l[0].sub('new_','').to_i <=> r[0].sub('new_','').to_i }.map { |e| Hash[e[1]] }
|
||
opts = opts.sort { |l, r| l[0].to_s.sub('new_','').to_i <=> r[0].to_s.sub('new_','').to_i }.map { |e| Hash[e[1]] }
|
||
opts.map do |v|
|
||
if v[:"_delete"] == '1' && v[:id].blank?
|
||
nil
|
||
else
|
||
v.symbolize_keys # convert to symbols deeper hashes
|
||
v.deep_symbolize_keys # convert to symbols deeper hashes
|
||
end
|
||
end.compact
|
||
end
|
app/models/compute_resources/foreman/model/ec2.rb | ||
---|---|---|
end
|
||
|
||
def create_vm(args = { })
|
||
args = vm_instance_defaults.merge(args.to_hash.symbolize_keys)
|
||
args = vm_instance_defaults.merge(args.to_hash.symbolize_keys).deep_symbolize_keys
|
||
if (name = args[:name])
|
||
args.merge!(:tags => {:Name => name})
|
||
end
|
app/models/compute_resources/foreman/model/libvirt.rb | ||
---|---|---|
def new_vm(attr = { })
|
||
test_connection
|
||
return unless errors.empty?
|
||
opts = vm_instance_defaults.merge(attr.to_hash).symbolize_keys
|
||
opts = vm_instance_defaults.merge(attr.to_hash).deep_symbolize_keys
|
||
|
||
# convert rails nested_attributes into a plain hash
|
||
[:nics, :volumes].each do |collection|
|
app/models/compute_resources/foreman/model/ovirt.rb | ||
---|---|---|
|
||
def save_vm(uuid, attr)
|
||
vm = find_vm_by_uuid(uuid)
|
||
vm.attributes.merge!(attr.symbolize_keys)
|
||
vm.attributes.merge!(attr.symbolize_keys).deep_symbolize_keys
|
||
update_interfaces(vm, attr[:interfaces_attributes])
|
||
update_volumes(vm, attr[:volumes_attributes])
|
||
vm.interfaces
|
app/models/compute_resources/foreman/model/vmware.rb | ||
---|---|---|
end
|
||
|
||
def parse_args(args)
|
||
args = args.symbolize_keys
|
||
args = args.deep_symbolize_keys
|
||
|
||
# convert rails nested_attributes into a plain, symbolized hash
|
||
[:interfaces, :volumes].each do |collection|
|
||
... | ... | |
|
||
def new_vm(args = {})
|
||
args = parse_args args
|
||
opts = vm_instance_defaults.symbolize_keys.merge(args.symbolize_keys)
|
||
opts = vm_instance_defaults.symbolize_keys.merge(args.symbolize_keys).deep_symbolize_keys
|
||
client.servers.new opts
|
||
end
|
||
|
app/models/lookup_value.rb | ||
---|---|---|
scoped_search :on => :match, :complete_value => true
|
||
scoped_search :in => :lookup_key, :on => :key, :rename => :lookup_key, :complete_value => true
|
||
|
||
def value=(val)
|
||
if val.is_a?(HashWithIndifferentAccess)
|
||
super(val.deep_to_hash)
|
||
else
|
||
super
|
||
end
|
||
end
|
||
|
||
def name
|
||
match
|
||
end
|
config/initializers/active_support_extensions.rb | ||
---|---|---|
#this makes sure that hash with indifferent access gets deep converted into hash.
|
||
#code borrowed from 4.2 ActiveSupport
|
||
module ActiveSupport
|
||
class HashWithIndifferentAccess < Hash
|
||
def deep_to_hash
|
||
new_hash = Hash.new(default)
|
||
each do |key, value|
|
||
new_hash[key] = deep_convert_value(value, :for => :to_hash)
|
||
end
|
||
new_hash
|
||
end
|
||
|
||
def deep_convert_value(value, options = {})
|
||
if value.is_a? HashWithIndifferentAccess
|
||
if options[:for] == :to_hash
|
||
value.deep_to_hash
|
||
else
|
||
value.nested_under_indifferent_access
|
||
end
|
||
elsif value.is_a?(Array)
|
||
unless options[:for] == :assignment
|
||
value = value.dup
|
||
end
|
||
value.map! { |e| deep_convert_value(e, options) }
|
||
else
|
||
value
|
||
end
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #9240 - in case of hash with indifferent access, serialize as hash
(cherry picked from commit 3059cea1c207718217f07b9cdc706bc516f5db1b)