Jenkins API xpath like functionality for JSON - json

I am trying to use the jenkins API to retrieve a list of running jobs buildURLs and this works with this the query
https://jenkins.server.com/computer/api/xml?tree=computer[executors[currentExecutable[url]]]&depth=1&xpath=//url&wrapper=buildUrls
By searching for all executors on given jenkins server and then grabbing the urls and wrapping them in a xml buildUrls object
(what I actually want is a count of total running jobs but I can filter in api then get a .size of this once client side)
However the application I am uses only accepts json, although I could convert to json I am wondering if there is a way I can have this return a json object which contains just the buildUrls. Currently if I change return to json the xpath modification is removed (since its xml only)
And I instead get returned the list of all executors and their status
I think my answer lies within Tree but i cant seem to get the result I want

To return a JSON object you can modify the URL as below
http://jenkins.server.com/computer/api/json?tree=computer[executors[currentExecutable[url]]]&depth=1&pretty=true
It will not be possible to get only the build urls you need to iterate through all the executables to get the total running jobs. The reason is that build urls alone cannot give you information where it is running. Hence, unfortunately we need to iterate through each executor.
The output you will get is something like below.
Another way of doing this same thing is parsing via jobs
http://jenkins.server.com/api/json?tree=jobs[name,url,lastBuild[building,timestamp]]&pretty=true
You can check for building parameter. But here too you will not be able to filter output from url diretly in jenkins. Iteration would be required through each and every job.

Related

jmeter multi response data used in request

i have a test plan that has many POST calls like
/api/v1/budgets
now each of those call has a response which return a uuid from the data base, i extract that using a json path extractor and save it to a variable
after i'm doing all the post calls, i need to do the same amount of calls but with DELETE and do it with the uuid i got from the response
is there an efficient way to extract those uuid? for now i had to add a json path extractor manually to each call
and after that, is there a way to save them and run on those saved vars in a loop just send the next one each time?
also i'm gonna use multiple users for each thread, so i don't know if jmeter will be able to solve that issue or i need to handle that as well the threads and the users per thread
JMeter provides ForEach Controller which can iterate variables having numeric postfix like:
uuid_1
uuid_2
uuid_3
etc.
So you can store the uuids above way using for example __counter() function and use single HTTP Request under ForEach Controller in order to delete them.
I would also recommend getting familiarized with Here’s What to Do to Combine Multiple JMeter Variables article to learn how to work with compound variables in JMeter scripts.

Groovy to generate dynamic parameters based on JSON of job values?

I just found that Jenkins jobs via a JSON URL each job has using this format: https://jenkinsurl.net/job/_test/lastBuild/api/json provides info like below.
{"_class":"hudson.model.FreeStyleBuild","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"build_num","value":""}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user anonymous","userId":"anonymous","userName":"anonymous"}]},{},{},{},{},{},{"_class":"org.jenkinsci.plugins.buildenvironment.actions.BuildEnvironmentBuildAction"},{}],"artifacts":[],"building":false,"description":null,"displayName":"#1","duration":1850,"estimatedDuration":1850,"executor":null,"fullDisplayName":"_test #1","id":"1","keepLog":false,"number":1,"queueId":5753,"result":"SUCCESS","timestamp":1479700053274,"url":"http://jenkinsserver.net/job/_test/1/","builtOn":"node_name","changeSet":{"_class":"hudson.scm.EmptyChangeLogSet","items":[],"kind":null},"culprits":[]}
I'd like to know how I can query those fields with a Groovy script to help me build Dynamic parameters when selecting "Build with Parameters". I already have the Groovy and Dynamic parameter plugins and use them for some simple queries I've been using to generate some parameters, like timestamps not overwritten by Jenkins jobs downstream.
How do I query for these and return their values? For example, if I wanted to return the value of the user that started the build from this segment:
[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user anonymous","userId":"anonymous","userName":"anonymous"}]
This isn't a question about getting the person that triggered the build. Sorry if that's not such a great example, but this one appears to be a bit nested, so presumably a good one to learn with.
I know how to reference the value of parameters and get some info to return to generate them, but not how to use that URL and extract specific info from it to help create a parameter's value before the build begins. Is this possible? If not, what other mechanism could I use?
You can do something similar to this one:
How do I discover the additional causes of my Jenkins build?
job = hudson.model.Hudson.instance.getItem("demo-job")
build = job.getLastBuild()
// get action first
def action = build.getAction(hudson.model.CauseAction.class)
cause = action.findCause(hudson.model.Cause.UserIdCause)
println cause.userId
http://javadoc.jenkins-ci.org/hudson/model/Cause.UserIdCause.html
The question is abit confusing so not sure if this helps at all. Anyway, you should be able to extract all the information you need from build returned by getLastBuild().
It not possible to extra parameters before the build begins. If that is what you mean.

Accessing nedb results

The question is: how to access multiple documents returned from a nedb 'find' command.
Understanding some insight I gained using nedb in nodejs.
Regarding docs returned via nedb find and accessing key value pair:
Thought this would work: docs.current.temp_f but it doesn't.
This works: docs[0].current.temp_f does work.
Background and detail:
I'm new to JavaScript, nodejs, nedb, and have never used mongodb, but understand nedb has a similar API.
The reason for my post is: I use this site a lot, but could not find an answer to my problem. I finally figured it out myself, and am posting it here so I can return a contribution to the community.
I'm accessing Wunderground using their API. Works great. I then pushed the results into nedb using the nedb API's 'insert' command. There are multiple examples of this available, here and elsewhere.
Using the nedb API's find command, I was able to retrieve my data. I could print it out using console.log(). Again, multiple examples here and elsewhere for this.
Wunderground returns a JSON object (if requested in this format), in the form {response:{...some data}, current_observation:{...some data...temp_f...some more data}}, which can be inserted into the nedb database.
Using the nedb API 'find' command (e.g.,
db.find({},function(err,docs) {console.log(docs);} I was able to print out my data. But my desire was to just obtain a few key:value pairs, not the whole structure. I thought that something like docs.current_observation.temp_f would do the trick, but this was always 'undefined' and caused an error.
What I didn't understand, because I'm so new in this area, is that nedb is returning a "set" of data; an array of JSON objects in this case, since I was inserting JSON objects. I never saw this mentioned anywhere on the npmjs.com site for nedb. So I wanted to alert others like myself that lacked this knowledge.
Here's what ultimately worked to obtain the key:value information:
console.log("temp_f: ", docs[0].current_observation.temp_f);
where I'm accessing the array element 0.
Of course, this would probably end up in something like a for/in loop.
I would be interested in seeing any additional ideas on retrieving key:values from the result set returned, if you know of any.
//Thought this would work, but it doesn't
docs.current_observation.temp_f
//but this does, where x is a value to record
docs[x].current_observation.temp_f
Simple answer is use mongo's query language with find method to return an array of matching documents then manipulate the array of objects to get the values you need. Use for of when iterating arrays not for in. Alternatively use map, filter, forEach and reduce array methods for manipulating array data.

JBPM rest calls with JSON

We want to start a process in JBPM6 using the rest API. We need to pass an object as a process variable.
We know how to do it JAXB and the execute call. But we want to do it with JSON and /runtime/{deploymentId}/process/{processDefId}/start
Is it possible? we try and have no success.
I am not sure whether my answer exactly addresses the question. But for someones use in future I place couple of lines here.
If you want to set a process variable when starting a process using the RESTful API, you can do it like this.
If your variable name is myVar just add the value as a URL parameter by appending the phrase "map_" to the parameter name. It means the parameter name should now be map_myVar. For an example see below request.
http://<host>:<port>/jbpm-console/rest/runtime/{deploymentId}/process/{processDefId}/start?map_myVar=myValue
You can confirm whether the value is set by writing below code in a script task.
Object var = kcontext.getVariable("myVar");
System.out.println("myVar : " + var);
See the 17.1.1.3. Map query parameters section of JBPM6 documentation.
After talking to the dev that is responsible for the REST API. I was able to confirm how it works.
The
/runtime/{deploymentId}/process/{processDefId}/start
Is a POST request where all the contents in the payload are ignored. The variables are written as key=value in the GET string.
With deployment id: com.web:work:1.0
With processDefId: work.worload
2 variables: var1 and var2
For example:
/runtime/com.web:work:1.0/process/work.worload/start?var1=a&var2=b
I'm still after trying to understand how to define objects with the remote API.
Meanwhile, I also confirmed that it is impossible to define objects using this way. The only way to define objects is only by using the JaxB. This uses the "/execute" path

How to query ASP.NET Web API like OData, but with a POSTed JSON object, not a GET URL/QueryString?

How to query ASP.NET Web API, but with a JSON POST, not GET URL?
The JSON object would contain the same data/filters/sort/paging as ODATA query or LINQ query. Can we deserialize the JSON object into something ODATA/LINQ can understand and then use that to easily execute on the DB (SQL Server)?
We've come across from articles about LINQ Expression Trees and ODataLib ODataUriParser, but still researching.
We want to expose an advanced search web service for a few tables or views in SQL Server, and want to keep it JSON and generic so many platforms can consume it. The consumer would need to pass in the search parameters, and we could probably create a data structure to contain it all, but are trying to also see if we can leverage some query model in ODATA or LINQ.
Any way to instead of putting the OData query in the URL, put it as a POSTed JSON object instead and have it continue to query the DB and return results normally? A couple of current reasons to put in POST are 1. can handle larger size and 2. instead of the consumer learning OData query syntax, they can just popular an search param object model.
Thanks in advance.