Work-around collectd::plugin::python::module limitation

This adds a Ruby function to work-around a limitation in the
collectd::plugin::python::module defined type.

See https://github.com/voxpupuli/puppet-collectd/issues/390.

Change-Id: I76a38c8b6bfdd041ebf2a077f4a6151ea889e074
This commit is contained in:
Éric Lemoine 2016-01-13 14:26:08 +01:00
parent cfed1cc64c
commit 4ef310a2c3
9 changed files with 125 additions and 47 deletions

View File

@ -0,0 +1,59 @@
# Copyright 2016 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# This function is used by the lma_collector::collectd::python defined
# type to work-around a bug in collectd::plugin::python::module where
# the config hash cannot include values that are arrays or hashes.
# See https://github.com/voxpupuli/puppet-collectd/issues/390.
#
# Ex:
#
# ARG0:
# {"key1" => ["e1", "e2"],
# "key2" => {"k1" => "v1", "k2" => "v2"}
# "key3" => "val3"}
#
# Result:
# {"key1 \"e1\"" => "", "key1 \"e2\"" => "",
# "key2 \"k1\"" => "\"v1\"", "key2 \"k2\"" => "\"v1\"",
# "key3" => "\"val3\""}
#
module Puppet::Parser::Functions
newfunction(:adapt_collectd_python_plugin_config, :type => :rvalue) do |args|
config = args[0]
raise Puppet::ParseError, "arg[0] isn't a hash" unless config.is_a?(Hash)
adapted_config = Hash.new
config.each do |key,val|
if val.is_a?(Array)
val.each do |elt|
adapted_config["#{key} \"#{elt}\""] = ""
end
elsif val.is_a?(Hash)
val.each do |k,v|
adapted_config["#{key} \"#{k}\""] = "\"#{v}\""
end
else
adapted_config[key] = "\"#{val}\""
end
end
return adapted_config
end
end

View File

@ -26,16 +26,16 @@ class lma_collector::collectd::hypervisor (
include lma_collector::collectd::python_openstack_base
$config = {
'Username' => "\"${user}\"",
'Password' => "\"${password}\"",
'Tenant' => "\"${tenant}\"",
'KeystoneUrl' => "\"${keystone_url}\"",
'Timeout' => "\"${timeout}\"",
'CpuAllocationRatio' => "\"${cpu_allocation_ratio}\"",
'Username' => $user,
'Password' => $password,
'Tenant' => $tenant,
'KeystoneUrl' => $keystone_url,
'Timeout' => $timeout,
'CpuAllocationRatio' => $cpu_allocation_ratio,
}
if $pacemaker_master_resource {
$real_config = merge($config, {'DependsOnResource' => "\"${pacemaker_master_resource}\""})
$real_config = merge($config, {'DependsOnResource' => $pacemaker_master_resource})
} else {
$real_config = $config
}

View File

@ -38,15 +38,15 @@ define lma_collector::collectd::openstack (
}
$config = {
'Username' => "\"${user}\"",
'Password' => "\"${password}\"",
'Tenant' => "\"${tenant}\"",
'KeystoneUrl' => "\"${keystone_url}\"",
'Timeout' => "\"${real_timeout}\"",
'Username' => $user,
'Password' => $password,
'Tenant' => $tenant,
'KeystoneUrl' => $keystone_url,
'Timeout' => $real_timeout,
}
if $pacemaker_master_resource {
$real_config = merge($config, {'DependsOnResource' => "\"${pacemaker_master_resource}\""})
$real_config = merge($config, {'DependsOnResource' => $pacemaker_master_resource})
} else {
$real_config = $config
}

View File

@ -25,15 +25,15 @@ class lma_collector::collectd::openstack_checks (
include lma_collector::collectd::python_openstack_base
$config = {
'Username' => "\"${user}\"",
'Password' => "\"${password}\"",
'Tenant' => "\"${tenant}\"",
'KeystoneUrl' => "\"${keystone_url}\"",
'Timeout' => "\"${timeout}\"",
'Username' => $user,
'Password' => $password,
'Tenant' => $tenant,
'KeystoneUrl' => $keystone_url,
'Timeout' => $timeout,
}
if $pacemaker_master_resource {
$real_config = merge($config, {'DependsOnResource' => "\"${pacemaker_master_resource}\""})
$real_config = merge($config, {'DependsOnResource' => $pacemaker_master_resource})
} else {
$real_config = $config
}

View File

@ -20,10 +20,16 @@ define lma_collector::collectd::python (
validate_hash($config)
# We use the adapt_collectd_python_plugin_config function to work around
# a limitation in collectd::plugin::python::module where the config hash
# cannot include values that are arrays or hashes. See
# https://github.com/voxpupuli/puppet-collectd/issues/390.
$real_config = adapt_collectd_python_plugin_config($config)
collectd::plugin::python::module { "module_${title}":
module => $title,
modulepath => $lma_collector::collectd::python_base::modulepath,
script_source => "puppet:///modules/lma_collector/collectd/${title}.py",
config => $config,
config => $real_config,
}
}

View File

@ -25,10 +25,10 @@ describe 'lma_collector::collectd::hypervisor' do
:keystone_url => 'http://example.com/keystone'}
end
it { is_expected.to contain_lma_collector__collectd__python('hypervisor_stats') \
.with_config({"Username" => '"user"', "Password" => '"password"',
"Tenant" => '"tenant"',
"KeystoneUrl" => '"http://example.com/keystone"',
"Timeout" => '"5"', "CpuAllocationRatio" => '"16.0"'}) }
.with_config({"Username" => "user", "Password" => "password",
"Tenant" => "tenant",
"KeystoneUrl" => "http://example.com/keystone",
"Timeout" => "5", "CpuAllocationRatio" => "16.0"}) }
end
describe 'with required and optional params' do
@ -38,11 +38,11 @@ describe 'lma_collector::collectd::hypervisor' do
:timeout => 10, :cpu_allocation_ratio => 10.0,
:pacemaker_master_resource => "vip__management"}}
it { is_expected.to contain_lma_collector__collectd__python('hypervisor_stats') \
.with_config({"Username" => '"user"', "Password" => '"password"',
"Tenant" => '"tenant"',
"KeystoneUrl" => '"http://example.com/keystone"',
"Timeout" => '"10"',
"CpuAllocationRatio" => '"10.0"',
"DependsOnResource" => '"vip__management"'}) }
.with_config({"Username" => "user", "Password" => "password",
"Tenant" => "tenant",
"KeystoneUrl" => "http://example.com/keystone",
"Timeout" => "10",
"CpuAllocationRatio" => "10.0",
"DependsOnResource" => "vip__management"}) }
end
end

View File

@ -25,10 +25,10 @@ describe 'lma_collector::collectd::openstack_checks' do
:keystone_url => 'http://example.com/keystone'}
end
it { is_expected.to contain_lma_collector__collectd__python('check_openstack_api') \
.with_config({"Username" => '"user"', "Password" => '"password"',
"Tenant" => '"tenant"',
"KeystoneUrl" => '"http://example.com/keystone"',
"Timeout" => '"5"'}) }
.with_config({"Username" => "user", "Password" => "password",
"Tenant" => "tenant",
"KeystoneUrl" => "http://example.com/keystone",
"Timeout" => "5"}) }
end
describe 'with required and optional params' do
@ -37,9 +37,9 @@ describe 'lma_collector::collectd::openstack_checks' do
:keystone_url => "http://example.com/keystone",
:timeout => 10, :pacemaker_master_resource => "vip__management"}}
it { is_expected.to contain_lma_collector__collectd__python('check_openstack_api') \
.with_config({"Username" => '"user"', "Password" => '"password"',
"Tenant" => '"tenant"',
"KeystoneUrl" => '"http://example.com/keystone"',
"Timeout" => '"10"', "DependsOnResource" => '"vip__management"'}) }
.with_config({"Username" => "user", "Password" => "password",
"Tenant" => "tenant",
"KeystoneUrl" => "http://example.com/keystone",
"Timeout" => "10", "DependsOnResource" => "vip__management"}) }
end
end

View File

@ -25,10 +25,10 @@ describe 'lma_collector::collectd::openstack' do
let(:params) {{:user => "user", :password => "password", :tenant => "tenant",
:keystone_url => "http://example.com/keystone"}}
it { is_expected.to contain_lma_collector__collectd__python('openstack_nova') \
.with_config({"Username" => '"user"', "Password" => '"password"',
"Tenant" => '"tenant"',
"KeystoneUrl" => '"http://example.com/keystone"',
"Timeout" => '"5"'}) }
.with_config({"Username" => "user", "Password" => "password",
"Tenant" => "tenant",
"KeystoneUrl" => "http://example.com/keystone",
"Timeout" => "5"}) }
end
describe 'with required and optional params' do
@ -37,9 +37,9 @@ describe 'lma_collector::collectd::openstack' do
:keystone_url => "http://example.com/keystone",
:timeout => 10, :pacemaker_master_resource => "vip__management"}}
it { is_expected.to contain_lma_collector__collectd__python('openstack_nova') \
.with_config({"Username" => '"user"', "Password" => '"password"',
"Tenant" => '"tenant"',
"KeystoneUrl" => '"http://example.com/keystone"',
"Timeout" => '"10"', "DependsOnResource" => '"vip__management"'}) }
.with_config({"Username" => "user", "Password" => "password",
"Tenant" => "tenant",
"KeystoneUrl" => "http://example.com/keystone",
"Timeout" => "10", "DependsOnResource" => "vip__management"}) }
end
end

View File

@ -25,6 +25,19 @@ describe 'lma_collector::collectd::python' do
it { is_expected.to contain_collectd__plugin__python__module('module_haproxy') \
.with_module('haproxy') \
.with_modulepath('/usr/lib/collectd') \
.with_config({"Foo" => "Bar"}) }
.with_config({"Foo" => "\"Bar\""}) }
end
describe 'with complex config' do
let(:title) { :haproxy }
let(:params) do
{:config => {"key1" => ["elt0", "elt1"],
"key2" => {"k1" => "v1", "k2" => "v2"}}}
end
it { is_expected.to contain_collectd__plugin__python__module('module_haproxy') \
.with_module('haproxy') \
.with_modulepath('/usr/lib/collectd') \
.with_config({"key1 \"elt0\"" => "", "key1 \"elt1\"" => "",
"key2 \"k1\"" => "\"v1\"", "key2 \"k2\"" => "\"v2\""}) }
end
end