Code Samples

The following code samples demonstrate how to call the Traffic Manager Control API from several different application environments. They are intended to illustrate the similarities, rather than the best practice for each language.

Listing Running Virtual Servers

The examples connect to a Traffic Manager, retrieve a list of the virtual servers and then query whether each virtual server is enabled (i.e. running). They then print out the running virtual servers.

The code structure is as follows:

Specify the location of the Admin Server, and the username and password of an account in the “admin” group or another group with explicit “SOAP Control API” permissions (see Security Considerations).

If necessary, configure the HTTPS layer to accept the Admin Server’s self-signed certificate.

Instantiate a means of calling the SOAP methods of the latest version of the VirtualServer interface, generally with reference to the WSDL specification1.

Invoke the VirtualServer:getVirtualServerNames() method, which returns an array of string values.

Invoke the VirtualServer:getEnabled() method, providing an array of string values (the names) and obtaining an array of Boolean values.

Iterate through the arrays, printing the names of the virtual servers which are enabled.

listVS.pl using Perl SOAP::Lite

#!/usr/bin/perl -w

 

use SOAP::Lite 0.60;

 

# This is the url of the Traffic Manager Admin Server

my $admin_server = 'https://username:password@host:9090';

 

my $conn = SOAP::Lite

-> ns('http://soap.zeus.com/zxtm/1.0/VirtualServer/')

-> proxy("$admin_server/soap");

 

# Get a list of Virtual Servers

my $res = $conn->getVirtualServerNames();

my @names = @{$res->result};

 

# Establish which are enabled

$res = $conn->getEnabled( \@names );

my @enabled = @{$res->result};

 

# Print those which are enabled

for( my $i = 0; $i <= $#names; $i++ ) {

if( $enabled[$i] ) {

print "$names[$i]\n";

}

}

Run the example as follows:

$ ./listVS.pl

Main website

Mail servers

Test site

To run this example, you need Perl, SOAP::Lite and IO::Socket::SSL.

On Debian-based systems, install the packages libsoap-lite-perl and libio-socket-ssl-perl.

On RedHat based systems, you’ll need the perl-SOAP-Lite and perl-IO-Socket-SSL rpms.

Sites that use CPAN can obtain the modules from the following URLs:

http://search.cpan.org/~byrne/SOAP-Lite-0.67/lib/OldDocs/SOAP/Lite.pm

http://search.cpan.org/~behroozi/IO-Socket-SSL-0.97/SSL.pm

Early versions of SOAP::Lite used a “uri” method instead of the current “ns” one. This affected versions prior to 0.65_5. If you are using a old version of SOAP::Lite, use the following code to create the SOAP::Lite connection instead:

my $conn = SOAP::Lite

-> uri('http://soap.zeus.com/zxtm/1.0/VirtualServer/')

-> proxy("$admin_server/soap");

Perl’s SOAP::Lite module does not use the WSDL file to perform any type checking, so calling errors are detected at runtime and SOAP structures and enumerations must be managed manually (for further information, see Using Perl SOAP::Lite). The SSL layer is able to accept self-signed certificates.

listVS.cs using C Sharp

using System;

using System.Net;

using System.IO;

using System.Security.Cryptography.X509Certificates;

 

public class AllowSelfSignedCerts : IcertificatePolicy {

public bool CheckValidationResult( ServicePoint sp,

X509Certificate cert, WebRequest request, int problem )

{

return true;

}

}

 

public class listVS {

 

public static void Main( string [] args )

{

System.Net.ServicePointManager.CertificatePolicy =

new AllowSelfSignedCerts();

 

string url= "https://host:9090/soap";

string username = "username";

string password = "password";

 

try {

         VirtualServer p = new VirtualServer();

p.Url = url;

p.Credentials = new NetworkCredential( username, password );

 

string[] names = p.getVirtualServerNames();

bool[] enabled = p.getEnabled( names );

 

for ( int i = 0; i < names.Length; i++ ) {

if( enabled[i] ) {

Console.WriteLine( "{0}", names[i] );

}

}

} catch ( Exception e ) {

Console.WriteLine( "{0}", e );

}

}

}

This code works with the .NET 1.1 SDK and with Mono2.

Using .Net 1.1, compile and run the example code as follows:

C:\> wsdl -o:VirtualServer.cs -n:Stingray VirtualServer.wsdl

C:\> csc /out:listVS.exe VirtualServer.cs listVS.cs

C:\> listVS.exe

Main website

Mail servers

Test site

Using Mono, compile and run as follows:

$ wsdl -o:VirtualServer.cs -n:Stingray VirtualServer.wsdl

$ mcs /out:listVS.exe /r:System.Web.Services \

VirtualServer.cs listVS.cs

$ listVS.exe

Main website

Mail servers

Test site

To obtain the WSDL interface specifications for the Control API, use the files located on the Traffic Manager file system in ZEUSHOME/zxtm/etc/wsdl/. Alternatively, download them from the “SOAP API” page of the Traffic Manager’s Online Help.

Note the use of the IcertificatePolicy derived class to override the default certificate checking method. This allows the application to accept the Admin Server’s self-signed certificate.

listVS.java using Java

import com.zeus.soap.zxtm._1_0.*;

 

import java.security.Security;

import java.security.KeyStore;

import java.security.Provider;

import java.security.cert.X509Certificate;

import javax.net.ssl.ManagerFactoryParameters;

import javax.net.ssl.TrustManager;

import javax.net.ssl.TrustManagerFactorySpi;

import javax.net.ssl.X509TrustManager;

 

public class listVS {

 

public static void main( String[] args ) {

 

// Install the all-trusting trust manager

Security.addProvider( new MyProvider() );

Security.setProperty( "ssl.TrustManagerFactory.algorithm",

"TrustAllCertificates" );

 

try {

VirtualServerLocator vsl = new VirtualServerLocator();

vsl.setVirtualServerPortEndpointAddress(

"https://username:password@host:9090/soap" );

VirtualServerPort vsp = vsl.getVirtualServerPort();

 

String[] vsnames = vsp.getVirtualServerNames();

boolean[] vsenabled = vsp.getEnabled( vsnames );

 

for( int i = 0; i < vsnames.length; i++ ){

if( vsenabled[i] ){

System.out.println( vsnames[i] );

}

}

} catch (Exception e) {

System.out.println( e.toString() );

}

 

}

 

/* The following code disables certificate checking.

* Use the Security.addProvider and Security.setProperty

* calls to enable it */

public static class MyProvider extends Provider {

public MyProvider() {

super( "MyProvider", 1.0, "Trust certificates" );

put( "TrustManagerFactory.TrustAllCertificates",

MyTrustManagerFactory.class.getName() );

}

 

protected static class MyTrustManagerFactory

extends TrustManagerFactorySpi {

public MyTrustManagerFactory() {}

protected void engineInit( KeyStore keystore ) {}

protected void engineInit(

ManagerFactoryParameters mgrparams ) {}

protected TrustManager[] engineGetTrustManagers() {

return new TrustManager[] {

new MyX509TrustManager()

};

}

}

 

protected static class MyX509TrustManager

implements X509TrustManager {

public void checkClientTrusted(

X509Certificate[] chain, String authType) {}

public void checkServerTrusted(

X509Certificate[] chain, String authType) {}

public X509Certificate[] getAcceptedIssuers() {

return null;

}

}

}

}

The majority of this code disables client certificate checking. Details of the code and surrounding infrastructure are available at:

http://java.sun.com/j2se/1.5.0/docs/guide/security/jsse/JSSERefGuide.html

This code works with the Java 1.5 SDK/JRE.

To build and run the code

1.Obtain a WSDL-to-Java converter by downloading “axis” from the Apache Web site: http://ws.apache.org/axis/. Copy all the .jar files from axis‑<version>/libs/ to the JAVAHOME/jre/lib/ext/ directory, or add them to your CLASSPATH.

2.To avoid warnings when the code is run, use the following URLs to download and install the Java Activation Framework and JavaMail libraries.

For Java Activation Framework:

http://java.sun.com/products/javabeans/glasgow/jaf.html.

For JavaMail:

http://java.sun.com/products/javamail/.

Copy activation.jar and mail.jar from these packages to JAVAHOME/jre/lib/ext/, or add them to your CLASSPATH.

3.From your build directory, type the following command to convert the required WSDL files into Java code3:

java org.apache.axis.wsdl.WSDL2Java VirtualServer.wsdl

4.To compile and run the example, type the following commands:

javac listVS.java

java listVS

This should produce the following output:

Main website

Mail servers

Test site

This code uses the functions within the VirtualServer interface. Other interfaces use a similar pattern.

For example, if you want to access functions within the “XXX” interface, you need to instantiate an XXXLocator object, declare the location of the Traffic Manager using the function setXXXPortEndpointAddress() and then create a connection using getXXXPort() to return an XXXPort object. You can then invoke methods using the XXXPort object. Java is verbose, but generally repetitive so the patterns can be copied thus:

SystemCacheLocator scl = new SystemCacheLocator();

scl.setSystemCachePortEndpointAddress(

"https://username:password@host:9090/soap" );

SystemCachePort scp = scl.getSystemCachePort();

/* Invoke the methods on the SystemCachePort object */

scp.clearWebCache();

listVS.py using Python

#!/usr/bin/python

 

import SOAPpy

 

conn = SOAPpy.WSDL.Proxy("VirtualServer.wsdl")

names = conn.getVirtualServerNames()

enabled = conn.getEnabled(names)

 

for i in range(0,len(names)):

if ( enabled[i] ):

print names[i]

By default, most SOAP implementations read the location of the SOAP server from the WSDL file4. However, for security reasons, the location of the Admin Server (including the required administrator username and password) is not embedded in the Traffic Manager WSDL files.

Most SOAP toolkits allow you to override the location specified in the WSDL file, but Python’s SOAP.py module does not. Before you run this example, edit your WSDL files. Locate the “soap:address” node at the end of each WSDL file and edit appropriately:

<service name="VirtualServer">

<port name="VirtualServerPort"

binding="Stingrayns:VirtualServerBinding">

<soap:address

location="https://username:password@host:9090/soap" />

</port>

</service>

Run the Python script5:

$ ./listVS.py

Main website

Mail servers

Test site

listVS.php using PHP 5

#!/usr/bin/php

 

<?php

$conn = new SoapClient( "VirtualServer.wsdl",

array('login' => "username", 'password' => "password") );

 

$names = $conn->getVirtualServerNames();

$enabled = $conn->getEnabled($names);

 

for ($i=0; $i < count( $names ); $i++) {

if ( $enabled[$i] )

print "$names[$i]\n";

}

?>

You might need to enable the SOAP extensions in your php.ini file. To do this, follow the instructions at http://www.php.net/soap.

By default, the PHP Soap toolkit expects to find the location of the SOAP server in the WSDL file. To override this behavior, use the method __setLocation(). For example:

#!/usr/bin/php

<?php

$conn = new SoapClient( "VirtualServer.wsdl",

array('login' => "username", 'password' => "password") );

$conn->__setLocation('https://host:9090/soap');

Alternatively, load the WSDL interactively from the Traffic Manager:

<?php

$stm_url = "https://host:9090";

 

$conn = new SoapClient(

$stm_url."/apps/zxtm/wsdl/VirtualServer.wsdl",

array('login' => "username", 'password' => "password") );

$conn->__setLocation($stm_url.'/soap');

For more details on __setLocation(), see http://www.php.net/manual/en/soapclient.setlocation.php.

You can also specify the details from your application, so these do not need to be embedded in the WSDL:

<service name="VirtualServer">

<port name="VirtualServerPort"

binding="Stingrayns:VirtualServerBinding">

<soap:address location="https://host:9090/soap" />

</port>

</service>

Run the PHP script as follows:

$ ./listVS.php

Main website

Mail servers

Test site