Groovy - How to simplify embedded function arguments - groovy

I am running groovy in jenkins and I want to split the data from the functionality. I've tried to create a map with variable names and values but it looks like functions inside functions screws that up. I want to pull out CurrentBuildNo, ProjectName, Results_Folder (they are different for each parallel run) without duplicating code.
autotests = parallel (
{ ignore(ABORTED) {
retry ( 2 ) {
build("AutoTest", CurrentBuildNo: CurrentBuildNo, ProjectName: params["ProjectName"], Results_Folder: Results_Folder)
} } },
{ ignore(ABORTED) {
retry ( 2 ) {
build("AutoTest", CurrentBuildNo: CurrentBuildNo, ProjectName: params["ProjectName"], Results_Folder: Results_Folder)
} } }
)
The logic I want is something like:
tests = {{CurrentBuildNo: CurrentBuildNo, ...},{CurrentBuildNo: CurrentBuildNo, ...}}
autotests = parallel (
for (i in tests){
ignore(ABORTED) {
retry ( 2 ) {
build("AutoTest", test[i]['CurrentBuildNo'], test[i]['ProjectName']...)
} } }
}
)

Ahhh, I think you'll want something like this:
def tests = [ [ CurrentBuildNo: CurrentBuildNo, ... ],
[ CurrentBuildNo: CurrentBuildNo, ... ] ]
parallel tests.collect { t ->
{
ignore( ABORTED ) {
retry( 2 ) {
build( 'AutoTest', t.CurrentBuildNo, ... )
}
}
}
}

Related

How to set multipe values in CascadeChoiceParameter's for referencedParameters

Trying to set multiple values in CascadeChoiceParameter's for referencedParameters. What the format should be? Docs documentation said that it should be 'string', but in case of setting referencedParameters: 'param1,param2' it goes to fallback script.
here is the class:
[$class: 'CascadeChoiceParameter',
name: 'SOME_PARAM',
description: 'some description',
randomName: '',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["item_1"]'
],
script: [
classpath: [],
sandbox: true,
script: """
if(PARAM_2.equals("some_value") && PARAM_3.equals("some_value")) {
return ["item_1", "item_2", "item_3"]
} else if((PARAM_2.equals("E2E_Tests") || (PARAM_2.equals("Real_API")) && PARAM_3.equals("knox_guard")) {
return ["item_1", "item_2", "item_4"]
} else {
return ["item_1"]
}
""".stripIndent()
]
],
choiceType: 'PT_SINGLE_SELECT',
referencedParameters: 'PARAM_2,PARAM_3',
filterable: false,
filterLength: 1
],
The fallback script is used if/when there is any kind of exception/error when attempting to execute the main script. In your case, there is a compilation error in the else if((PARAM_2.equals("E2E_Tests") || (PARAM_2.equals("Real_API")) && PARAM_3.equals("knox_guard")) line. All the open parenthesis haven't been closed - there are 6 open parenthesis and only 5 close parenthesis. Anyway, I haven't tested a CascadeChoiceParameter version of the pipeline. I have tested using activeChoiceReactiveParam. Below is a working job DSL:
String choicesScript = """
if(PARAM_2.equals("some_value") && PARAM_3.equals("some_value")) {
return ["item_1", "item_2", "item_3"]
} else if((PARAM_2.equals("E2E_Tests") || (PARAM_2.equals("Real_API")) && PARAM_3.equals("knox_guard"))) {
return ["item_1", "item_2", "item_4"]
} else {
return ["item_5"]
}
""".stripIndent()
String pipeline = '''
pipeline {
agent any
stages {
stage('Show parameter values') {
steps {
echo "PARAM_2: ${params.PARAM_2}, PARAM_3: ${params.PARAM_3}, SOME_PARAM: ${params.SOME_PARAM}"
}
}
}
}
'''.stripIndent()
pipelineJob('reactive-params') {
parameters {
activeChoiceParam('PARAM_2') {
description('First test parameter')
choiceType('SINGLE_SELECT')
groovyScript {
script('return ["some_value", "E2E_Tests", "Real_API"]')
}
}
activeChoiceParam('PARAM_3') {
description('Second test parameter')
choiceType('SINGLE_SELECT')
groovyScript {
script('return ["some_value", "knox_guard"]')
}
}
activeChoiceReactiveParam('SOME_PARAM') {
description('some description')
choiceType('SINGLE_SELECT')
referencedParameter('PARAM_2')
referencedParameter('PARAM_3')
groovyScript {
script(choicesScript)
fallbackScript('return ["item_1"]')
}
}
}
definition {
cps {
script(pipeline)
}
}
}

Extract a list of profileId's using jsonpath

I have the following JSON object r2,
[
{
"reserva_id":"200",
"estancias":[
{
"reserva_estancia":"266",
"huespedes":[
{
"reserva_huesped":"272",
"reserva_estancia":"266",
"numero_huesped":"1",
"huesped":"123",
"huesped_nombre":"dos dos, dos",
"rfid":null
},
{
"reserva_huesped":"276",
"reserva_estancia":"266",
"numero_huesped":"2",
"huesped":"183",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
}
]
}
]
},
{
"reserva_id":"201",
"estancias":[
{
"huespedes":[
{
"reserva_huesped":"273",
"reserva_estancia":"267",
"numero_huesped":"1",
"huesped":"148",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
},
{
"reserva_huesped":"277",
"reserva_estancia":"267",
"numero_huesped":"2",
"huesped":"187",
"huesped_nombre":"TEST TEST, TESTCIVITFUN",
"rfid":null
}
]
}
]
}
]
I am trying to get the first huesped for each reservation, and for that I am using the following script, to create a list called profiles and store profileId's:
def profiles = jsonpath(r2,'$..[:].estancias[:].huespedes[0].huesped')
The output should be the following:
[
"123",
"148"
]
However, when I print profiles.text I get all the content of the estancias object, instead of just the huesped number.
When using Jayway's JSONPath like this I get the desired oputput:
$..[*].estancias[*].huespedes[0].huesped
You can try the path expression with your JSON here online.

refactor ensure_packages to switch version of installed packages

I am successfully installing several PHP modules by version with puppet on Debian linux like this:
$php_version = '7.3'
ensure_packages([
"php$php_version-xml",
"php$php_version-zip",
"php$php_version-curl",
"php$php_version-mbstring",
"libapache2-mod-php$php_version",
],
{
'ensure' => 'present',
}
)
now I want to prepare for an update from PHP 7.3 to 7.4. This basically works, but the 7.3 packages stay installed. I would like to adapt the code to remove the old packages. I am looking for a way to reuse the list of packages of modules for uninstalling.
I am thinking of a signature like this
class profile::software::apache (
$php_version = '7.4',
$php_remove = ['7.0‘, ‘7.3'],
#...
) {
$myPackages = [
"php$php_version-xml",
"php$php_version-zip",
"php$php_version-curl",
"php$php_version-mbstring",
"libapache2-mod-php$php_version",
]
ensure_packages($myPackages,
{
'ensure' => 'present',
}
)
$php_remove.each | String $php_version | {
ensure_packages($myPackages,
{
'ensure' => 'absent',
}
)
}
}
Is there a way to solve this?
thx
I was able to solve this by using iteration functions of puppet.
From the two parameters I build a hash with keys of versions to work on and values to either install or remove the given version.
Now I can iterate over this hash with each, reusing the structure:
class profile::software::apache (
$php_version = '7.4',
$php_remove = ['7.0‘, ‘7.3'],
#...
) {
# build a hash of PHP Versions with a value of either present or absent
# and iterate over it with each
$phpInstallHash = { $php_version => 'present' }
#notify { "Value of phpRemove: ${php_remove}": }
if $php_remove {
# We have the array, use the map function to build remove hash
$phpRemoveHash = $php_remove.map |$version| {
{ $version => 'absent' }
}
$phpFullHash = $phpInstallHash + $phpRemoveHash
} else {
$phpFullHash = $phpInstallHash
}
#notify { "phpHash to iterate over to install/uninstall: ${phpFullHash}": }
#iterate over the result installing/uninstalling
$phpFullHash.each | $php_version, $ensure_value | {
ensure_packages([
"php$php_version-xml",
"php$php_version-zip",
"php$php_version-curl",
"php$php_version-mbstring",
"libapache2-mod-php$php_version",
],
{
'ensure' => $ensure_value,
require => [Class['apt::update'],
Apt::Source['source_php_sury'],
],
notify => Class['apache::service'],
}
)
}
}
hth

how can i retrieve all the childrens by n1ql query

I am planning to retrieve the child elements all the parent, How can i retrieve it, being inside the array of array objects.
SELECT sd.*
FROM test AS t
UNNEST stateDetails AS sd;
{
"type":"countries",
"docName":"CountryData",
"countryDetails":[
{
"name":"US",
"code":"+1",
"stateInfo":[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
},
{
"name":"France",
"code":"+33",
"stateInfo":[
{
"name":"Grand Est",
"id":"5212"
},
{
"name":"Brittany",
"id":"5214"
}
]
}
]
}
I am expecting the following output to bring out the state details of all the countries with their respective country Name
[
{
"countryName":"US",
"name":"Florida",
"id":"1212"
},
{
"countryName":"US",
"name":"NewYork",
"id":"1214"
},
{
"countryName":"France",
"name":"Grand Est",
"id":"5212"
},
{
"countryName":"France",
"name":"Brittany",
"id":"5214"
}
]
Use double UNNEST and project what you need
SELECT cd.name AS countryName, sd.name, sd.id
FROM test AS t
UNNEST t.countryDetails AS cd
UNNEST cd.stateInfo AS sd
WHERE t.type = "countries";

pass array to the puppet resource defined

I would like to test if multiple packages exist in my environment or not, based on this test I would like to generate the final catalog.
if !defined( Package[ 'apache2' ] ) {
package { 'apache2':
ensure => installed,
}
}
if !defined( Package[ 'libapache2-svn' ] ) {
package { 'libapache2-svn':
ensure => installed,
}
}
In future I would like to control in the following way:
Package { ensure => "installed" }
$packageList = [ 'apache2', 'libapache2-svn' ]
if !defined( Package[ $packageList ] ) {
package { $packageList: }
}

Resources