diff options
author | Paul Sokolovsky <paul.sokolovsky@linaro.org> | 2013-12-06 23:05:15 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2013-12-06 23:05:15 +0200 |
commit | 1695644372d8f39234bb6884efdcf4147c049737 (patch) | |
tree | 2aea4a8cab67fd48f2db0f2004e0d98b96964726 |
Clean up and import Crowd API query tool used for some time.
-rwxr-xr-x | crowd-tool | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/crowd-tool b/crowd-tool new file mode 100755 index 0000000..0ae9ce9 --- /dev/null +++ b/crowd-tool @@ -0,0 +1,91 @@ +#!/usr/bin/env python +import sys +import httplib +import urllib +import base64 +import json +import optparse +import getpass +import logging +from pprint import pprint +#from BeautifulSoup import BeautifulSoup + + +BASE_URL = "https://login.linaro.org:8443/crowd/rest/usermanagement/1" +AUTH = (None, None) + +log = logging.getLogger() + + +class CrowdRequestError(Exception): + pass + + +def strip_html(html): + return html + html = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES) + if not html.body: + return html + m = ' '.join(html.body.findAll(text=True)) + return m + + +def rest(uri, params): + c = httplib.HTTPSConnection("login.linaro.org", 8443) + auth = base64.encodestring('%s:%s' % AUTH) + headers = {"Authorization": "Basic " + auth} + + url = uri + "?" + urllib.urlencode(params) + c.request("GET", "/crowd/rest/usermanagement/1" + url, headers=headers) + resp = c.getresponse() + if resp.status != 200: + data = resp.read() + log.error("Non-successful response code %d from Crowd: %s", resp.status, strip_html(data)) + raise CrowdRequestError(resp, data) + data = json.load(resp) + return data + + +if __name__ == "__main__": + logging.basicConfig() + optparser = optparse.OptionParser(usage="%prog") + optparser.add_option("-u", "--user", default="rest-test", help="Crowd username") + optparser.add_option("-p", "--passwd", help="Crowd password") + optparser.add_option("-P", "--ask-passwd", action="store_true", help="Ask Crowd password") + optparser.add_option("-n", "--nested", action="store_true", help="Process nested groups") + optparser.add_option("-r", "--raw", action="store_true", help="Show raw JSON response") + options, args = optparser.parse_args(sys.argv[1:]) + if len(args) < 1: + optparser.error("Wrong number of arguments") + + if options.ask_passwd: + options.passwd = getpass.getpass("Crowd password: ") + + AUTH = (options.user, options.passwd) + + if args[0] == "user": + pprint(rest("/user.json", {"username": args[1], "expand": "attributes"})) + + elif args[0] == "aliases": + pprint(rest("/aliases.json", {"username": args[1]})) + + elif args[0] == "usergroups": + url = "/user/group/nested.json" if options.nested else "/user/group/direct.json" + data = rest(url, {"username": args[1]}) + if options.raw: + pprint(data) + else: + names = [x["name"] for x in data["groups"]] + names.sort() + for n in names: + print n + + elif args[0] == "ismember": + url = "/user/group/nested.json" if options.nested else "/user/group/direct.json" + try: + data = rest(url, {"username": args[1], "groupname": args[2]}) + assert data["name"] == args[2] + print "yes" + except CrowdRequestError, e: + print e.args[1] + print "no" |