Sample Scripts

JSON over HTTP API scripts

HTTP login with Curl

Below is an example of the login process using an authentication token with the JSON over HTTP API.

The JSON over HTTP API uses authentication tokens for access. The token is sent in a cookie called pxt-session-cookie in a response to a call to the auth.login endpoint. The auth.login endpoint accepts a POST request with a JSON body that has login and password properties in a top-level object.

$ API=https://manager.example.com/rhn/manager/api
$ curl -H "Content-Type: application/json" -d '{"login": "myusername", "password": "mypass"}' -i $API/auth/login

HTTP/1.1 200 200
...
Set-Cookie: pxt-session-cookie=<tokenhash>; ...
...
{"success":true,"messages":[]}

Once the login is successful, the retrieved cookie must be added to each subsequent request for authenticated access.

HTTP GET example

Below is an example of an HTTP GET call to the contentmanagement.lookupProject API. In a GET request, method parameters must be sent as query string key-value pairs.

The JSON output is pretty-printed for clarity.

$ API=https://manager.example.com/rhn/manager/api
$ curl -H "Content-Type: application/json" --cookie "pxt-session-cookie<tokenhash>;" \
> $API/contentmanagement/lookupProject?projectLabel=myproject
{
  "success": true,
  "result": {
    "name": "My Project",
    "description": "My CLM project",
    "id": 1,
    "label": "myproject",
    "orgId": 1
  }
}

HTTP POST example

Below is an example of an HTTP POST call to the contentmanagement.createProject API. In a POST request, method parameters can be sent as query string key-value pairs, as a JSON object in the request body, or a mix of both. object type parameters cannot be represented as a query string element and therefore must be sent in the request body. The following examples show both approaches.

The JSON output is pretty-printed for clarity.

Using the query string

$ API=https://manager.example.com/rhn/manager/api
$ curl -H "Content-Type: application/json" --cookie "pxt-session-cookie<tokenhash>;" -X POST \
> "$API/contentmanagement/createProject?projectLabel=myproject&name=My%20Project&description="
{
  "success": true,
  "result": {
    "name": "My Project",
    "id": 1,
    "label": "myproject",
    "orgId": 1
  }
}

Using the request body

The request body must be a top-level JSON object that contains all the parameters as its properties.

$ API=https://manager.example.com/rhn/manager/api
$ curl -H "Content-Type: application/json" --cookie "pxt-session-cookie<tokenhash>;" \
> -d '{"projectLabel":"myproject","name":"My Project","description":""}' \
> $API/contentmanagement/createProject
{
  "success": true,
  "result": {
    "name": "My Project",
    "id": 1,
    "label": "myproject",
    "orgId": 1
  }
}

===Python 3 example

Below is an example of the system.listActiveSystems call being used.

#!/usr/bin/env python3
import requests
import pprint

MANAGER_URL = "https://manager.example.com/rhn/manager/api"
MANAGER_LOGIN = "username"
MANAGER_PASSWORD = "password"
SSLVERIFY = "/path/to/CA" # or False to disable verify;

data = {"login": MANAGER_LOGIN, "password": MANAGER_PASSWORD}
response = requests.post(MANAGER_URL + '/auth/login', json=data, verify=SSLVERIFY)
print("LOGIN: {}:{}".format(response.status_code, response.json()))

cookies = response.cookies
res2 = requests.get(MANAGER_URL + '/system/listActiveSystems', cookies=cookies, verify=SSLVERIFY)
print("RETCODE: {}".format(res2.status_code))
pprint.pprint(res2.json())

sysinfo = res2.json()['result'][0]

note = {"sid": sysinfo['id'], "subject": "Title", "body": "Content of the Note"}
res2 = requests.post(MANAGER_URL + '/system/addNote', json=note, cookies=cookies, verify=SSLVERIFY)
print("RETCODE: {}".format(res2.status_code))
pprint.pprint(res2.json())

res2 = requests.get(MANAGER_URL + '/system/listNotes?sid={}'.format(sysinfo['id']), cookies=cookies, verify=SSLVERIFY)
print("RETCODE: {}".format(res2.status_code))
pprint.pprint(res2.json())

res2 = requests.post(MANAGER_URL + '/auth/logout', cookies=cookies, verify=SSLVERIFY)
print("RETCODE: {}".format(res2.status_code))
pprint.pprint(res2.json())

XMLRPC Scripts

Perl Example

This Perl example shows the system.listUserSystems call being used to get a list of systems a user has access to. In the example below, the name of each system will be printed.

#!/usr/bin/perl
use Frontier::Client;

my $HOST = 'manager.example.com';
my $user = 'username';
my $pass = 'password';

my $client = new Frontier::Client(url => "http://$HOST/rpc/api");
my $session = $client->call('auth.login',$user, $pass);

my $systems = $client->call('system.listUserSystems', $session);
foreach my $system (@$systems) {
  print $system->{'name'}."\n";
}
$client->call('auth.logout', $session);

Python 2 Example

Below is an example of the user.listUsers call being used. Only the login of each user is printed.

#!/usr/bin/python
import xmlrpclib

MANAGER_URL = "http://manager.example.com/rpc/api"
MANAGER_LOGIN = "username"
MANAGER_PASSWORD = "password"

client = xmlrpclib.Server(MANAGER_URL, verbose=0)

key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)
list = client.user.list_users(key)
for user in list:
  print user.get('login')

client.auth.logout(key)

The following code shows how to use date-time parameters. This code will schedule immediate installation of package rhnlib-2.5.22.9.el6.noarch to system with id 1000000001.

#!/usr/bin/python
from datetime import datetime
import time
import xmlrpclib

MANAGER_URL = "http://manager.example.com/rpc/api"
MANAGER_LOGIN = "username"
MANAGER_PASSWORD = "password"

client = xmlrpclib.Server(MANAGER_URL, verbose=0)

key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)
package_list = client.packages.findByNvrea(key, 'rhnlib', '2.5.22', '9.el6', '', 'noarch')
today = datetime.today()
earliest_occurrence = xmlrpclib.DateTime(today)
client.system.schedulePackageInstall(key, 1000000001, package_list[0]['id'], earliest_occurrence)

client.auth.logout(key)

Python 3 with SSL Example

Below is an example of the user.listUsers call being called.

#!/usr/bin/env python3
from xmlrpc.client import ServerProxy
import ssl

MANAGER_URL = "https://manager.example.com/rpc/api"
MANAGER_LOGIN = "username"
MANAGER_PASSWORD = "password"

# You might need to set to set other options depending on your
# server SSL configuartion and your local SSL configuration
context = ssl.create_default_context()
client = ServerProxy(MANAGER_URL, context=context)
key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)

print(client.user.list_users(key))

client.auth.logout(key)

Python 3 Example

Below is an example of the user.listUsers call being called.

#!/usr/bin/env python3
from xmlrpc.client import ServerProxy

MANAGER_URL = "http://manager.example.com/rpc/api"
MANAGER_LOGIN = "username"
MANAGER_PASSWORD = "password"

client = ServerProxy(MANAGER_URL)
key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)

print(client.user.list_users(key))

client.auth.logout(key)

The following code shows how to use date-time parameters. This code will schedule immediate installation of package rhnlib-2.5.22.9.el6.noarch to system with id 1000000001.

#!/usr/bin/env python3
from datetime import datetime
from xmlrpc.client import ServerProxy

MANAGER_URL = "http://manager.example.com/rpc/api"
MANAGER_LOGIN = "username"
MANAGER_PASSWORD = "password"

client = ServerProxy(MANAGER_URL)

key = client.auth.login(MANAGER_LOGIN, MANAGER_PASSWORD)
package_list = client.packages.findByNvrea(key, 'rhnlib', '2.5.22', '9.el6', '', 'noarch')
earliest_occurrence = datetime.today()
client.system.schedulePackageInstall(key, 1000000001, [package_list[0]['id']], earliest_occurrence)

client.auth.logout(key)

Ruby Example

Below is an example of the channel.listAllChannels API call. List of channel labels is printed.

#!/usr/bin/ruby
require "xmlrpc/client"

@MANAGER_URL = "http://manager.example.com/rpc/api"
@MANAGER_LOGIN = "username"
@MANAGER_PASSWORD = "password"

@client = XMLRPC::Client.new2(@MANAGER_URL)

@key = @client.call('auth.login', @MANAGER_LOGIN, @MANAGER_PASSWORD)
channels = @client.call('channel.listAllChannels', @key)
for channel in channels do
  p channel["label"]
end

@client.call('auth.logout', @key)