Automation with Perl SOAP XML

perlxml

perlxmlI’m always looking for ways to help automate the environment around me. I was recently asked to investigate how we could integrate our internally developed applications into our installation of CA Service Desk. I looked at the integration offered by Maileater but I wanted the ability to record/track the ticket numbers and handles for any tickets, incidents, requests or changes we were opening within Service Desk. I discovered that I could make SOAP/XML calls directly against the application, as long as I ordered the data properly and provided the correct bits and bytes. The value in tracking the ticket number and handle comes when you want to automatically acknowledge/close previously open tickets or if you might want to re-open previously closed tickets or requests when some condition is met.

I should note that the Service Desk Users forum was a big help in my initial research and already has a number of code snippets available on their site.

Here’s a quick Perl script that demonstrates how to interface with CA Service Desk via SOAP/XML calls. This specific script will create an Incident (type = crt:182) assigned to the group Help Desk ( group = 9C4E34BDF796B04DBAD365034FC92288) with the problem category of Alarms (category = pcat:400978) and set the reporting method to Other ( zreporting_method = i:7304).

The values above are specific to my installation of CA Service Desk, you may need to adjust the values for your specific environment and installation. You will also need to substitute a valid username and password that has access to Service Desk in the variables $USERNAME and $PASSWORD. You will also need to update the $wsdl variable to reference the hostname or IP address of the CA Service Desk application server.

You’ll note that I’m using a number of Perl modules to make life easy. You’ll need to have these Perl modules installed in order for the script to run properly. You can install the modules with the following commands assuming you are running CentOS/RedHat Linux;

yum install perl-SOAP-Lite perl-XML-Simple perl-XML-Dumper

Yum will automatically add any other per-requisite packages for you.

Here’s the actual Perl script;

#!/usr/bin/perl

#use SOAP::Lite trace=>'debug';
use SOAP::Lite;
use Data::Dumper;
use XML::Simple;
use strict;
use warnings;

# Declare Constants
use constant DEBUG      => 0;           # DEBUG settings
use constant CONSOLE    => 0;           # CONSOLE DEBUG settings

################################################################
# UPDATE WITH YOUR SPECIFIC USERNAME AND PASSWORD
################################################################
my $username = 'USERNAME';
my $password = 'PASSWORD';

# Declare Variables
my $xml = new XML::Simple;
my $ticket_number;
my $ticket_handle;
my $wsdl = 'http://hostname:8080/axis/services/USD_R11_WebService?wsdl';
my $service = SOAP::Lite->service($wsdl);

my $sid = $service->login($username, $password);

my $userHandle = $service->getHandleForUserid($sid,$user);
my $method = SOAP::Data->name('createRequest');

my @params = (SOAP::Data->name("sid" => $sid),
SOAP::Data->name("userHandle" => $userHandle),
SOAP::Data->name("attrVals" =>
  \SOAP::Data->value(
   SOAP::Data->name("string" => "description"),
   SOAP::Data->name("string" => "This is a test indicent created from a Perl script using a SOAP/XML interface"),
   SOAP::Data->name("string" => "customer"),
   SOAP::Data->name("string" => $userHandle),
   SOAP::Data->name("string" => "type"),
   SOAP::Data->name("string" => "crt:182"),
   SOAP::Data->name("string" => "category"),
   SOAP::Data->name("string" => "pcat:400978"),
   SOAP::Data->name("string" => "group"),
   SOAP::Data->name("string" => "9C4E34BDF796B04DBAD365034FC92288"),
   SOAP::Data->name("string" => "zreporting_method"),
   SOAP::Data->name("string" => "i:7304"),
   SOAP::Data->name("string" => "summary"),
   SOAP::Data->name("string" => "This is a test incident created via Perl SOAP/XML.")
  )
),
SOAP::Data->name("propertyValues" =>
  \SOAP::Data->value(
   SOAP::Data->name("string" => '')
  )
),
SOAP::Data->name("template" => ''),
SOAP::Data->name("attributes" =>
  \SOAP::Data->value(
   SOAP::Data->name("string" => 'ref_num'),
   SOAP::Data->name("string" => 'persistent_id')
  )
),
SOAP::Data->name("newRequestHandle"),
SOAP::Data->name("newRequestNumber")
);

print "dumper: ".Dumper(\@params)."\n" if (DEBUG);

my $soap = $service->call($method => @params);

if ($soap->fault){
        print "Error\n";
        print "[".$soap->faultcode."] [".$soap->faultstring."] [".$soap->faultdetail."]";
} else {
        print $soap->result if (DEBUG);
}

print "SOAP dump: ".Dumper($soap->result)."\n" if (DEBUG);

$xml = XMLin($soap->result);

$ticket_number = $xml->{Attributes}->{Attribute}->[0]->{AttrValue};
$ticket_handle = $xml->{Attributes}->{Attribute}->[1]->{AttrValue};

print "Ticket Number = $ticket_number\n";
print "Ticket Handle = $ticket_handle\n";

$service->logout($sid);

exit;

Cheers!

{ 1 comment… add one }

  • Laurent January 28, 2014, 3:51 am

    great, thks for this post
    just in line 27 :
    my $userHandle = $service->getHandleForUserid($sid,$user);
    should be
    my $userHandle = $service->getHandleForUserid($sid,$username);

    ;-)

Leave a Comment