At the university, our list of departments is in routine flux -- departments get created, merged with other departments, renamed, etc. When I initially set up our JSS years ago, I did so with a dump of 280 existing LDAP departments at the time. Today there are 326 departments, and needless to say, the missing data is creating some gaps in information within the computer records. I need to update the list of departments in the JSS.
Sadly, there is no current mechanism in the JSS to get those updates directly from LDAP, so I'm attempting to script updates via the JSS API. I'm having no difficulty retrieving existing information via GET, but I've yet to have any success creating new records with POST. Each attempt is successful from an HTTP point of view (return code of 201), but the JSS itself doesn't seem to create anything new, returning only:
<?xml version="1.0" encoding="UTF-8"?><department><id>-1</id></department>
In each case, I'm using the URI '/departments/id/0' which should create a new department with the next available ID number. As I understand it, a successful POST via ID number should return the id number created. I don't know where '<id>-1</id>' is coming from, as that certainly isn't a valid ID number. I'm guessing it means the JSS rejected the information for some reason? Any ideas?
I've attempted in both bash and python with the same results. Below are some [sanitized] code examples. I'd appreciate any thoughts on why '-1' keeps coming back as my returned ID value.
Bash (well, sh, but same idea):
#!/bin/sh
user="username"
pass="password"
# department XML data to post
POSTxml="<department><name>General University</name></department>"
# submit POST to JSS and write out HTTP return code
curl https://jssurl.org/JSSResource/departments/id/0 --user "$user:$pass" -H "Content-Type: text/xml" -X POST -d "${POSTxml}" --write-out \\
%{http_code} --output -
exit 0
Python:
import urllib2
# create a password manager and authenticated URL opener
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the base URL to auth against, and the username and password.
top_url = 'https://jssurl.org/JSSResource'
password_mgr.add_password(None, top_url, "username", "password")
# create handler
pwhandler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(pwhandler)
# department XML data to post
POST_xml = '<department><name>General University</name></department>'
# HTTP request
POST_request = urllib2.Request(top_url + '/departments/id/0',POST_xml)
POST_request.add_header('Content-Type','text/xml')
POST_request.get_method = lambda: 'POST'
response = opener.open(POST_request)
print response.read()