Playing with Pulp's REST API

Pulp gives you a very powerful admin cli utility in pulp-admin, however that power comes with a price. The command string can sometimes get a bit lengthy. Issuing iterative commands (bulk operations to all repositories for example) often require a bit of grep or awk piping in order to get things done. Fortunately pulp has implemented a very nice rest api that allows you create your own programmable hooks for repetitive tasks. A dash of python, a hint of json, and you're off and running with your own custom code for pulp. How easy is it to work with? I finally caved in and started learning python exactly 4 days ago. You can jump from 'hello world' to writing your own pulp hooks in no time at all.

Authentication:

The first thing you'll need to do if you want to talk to pulp is to authenticate. This is pretty straight forward, but unfortunately it's not well documented yet. We'll start with an example of basic auth. This could probably be done with urllib2, but as Jay Dobies helpfully pointed out, it's just easier with httpslib.


#!/usr/bin/python
from M2Crypto import httpslib
import base64

# Set the basic info
username = 'admin'
password = 'admin'

# Create the auth header
raw = ':'.join((username, password))
encoded = base64.encodedstring(raw)[:-1]
headers = {}
headers['Authorization'] = 'Basic ' + encoded

With that bit of code we've taken the username and password, wrapped them in base64 and made ourselves a header we can use when we connect to the server. Let's extend this a bit further and actually do something.
#!/usr/bin/python
from M2Crypto import httpslib
import base64

# Set the basic info
username = 'admin'
password = 'admin'
hostname = 'pulp.example.com' # Change me!
port = 443

# Create the auth header
raw = ':'.join((username, password))
encoded = base64.encodedstring(raw)[:-1]
headers = {}
headers['Authorization'] = 'Basic ' + encoded

# We'll need this later on
method = 'GET' # POST, DELETE, etc.
url = '/pulp/api/v2/repositories/'
body = None # Fill in with a dict for a POST

#Make the connection, and get some info
connection = httpslib.HTTPSConnection(hostname, port)
connection.request(method, url, body=body, headers=headers)
response = connection.getresponse()
response_str = response.read()

# Let's see what we got
print 'Response Code: %s' % response.status
print 'Response: %s' % response_str
If you've set everything correctly, you should get a response code of 200, and a glob of information about any repositories you have configured in pulp. If you have no repos defined, you'll just get an empty response string.
Response Code: 200
Response: []

Great! Now we're getting somewhere, but printing a glob of junk isn't really that useful. Let's tweak this a bit and have it print out something useful, like the repo-id values.
#!/usr/bin/python
from M2Crypto import httpslib
import base64
import simplejson

# Set the basic info
username = 'admin'
password = 'admin'
hostname = 'pulp.example.com' # Change me!
port = 443

# Create the auth header
raw = ':'.join((username, password))
encoded = base64.encodedstring(raw)[:-1]
headers = {}
headers['Authorization'] = 'Basic ' + encoded

# We'll need this later on
method = 'GET' # POST, DELETE, etc.
url = '/pulp/api/v2/repositories/'
body = None # Fill in with a dict for a POST

#Make the connection, and get some info
connection = httpslib.HTTPSConnection(hostname, port)
connection.request(method, url, body=body, headers=headers)
response = connection.getresponse()
response_str = simplejson.loads(response.read())

# print stuff out
for repo in response_str:
    print repo['id']


There you go. A simple python script to return the names of the repositories you currently have in pulp!

For more information on pulp, see the website. For the REST API,  see the documentation at http://pulp-dev-guide.readthedocs.org/en/latest/rest-api/index.html

2 comments:

martha meyer said...

I would like to experiment with Pulp via the open source approach of conducting various privilege access apart from the default admin super accounts. Extensions such as underling for virtual administrative assistant platforms could be added if PULP allowed it.

SEJeff said...

Another idea, make your script run in something other than old Linux that doesn't use python 2.5+ and includes json in stdlib. You can install simplejson there, but it makes 0 sense. Try this:

try:
import json
except ImportError:
import simplejson as json

problem solved. They are api compatible.

Post a Comment

Copyright © Bit Integrity