JENKINS - How to perform REST Call using vbscript

Jenkins allow you to do inquiry on differents entity using REST API. Here I would like to give an example on how to perform a REST call with vbscript.

 

I create a simple function to do so and I retrieve on the net very usefull function for base64 text encoding.

 

PREREQUISITE

There is a simple prerequisite: when you do an inquiry to jenkins, you should need to authenticate. This is done through the username within a token create directly in Jenkins. You have to go to the user section of the username and select the Configuration menu:

https://<JENKINSURL>/user/<USERNAME>/configure

there is a particular section, "Token API", that give you the ability to create a token to use to perform inquiry and the operations the user can do.

 

Using the "Add new Token" you must give a name for the new entry (it is only a label):

When you click "Genarate" it will generate the real token you must save because it will be impossibile to retrieve again:

 

This token will be used as password in the authenticated operation.

 

 

FUNCTION CODE

'**********************************************************************************************************

'Function to execute REST call to Jenkins and retrieve the response as text

'**********************************************************************************************************

Function JRequest(jHomeURL, endPointClient, strUSR, strPWD, URLProxy, PORTProxy)

'jHomeURL is the Jenkins Home URL, something like "https://jenkins.company.com"

'endPointClient is the ojbect of the request (such as, for the status of MYNODE, "/computer/MYNODE/api/json?depth=0")

'URLProxy is a the proxy URL if need to be used to do the request (in the form "http=proxyname.company.com")

'PORTProxy is the port (pass it as string, example: "8787")

'strUSR is the username to use in the request

'strPWD is the password (token that you can retrieve when you add it from the user configuration in the "<Jenkins Home URL>/user/<USERNAME>/configure")

 

Dim strRes  : strRes = ""

Dim strURL  : strURL = jHomeURL & endPointClient

Dim HttpReq : Set HttpReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")

 

HttpReq.Open "POST", strURL, False

 

'if URLProxy is not "" it means we need the proxy to perform the request

if len(URLProxy)>0 then

HttpReq.setProxy 2, URLProxy, PORTProxy  

end if

HttpReq.setRequestHeader "Authorization", "Basic "& Base64Encode(strUSR &":"& strPWD)

HttpReq.Send

strRes = HttpReq.responseText

 

Set HttpReq = Nothing

 

JRequest = strRes

End Function

'**********************************************************************************************************

'*****************************************************************************************

'Base64 Function to encode user and password for, in this case, the basic authentication

'*****************************************************************************************

Function Base64Encode(sText)

Dim oXML, oNode

Set oXML = CreateObject("Msxml2.DOMDocument.3.0")

Set oNode = oXML.CreateElement("base64")

oNode.dataType = "bin.base64"

oNode.nodeTypedValue = Stream_StringToBinary(sText)

Base64Encode = oNode.text

Set oNode = Nothing

Set oXML = Nothing

End Function

 

Private Function Stream_StringToBinary(Text)

Const adTypeText = 2

Const adTypeBinary = 1

Dim BinaryStream 'As New Stream

Set BinaryStream = CreateObject("ADODB.Stream")

BinaryStream.Type = adTypeText

BinaryStream.CharSet = "us-ascii"

BinaryStream.Open

BinaryStream.WriteText Text

BinaryStream.Position = 0

BinaryStream.Type = adTypeBinary

BinaryStream.Position = 0

Stream_StringToBinary = BinaryStream.Read

Set BinaryStream = Nothing

End Function

 

'*****************************************************************************************

 

JRequest is the major function that perform the REST call to jenkins and retrieve the responseText. You now have the entire response and you can use vbscript string function or regular expression to extract the information you need.

The example report how to check node state, so you should use the function like this:

Dim strResponse

strResponse = JRequest("http://MYJENKINS.COMPANY.COM", _

                                    "/computer/MYNODE/api/json?depth=0", _

                                    "MYUSERNAME", _

                                    "MYTOKEN", _

                                    "", _

                                    "")

 

In this example I don't use Proxy Server to perform the request (last two arguments).

 

strResponse will be something like this:

 

{"_class":"hudson.slaves.SlaveComputer","actions":[{}],"assignedLabels":[{"name":"MYNODE"}],"description":"","displayName":"MYNODE","executors":[{},{}],"icon":"computer-x.png","iconClassName":"icon-computer-x","idle":true,"jnlpAgent":true,"launchSupported":false,"loadStatistics":{"_class":"hudson.model.Label$1"},"manualLaunchAllowed":true,"monitorData":{},"numExecutors":1,"offline":true,"offlineCause":null,"offlineCauseReason":"","oneOffExecutors":[],"temporarilyOffline":false,"absoluteRemotePath":null}

You can now manipulate this response as a string and retrieve the information you need.

Pag: << < > >>