From 8d71bf588b90c029fa44f7901d1fdf8a8209f7d2 Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Fri, 15 Aug 2014 16:37:52 +0100 Subject: [PATCH] Add custom parameter to subscriptions --- .../sensu_client_subscription/json.rb | 25 ++++++++++++- lib/puppet/type/sensu_client_subscription.rb | 34 ++++++++++++++++++ manifests/subscription.pp | 6 ++++ spec/defines/sensu_subscription_spec.rb | 35 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/sensu_client_subscription/json.rb b/lib/puppet/provider/sensu_client_subscription/json.rb index 69a7e1002e..22c88f0b5d 100644 --- a/lib/puppet/provider/sensu_client_subscription/json.rb +++ b/lib/puppet/provider/sensu_client_subscription/json.rb @@ -1,8 +1,16 @@ require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3' require 'json' if Puppet.features.json? +begin + require 'puppet_x/sensu/to_type' +rescue LoadError => e + libdir = Pathname.new(__FILE__).parent.parent.parent.parent + require File.join(libdir, 'puppet_x/sensu/to_type') +end + Puppet::Type.type(:sensu_client_subscription).provide(:json) do confine :feature => :json + include Puppet_X::Sensu::Totype def conf begin @@ -23,13 +31,19 @@ def config_file end def create - conf['client'] = {'subscriptions' => [ resource[:name] ] } + conf['client'] = {} + self.subscriptions = [ resource[:name] ] + self.custom = resource[:custom] unless resource[:custom].nil? end def destroy @conf = nil end + def check_args + ['name', 'address', 'subscriptions', 'safe_mode', 'bind'] + end + def subscriptions conf['client']['subscriptions'] end @@ -38,6 +52,15 @@ def subscriptions=(value) conf['client']['subscriptions'] = value end + def custom + conf['client'].reject { |k,v| check_args.include?(k) } + end + + def custom=(value) + conf['client'].delete_if { |k,v| not check_args.include?(k) } + conf['client'].merge!(to_type(value)) + end + def exists? conf.has_key?('client') && conf['client'].has_key?('subscriptions') end diff --git a/lib/puppet/type/sensu_client_subscription.rb b/lib/puppet/type/sensu_client_subscription.rb index e21fb1ac42..1a982c226d 100644 --- a/lib/puppet/type/sensu_client_subscription.rb +++ b/lib/puppet/type/sensu_client_subscription.rb @@ -1,3 +1,9 @@ +begin + require 'puppet_x/sensu/to_type' +rescue LoadError => e + libdir = Pathname.new(__FILE__).parent.parent.parent + require File.join(libdir, 'puppet_x/sensu/to_type') +end Puppet::Type.newtype(:sensu_client_subscription) do @doc = "" @@ -38,6 +44,34 @@ def initialize(*args) end end + newproperty(:custom) do + desc "Custom client variables" + + include Puppet_X::Sensu::Totype + + def is_to_s(hash = @is) + hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ") + end + + def should_to_s(hash = @should) + hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ") + end + + def insync?(is) + if defined? @should[0] + if is == @should[0].each { |k, v| value[k] = to_type(v) } + true + else + false + end + else + true + end + end + + defaultto {} + end + autorequire(:package) do ['sensu'] end diff --git a/manifests/subscription.pp b/manifests/subscription.pp index 62c7fd03a2..318039b09f 100644 --- a/manifests/subscription.pp +++ b/manifests/subscription.pp @@ -8,9 +8,14 @@ # String. Whether the check should be present or not # Default: present # Valid values: present, absent + +# [*custom*] +# Hash. Custom client variables +# Default: {} # define sensu::subscription ( $ensure = 'present', + $custom = {}, ) { validate_re($ensure, ['^present$', '^absent$'] ) @@ -25,6 +30,7 @@ sensu_client_subscription { $name: ensure => $ensure, + custom => $custom, notify => Class['sensu::client::service'], } diff --git a/spec/defines/sensu_subscription_spec.rb b/spec/defines/sensu_subscription_spec.rb index e69de29bb2..84bc4557de 100644 --- a/spec/defines/sensu_subscription_spec.rb +++ b/spec/defines/sensu_subscription_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'sensu::subscription', :type => :define do + context 'without whitespace in name' do + let(:title) { 'mysubscription' } + + context 'defaults' do + it { should contain_sensu_client_subscription('mysubscription') } + end + + context 'setting params' do + let(:params) { { + :custom => { 'a' => 'b', 'array' => [ 'c', 'd' ] }, + } } + + it { should contain_sensu_client_subscription('mysubscription').with( + :custom => { 'a' => 'b', 'array' => [ 'c', 'd' ] } + ) } + end + + context 'ensure absent' do + let(:params) { { + :ensure => 'absent', + } } + + it { should contain_sensu_client_subscription('mysubscription').with_ensure('absent') } + end + end + + context 'notifications' do + let(:title) { 'mysubscription' } + + it { should contain_sensu_client_subscription('mysubscription').with(:notify => ['Class[Sensu::Client::Service]' ]) } + end +end