mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-27 10:28:41 -05:00
(QENG-3919) extract common method from both methods of VM allocation
This commit is contained in:
parent
0e66d35199
commit
67748e33b4
1 changed files with 38 additions and 58 deletions
|
|
@ -349,39 +349,11 @@ module Vmpooler
|
||||||
|
|
||||||
post "#{api_prefix}/vm/?" do
|
post "#{api_prefix}/vm/?" do
|
||||||
jdata = alias_deref(JSON.parse(request.body.read))
|
jdata = alias_deref(JSON.parse(request.body.read))
|
||||||
|
|
||||||
content_type :json
|
content_type :json
|
||||||
result = { 'ok' => false }
|
result = { 'ok' => false }
|
||||||
|
|
||||||
if jdata and !jdata.empty?
|
if jdata and !jdata.empty?
|
||||||
failed = false
|
result = atomically_allocate_vms(jdata)
|
||||||
vms = []
|
|
||||||
|
|
||||||
jdata.each do |template, count|
|
|
||||||
count.to_i.times do |_i|
|
|
||||||
vm = fetch_single_vm(template)
|
|
||||||
if !vm
|
|
||||||
failed = true
|
|
||||||
break
|
|
||||||
else
|
|
||||||
vms << [ template, vm ]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if failed
|
|
||||||
vms.each do |(template, vm)|
|
|
||||||
return_single_vm(template, vm)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
vms.each do |(template, vm)|
|
|
||||||
account_for_starting_vm(template, vm)
|
|
||||||
update_result_hosts(result, template, vm)
|
|
||||||
end
|
|
||||||
|
|
||||||
result['ok'] = true
|
|
||||||
result['domain'] = config['domain'] if config['domain']
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
status 404
|
status 404
|
||||||
end
|
end
|
||||||
|
|
@ -400,13 +372,10 @@ module Vmpooler
|
||||||
payload
|
payload
|
||||||
end
|
end
|
||||||
|
|
||||||
post "#{api_prefix}/vm/:template/?" do
|
def atomically_allocate_vms(payload)
|
||||||
payload = alias_deref(payload_from_template(params[:template]))
|
return false unless payload and !payload.empty?
|
||||||
|
|
||||||
content_type :json
|
|
||||||
result = { 'ok' => false }
|
result = { 'ok' => false }
|
||||||
|
|
||||||
if payload and !payload.empty?
|
|
||||||
failed = false
|
failed = false
|
||||||
vms = []
|
vms = []
|
||||||
|
|
||||||
|
|
@ -435,6 +404,17 @@ module Vmpooler
|
||||||
result['ok'] = true
|
result['ok'] = true
|
||||||
result['domain'] = config['domain'] if config['domain']
|
result['domain'] = config['domain'] if config['domain']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
post "#{api_prefix}/vm/:template/?" do
|
||||||
|
payload = alias_deref(payload_from_template(params[:template]))
|
||||||
|
content_type :json
|
||||||
|
result = { 'ok' => false }
|
||||||
|
|
||||||
|
if payload and !payload.empty?
|
||||||
|
result = atomically_allocate_vms(payload)
|
||||||
else
|
else
|
||||||
status 404
|
status 404
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue