From 402b98284242713fbb7b0173da8aa6eff87fa595 Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 12 Aug 2015 08:48:24 +0200 Subject: Fix #14 - allow trocla_get not to raise an error if nothing is found Up to now we raised an error if nothing was found while using trocla_get. The main idea was to ensure that typos in the key/format are easily spotted and not overlooked as no password being returned usually indicates that something is wrong. As outlined in #14 there are use cases where it makes sense to not have this behavior. This change allows us to suppress the error raising and just return the puppet undef if nothing is found. --- lib/puppet/parser/functions/trocla_get.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/puppet/parser/functions/trocla_get.rb b/lib/puppet/parser/functions/trocla_get.rb index 01c4e01..fb5cd5a 100644 --- a/lib/puppet/parser/functions/trocla_get.rb +++ b/lib/puppet/parser/functions/trocla_get.rb @@ -4,7 +4,7 @@ module Puppet::Parser::Functions Usage: - $password_user1 = trocla_get(key,[format='plain']) + $password_user1 = trocla_get(key,[format='plain'[,raise_error=true]]) Means: @@ -16,13 +16,23 @@ Get the plain text password for the key 'user1' Get the mysql style sha1 hashed password. -It will raise a parse error if the password haven't yet been stored in trocla. +By default puppet will raise a parse error if the password haven't yet been +stored in trocla. This can be turned off by setting false as a third argument: + + $password_user3 = trocla_get('user2','mysql',false) + +the return value will be undef if the key & format pair is not found. " ) do |*args| + if args[0].is_a?(Array) + args = args[0] + end require File.dirname(__FILE__) + '/../../util/trocla_helper' - if (answer=Puppet::Util::TroclaHelper.trocla(:get_password,false,*args)).nil? - raise(Puppet::ParseError, "No password for key,format #{args.flatten.inspect} found!") + args[1] ||= 'plain' + raise_error = args[2].nil? ? true : args[2] + if (answer=Puppet::Util::TroclaHelper.trocla(:get_password,false,[args[0],args[1]])).nil? && raise_error + raise(Puppet::ParseError, "No password for key,format #{args[0..1].flatten.inspect} found!") end - answer + answer.nil? ? :undef : answer end end -- cgit v1.2.3