Checking if variable is a Number in Puppet DSL - puppet

How to check that a variable is a Number in Puppet DSL ?
thanks

Use the is_numeric function from the stdlib: https://github.com/puppetlabs/puppetlabs-stdlib

Related

template yaml for backstage - is there a built-in variable that can generate set of numbers similar to uuid?

This is very specific to Backstage. Is there some built-in variable such as $RANDOM or uuid generator that can work with fetch:template?
Thank you
I had used ${{range(1000)|random}} and that works

Call groovy script dynamically in Apache Camel using doTry-doCatch

I'm building a route which calls a groovy script whose path is dynamically computed and, if the script can't be found, defaults to a generic, static script:
.doTry()
.toD("language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
The problem is that the exchange property consumerType is not resolved since the uri string parameter of toD is evaluated using groovy and not simple.
MultipleCompilationErrorsException -> startup failed:
Script_09b4150584d9e2c979353feee06897b5.groovy: 1: Unexpected input: 'scripts/${exchangeProperty.consumerType}' # line 1, column 20.
resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy
^
1 error
How can I obtain the desired behavior?
According to the error shown there, it seems Camel is not able to resolve the string you provided in the toD().
By default, the expression you pass to a dynamic to is evaluated as Simple language but, as described in To Dynamic Camel documentation, you can specify other languages for the dynamic evaluation.
In your case, you are trying to evaluate the endpoint with groovy language but then you're using Simple language to substitute a piece of the name of the script.
One solution I've found (yet not the best) would be to specify the language for the interpretation of the string as simple and then use language:groovy to specify the endpoint that will need to be called.
You could write something like this:
.doTry()
.toD("language:simple:language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
It seems to work, but I hope someone else comes up with a better solution.

Map Hiera value to another value

How can I achieve something like this in Hiera?
service::enabled: true
plugin:
sensu:
ensure: (if service::enabled: 'present' else 'absent')
I know I can do this with puppet but would like to avoid that.
If you really, really (see below why you don't want to do this) want to have conditional logic in your data, then you could use my tahu::ppyaml() hiera backend which allows you to have puppet logic embedded in the yaml data, or write your own specific backend function. The tahu module requires Puppet 6. For versions of puppet before Hiera 5 you need to write a hiera 3 backend to achieve something similar. With Hiera 5 a backend function is very simple and can even be written in the puppet language.
You can find the tahu::ppayaml function here:
https://github.com/hlindberg/tahu/blob/master/lib/puppet/functions/tahu/ppyaml_key.rb
Your data would then look like this:
service::enabled: true
plugin:
sensu:
ensure: "if $service::enabled { 'present'} else {'absent'}"
Since the ppyaml backend treats every string as puppet language you need to quote all literal strings in the data file read by ppyaml; for example "'foo'" or '"foo"'.
You can however break out the key with a conditional into a separate file and use and alias in your main data file. Like this:
service::enabled: true
plugin:
sensu:
ensure: '%{alias("sensu::ensure")}'
And then, either using the tahu::ppyaml to bind only dynamic keys:
sensu::ensure: if $service::enabled { 'present'} else {'absent'}"
and adding that to your hiera.yaml referencing tahu::ppyaml as a backend.
It would work the same way if you write your own backend.
If any of this is recommended is a different question as it is questionable to have conditional logic in the data that depends on a variable being set or not in a manifest as you will get one value if you do the lookup before the inclusion of sensu and a different value after - and you are probably looking up the hash for the very purpose of declaring sensu.
Unfortunately, Hiera doesn't have expressions that can do anything as sophisticated as conditional logic.
There are some aliasing and lookup functions, so you may be able to pass the value service::enabled through unmodified, but that is it. The functions in Hiera are documented at https://puppet.com/docs/puppet/latest/hiera_merging.html.

Is it possible to add the values of two variables using Morphline's inbuilt set of commands?

I'm wondering if there is any way to add the values of two variables in morphlines, without having to write a custom command.
For example, something like:
addValues {
answer : "#{value_one}" + 50
}
Any help is appreciated, thanks
I did not find out how to (or whether it was possible to) do this using Morphline's inbuilt commands. however, it is possible to do so within the java{} command, which allows you to write plain java code in-line in the Morphlines config.

Format the stirng value

I have the following value in a variable.
9.2.345.113619.2-218.98721247546.30582.1191562771895.2
I want to change the format of this value to 9-2-345-113619-2-218-98721247546-30582-1191562771895-2 and store it in another variable. Is there any function in JMeter that can do this?
Suppose your value is stored in Jmeter_variable
Then place one bean-shell sampler and write below mentioned code
value=vars.get("Jmeter_variable").replaceAll("\\.","-");
vars.put("Updated_variable",value);
Now use ${updated_variable}, where you want to use
Inline, using __javaScript() function
${__javaScript("9.2.345.113619.2-218.98721247546.30582.1191562771895.2".split(".").join("-");,)}
Against variable using __Beanshell function
${__BeanShell(vars.put("newStudyID"\, vars.get("").replaceAll("\\\\."\,"-")),)}
In any Beanshell Test Element:
vars.put("newStudyID", vars.get("").replaceAll("\\\\.","-"));
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using scripting in your JMeter test.

Resources