mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 10:08:40 -05:00
Merge pull request #110 from sschneid/tagging_restriction
(QENG-2388) Tagging restrictions
This commit is contained in:
commit
9e092fd148
4 changed files with 58 additions and 9 deletions
|
|
@ -79,6 +79,26 @@ module Vmpooler
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def export_tags(backend, hostname, tags)
|
||||||
|
tags.each_pair do |tag, value|
|
||||||
|
next if value.nil? or value.empty?
|
||||||
|
|
||||||
|
backend.hset('vmpooler__vm__' + hostname, 'tag:' + tag, value)
|
||||||
|
backend.hset('vmpooler__tag__' + Date.today.to_s, hostname + ':' + tag, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_tags(tags)
|
||||||
|
return unless Vmpooler::API.settings.config[:tagfilter]
|
||||||
|
|
||||||
|
tags.each_pair do |tag, value|
|
||||||
|
next unless filter = Vmpooler::API.settings.config[:tagfilter][tag]
|
||||||
|
tags[tag] = value.match(filter).captures.join if value.match(filter)
|
||||||
|
end
|
||||||
|
|
||||||
|
tags
|
||||||
|
end
|
||||||
|
|
||||||
def mean(list)
|
def mean(list)
|
||||||
s = list.map(&:to_f).reduce(:+).to_f
|
s = list.map(&:to_f).reduce(:+).to_f
|
||||||
(s > 0 && list.length > 0) ? s / list.length.to_f : 0
|
(s > 0 && list.length > 0) ? s / list.length.to_f : 0
|
||||||
|
|
|
||||||
|
|
@ -476,6 +476,10 @@ module Vmpooler
|
||||||
unless arg.is_a?(Hash)
|
unless arg.is_a?(Hash)
|
||||||
failure = true
|
failure = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if config['allowed_tags']
|
||||||
|
failure = true if not (arg.keys - config['allowed_tags']).empty?
|
||||||
|
end
|
||||||
else
|
else
|
||||||
failure = true
|
failure = true
|
||||||
end
|
end
|
||||||
|
|
@ -493,15 +497,8 @@ module Vmpooler
|
||||||
|
|
||||||
backend.hset('vmpooler__vm__' + params[:hostname], param, arg)
|
backend.hset('vmpooler__vm__' + params[:hostname], param, arg)
|
||||||
when 'tags'
|
when 'tags'
|
||||||
arg.keys.each do |tag|
|
filter_tags(arg)
|
||||||
if Vmpooler::API.settings.config[:tagfilter] and Vmpooler::API.settings.config[:tagfilter][tag]
|
export_tags(backend, params[:hostname], arg)
|
||||||
filter = Vmpooler::API.settings.config[:tagfilter][tag]
|
|
||||||
arg[tag] = arg[tag].match(filter).captures.join if arg[tag].match(filter)
|
|
||||||
end
|
|
||||||
|
|
||||||
backend.hset('vmpooler__vm__' + params[:hostname], 'tag:' + tag, arg[tag])
|
|
||||||
backend.hset('vmpooler__tag__' + Date.today.to_s, params[:hostname] + ':' + tag, arg[tag])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -323,6 +323,15 @@ describe Vmpooler::API::V1 do
|
||||||
expect(last_response.status).to eq(200)
|
expect(last_response.status).to eq(200)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'skips empty tags' do
|
||||||
|
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":""}}'
|
||||||
|
|
||||||
|
expect(last_response).to be_ok
|
||||||
|
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||||
|
expect(last_response.body).to eq(JSON.pretty_generate({'ok' => true}))
|
||||||
|
expect(last_response.status).to eq(200)
|
||||||
|
end
|
||||||
|
|
||||||
it 'does not set tags if request body format is invalid' do
|
it 'does not set tags if request body format is invalid' do
|
||||||
put "#{prefix}/vm/testhost", '{"tags":{"tested"}}'
|
put "#{prefix}/vm/testhost", '{"tags":{"tested"}}'
|
||||||
|
|
||||||
|
|
@ -332,6 +341,23 @@ describe Vmpooler::API::V1 do
|
||||||
expect(last_response.status).to eq(400)
|
expect(last_response.status).to eq(400)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '(allowed_tags configured)' do
|
||||||
|
let(:config) { {
|
||||||
|
config: {
|
||||||
|
'allowed_tags' => ['created_by', 'project', 'url']
|
||||||
|
}
|
||||||
|
} }
|
||||||
|
|
||||||
|
it 'fails if specified tag is not in allowed_tags array' do
|
||||||
|
put "#{prefix}/vm/testhost", '{"tags":{"created_by":"rspec","tested_by":"rspec"}}'
|
||||||
|
|
||||||
|
expect(last_response).to_not be_ok
|
||||||
|
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||||
|
expect(last_response.body).to eq(JSON.pretty_generate({'ok' => false}))
|
||||||
|
expect(last_response.status).to eq(400)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '(tagfilter configured)' do
|
context '(tagfilter configured)' do
|
||||||
let(:config) { {
|
let(:config) { {
|
||||||
tagfilter: { 'url' => '(.*)\/' },
|
tagfilter: { 'url' => '(.*)\/' },
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,9 @@
|
||||||
# Same as vm_lifetime, but applied if a valid authentication token is
|
# Same as vm_lifetime, but applied if a valid authentication token is
|
||||||
# included during the request.
|
# included during the request.
|
||||||
#
|
#
|
||||||
|
# - allowed_tags
|
||||||
|
# If set, restricts tags to those specified in this array.
|
||||||
|
#
|
||||||
# - domain
|
# - domain
|
||||||
# If set, returns a top-level 'domain' JSON key in POST requests
|
# If set, returns a top-level 'domain' JSON key in POST requests
|
||||||
|
|
||||||
|
|
@ -169,6 +172,9 @@
|
||||||
vm_checktime: 15
|
vm_checktime: 15
|
||||||
vm_lifetime: 12
|
vm_lifetime: 12
|
||||||
vm_lifetime_auth: 24
|
vm_lifetime_auth: 24
|
||||||
|
allowed_tags:
|
||||||
|
- 'created_by'
|
||||||
|
- 'project'
|
||||||
domain: 'company.com'
|
domain: 'company.com'
|
||||||
|
|
||||||
# :pools:
|
# :pools:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue