Pytwist details

This section describes the behavior and syntax that is specific to Pytwist.

Authentication modes

The authentication mode of a Pytwist client is important because it affects the SA features and the resources that the client can access. A Pytwist client can run in one of the following modes:

  • Authenticated: The client has called the authenticate(username, password) method on a TwistServer object. After calling the authenticate method, the client is authorized as the SA user specified by the username parameter, much like an end user who logs onto the SA Client.
  • Not Authenticated: The client has not called the TwistServer.authenticate method. On a managed server, the client is authenticated as if it is the device that controls the Agent certificate. When used within a custom extension, a non-authenticated Pytwist client needs access to the Command Engine certificate. For more information on custom extensions and certificates, contact your technical support representative.

TwistServer method syntax

The TwistServer method configures the connection from the client to the Web Services Data Access Engine. (For sample invocations, see Pytwist examples.) All of the arguments of TwistServer aroptional. The following table lists the default values for the arguments.

Arguments of the TwistServer method

Argument

Description

Default

host

The hostname to connect to.

twist

port

The port number to connect to.

1032

secure

Whether to use https for the connection. Allowed values: 1 (true) or 0 (false).

1

ctx

The SSL context for the connection.

None. (See Authentication modes.)

When the TwistServer object is created, the client does not establish a connection with the server. Therefore, if a connectivity problem occurs, it is not encountered until the client calls authenticate or an SA API method.

Error handling

If the TwistServer.authenticate method or an SA API method encounters a problem, a Python exception is raised. You can catch these exceptions in an except clause, as in the following example:

# Create the TwistServer object.
ts = twistserver.TwistServer(’localhost’)
# Authenticate by passing an SA user name and password.
try:
	ts.authenticate(’jdoe’, ’secretpass’)
except:
	print "Authentication failed."
	sys.exit(2)	

Mapping Java package names and data types to Pytwist

The Pytwist interface is for Python, but the SA API is written in Java. Because of the differences between two programming languages a Pytwist client must follow the mapping rules described in this section.

In the SA API documentation, Java package names begin with com.opsware. When specifying the package name in Pytwist, insert pytwist at the beginning, for example:

from pytwist.com.opsware.compliance.sco import *

The SA API documentation specifies method parameters and return values as Java data types. The following table shows how to map the Java data types to Python for the API method invocations in Pytwist.

Mapping data types from Java to Python

Java data type in SA API

Python data type in Pytwist

Boolean

An integer 1 for true or the integer 0 for false.

Object[]
(object array)

As input parameters to API method calls, object arrays can be either Python tuples or lists. As output from API method calls, object arrays are returned as Python tuples.

Map

Dictionary

Date

A long data type representing the number of milliseconds since epoch (midnight on January 1, 1970).