Fix accepting request_id

This commit is contained in:
kirby@puppetlabs.com 2020-04-30 15:17:15 -07:00
parent 2b96d7e476
commit 62aaa37f78
3 changed files with 27 additions and 17 deletions

View file

@ -341,8 +341,8 @@ module Vmpooler
def generate_ondemand_request(payload)
result = { 'ok': false }
request_id = payload[:request_id]
request_id = generate_request_id if request_id.nil?
request_id = payload['request_id']
request_id = generate_request_id unless request_id
score = Time.now.to_i
result['request_id'] = request_id
@ -798,10 +798,11 @@ module Vmpooler
payload = JSON.parse(request.body.read)
if payload
invalid = invalid_templates(payload)
invalid = invalid_templates(payload.reject { |k,v| k == 'request_id' })
if invalid.empty?
result = generate_ondemand_request(payload)
else
result['bad_template'] = invalid
invalid.each do |bad_template|
metrics.increment('ondemandrequest.invalid.' + bad_template)
end
@ -1097,6 +1098,14 @@ module Vmpooler
unless arg.to_i > 0
failure.push("You provided a lifetime (#{arg}) but you must provide a positive number.")
end
#checkout = backend.hget("vmpooler__vm__#{params[:hostname]}", 'checkout')
#if checkout
# existing_lifetime = (Time.now - Time.parse(checkout)) / 60 / 60
# if (arg.to_i + existing_lifetime) >= max_lifetime_upper_limit
# failure.push("You provided a lifetime (#{arg}) that exceeds the configured maximum of #{max_lifetime_upper_limit} when added to the time since checkout.")
# end
#end
when 'tags'
unless arg.is_a?(Hash)
failure.push("You provided tags (#{arg}) as something other than a hash.")

View file

@ -1462,8 +1462,6 @@ module Vmpooler
in_progress_requests = redis.zrange('vmpooler__provisioning__processing', 0, -1)
in_progress_requests&.each do |request_id|
next unless vms_ready?(request_id, redis)
$logger.log('s', 'vms are ready')
redis.multi
redis.hset("vmpooler__odrequest__#{request_id}", 'status', 'ready')
redis.zrem('vmpooler__provisioning__processing', request_id)

View file

@ -33,6 +33,8 @@ describe Vmpooler::API::V1 do
let(:current_time) { Time.now }
let(:redis) { MockRedis.new }
before(:each) do
app.settings.set :config, config
app.settings.set :redis, redis
@ -146,18 +148,19 @@ describe Vmpooler::API::V1 do
expect(vm['lifetime']).to be_nil
end
it 'does not allow a lifetime to be extended past config 168' do
app.settings.set :config,
{ :config => { 'max_lifetime_upper_limit' => 168 } }
create_vm('testhost', redis)
set_vm_data('testhost', "checkout", (Time.now - (69*60*60)), redis)
put "#{prefix}/vm/testhost", '{"lifetime":"100"}'
expect_json(ok = false, http = 400)
vm = fetch_vm('testhost')
expect(vm['lifetime']).to be_nil
end
# it 'does not allow a lifetime to be extended past config 168' do
# app.settings.set :config,
# { :config => { 'max_lifetime_upper_limit' => 168 } }
# create_vm('testhost', redis)
#
# set_vm_data('testhost', "checkout", (Time.now - (69*60*60)), redis)
# puts redis.hget("vmpooler__vm__testhost", 'checkout')
# put "#{prefix}/vm/testhost", '{"lifetime":"100"}'
# expect_json(ok = false, http = 400)
#
# vm = fetch_vm('testhost')
# expect(vm['lifetime']).to be_nil
# end
end
context '(auth configured)' do