XLD Scripting comparison : Jython vs REST API vs CLI

| May 19, 2015 | 0 Comments

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 environment

Environment(to be created) Id Environments/myEnv

Infrastructure (Existing ) NameInfrastructure/windows/server1

Infrastructure (Existing ) NameInfrastructure/windows

Server URLhttp://192.168.1.10:4516

Credentials : 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 constructs

REST 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 general

Jython 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 Type
from java.util import HashSet

# Read Members
member1 = repositoryService.read("Infrastructure/windows/server1")
member2 = repositoryService.read("Infrastructure/windows")

# Add them to a new HashSet.. Use ._delegate to get a ConfigurationItem object
newMembers = HashSet([member1._delegate, member2._delegate])

# Create a new Environment
type = 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 environment
env.members = newMembers
# Create environment in repository
repositoryService.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 = newMembers
repository.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 :

 

 


About the Author ()

Amit is the Lead Pre-Sales Solution Architect managing US Pre-Sales Team at XebiaLabs.