Skip to main content
DevOps Image

This post is from the XebiaLabs blog and has not been updated since the original publish date.

Last Updated May 18, 2015 — DevOps Expert

XLD Scripting comparison : Jython vs REST API vs CLI

DevOps

I bet all those of you working on XL Deploy for a while might feel overwhelmed by all those different ways you can interact with the server. There is the GUI interface and then there's CLI(Command Line Interface). Now if you want to go further, it has the REST API and last but not least, the latest addition of the Jython APIs (Server side scripting) that can be used for XL-Rules and for creation of custom REST Endpoints. How about we do a comparison of  Jython API vs REST API vs CLI tool. It would give you a fine comparison of how to do the same thing using different approaches. We can talk further about each one can be used

USE CASE

Our use case is to create an environment and add the infrastructure to that environmentEnvironment(to be created) Id Environments/myEnvInfrastructure (Existing ) NameInfrastructure/windows/server1Infrastructure (Existing ) NameInfrastructure/windowsCredentials : admin/admin

ABOUT

CLI Scripting - It uses a custom java library and jython to interact with a target XLDeploy server. You can connect to any server as long you can access the server and you have login permissions(in security). You require knowledge of python to work your way with CLI. Objects returned from the server are converted to convenient python objects to work with simple python constructsREST API - This is the easiest interface to work with since its a standard and you can make direct calls through command line tools to connect to any target XLD Server. You can connect to any server as long you can access the server and you have login permissions(in security).  It however becomes very tedious since if you are trying to do complex work, you would also have to deal with conversion of xml request/response. You require knowledge about basic usage on how to call REST Services in generalJython API - This is a server side scripting interface so this is definitely the fastest out of three in terms of execution, but it runs on the server itself so it can't connect to different servers. Because of its server side nature, it carries pre-authenticated objects which hold a users security and permissions information. For this, you require good knowledge of python, working knowledge of jython/java(depending on use cases).  

Using REST API 

URL : http://192.168.1.10:4516/deployit/repository/ci/Environments/myEnv
Method : POST
Headers :  authorization:Basic YWRtaW46YWRtaW4= and content-type:application/xml
Body :
<udm.Environment id="Environments/myEnv" >
    <members>
      <ci ref="Infrastructure/windows/server1"/>
      <ci ref="Infrastructure/windows"/>
    </members>
    <dictionaries/>
    <triggers/>
  </udm.Environment>
Output :
<udm.Environment id="Environments/myEnv" token="560782cb-ae0a-4387-b64e-1fbc2c78f60c" created-by="admin" created-at="2015-05-12T13:05:28.513-0400" last-modified-by="admin" last-modified-at="2015-05-12T13:05:28.513-0400">
  <members>
    <ci ref="Infrastructure/windows/server1"/>
    <ci ref="Infrastructure/windows"/>
  </members>
  <dictionaries/>
  <triggers/>
</udm.Environment>

 Using Jython API

from com.xebialabs.deployit.plugin.api.reflect import Typefrom java.util import HashSet# Read Membersmember1 = repositoryService.read("Infrastructure/windows/server1")member2 = repositoryService.read("Infrastructure/windows")# Add them to a new HashSet.. Use ._delegate to get a ConfigurationItem objectnewMembers = HashSet([member1._delegate, member2._delegate])# Create a new Environmenttype = Type.valueOf("udm.Environment")env = type.descriptor.newInstance("Environments/myEnv")# Use this to add individual members to environment #env.members.add(member1._delegate)# Add members to environmentenv.members = newMembers# Create environment in repositoryrepositoryService.create(env.id, env)print "Environment " + env.id + " Created"
 

Using CLI Scripting

member1 = repository.read("Infrastructure/windows/server1")member2 = repository.read("Infrastructure/windows")env = factory.configurationItem("Environments/myEnv", "udm.Environment")newMembers = [member1.id, member2.id]env.members = newMembersrepository.create(env)print str(env.id) + "Created" 

Where to Try each of the options

 For REST API :
  • The simplest way is to use a firefox extension called REST Client , set authorization and content-type headers and try it out
  • Another way is to use them using curl/wget in linux. You will have to set the user/password using respective flags and also set headers. You can put the body content in a file and pass that file with a suitable flag
  • Yet another way is to use python with Requests Library or any other http connection library to make connections
  • Finally, you can make these connections through html/javascript,  java or any other language to setup dashboards, perform tasks on both pc and mobile.
For CLI Scripting :
  • You can simply login in the CLI tool and start typing the commands
  • You may also put the lines in a file with extension .py. Then use the command line with the -f flag to execute the script
For Jython Scripting :

More from the Blog

View more
Mar 04, 2021

Getting key stakeholder buy-in for changes perceived as risky

DevOps
Organizational leaders must recognize that change is vital for the sur ...
Read More
Mar 01, 2021

Discover the change management practices that are ripe for optimization

DevOps
Change has become the most important part of modern digital product cr ...
Read More
Feb 22, 2021

Reckoning DevOps’ role in the enterprise value stream

DevOps
If you’re a software or digital solutions company, you may use DevOps ...
Read More
Feb 10, 2021

Customer spotlight: Schneider avoiding bumps in the road with DevOps adoption

DevOps
Everyone wants to deliver software faster and more reliably. Companies ...
Read More
Contact Us