#!/usr/bin/python2.7 # grouper_create_group # create a group import httplib2 import json import sys,string def usage(): print('usage: grouper_create_group -f -g -n -d ') print('-f group will be created in this folder') print('-g group ID') print('-n group name') print('-d group description') exit(1) def main(): if len(sys.argv) != 9: usage() if sys.argv[1] != '-f': usage() thisFolder = sys.argv[2] if sys.argv[3] != '-g': usage() thisGroupId = sys.argv[4] if sys.argv[5] != '-n': usage() thisGroupName = sys.argv[6] if sys.argv[7] != '-d': usage() thisGroupDesc = sys.argv[8] thisGroup = thisFolder+':'+thisGroupId # initialize httplib2 http = httplib2.Http() # set the grouper URI (dev or prod), username, password grouper_ws_uri = grouperWSParameters(http) # give an error if the group already exists if grouperGetUuid(http,grouper_ws_uri,thisGroup) != 0: print(thisGroup + " group already exists") exit(1) # create a group createGroup = grouperCreateGroup(http,grouper_ws_uri,thisFolder,thisGroupId,thisGroupName,thisGroupDesc) if createGroup and createGroup['WsGroupSaveResults']['resultMetadata']['resultCode'] == 'SUCCESS': print(thisGroup + " group created") else: print("unable to create " + thisGroup) def grouperCreateGroup(http, grouper_ws_uri, stemIdPath, groupId, groupName, groupDescription): # create group groupIdPath = stemIdPath+':'+groupId body = { "WsRestGroupSaveRequest":{ "wsGroupToSaves":[ { "wsGroupLookup":{ "groupName": groupIdPath }, "wsGroup":{ "extension": groupId, "displayExtension": groupName, "name": groupIdPath, "description": groupDescription } } ] } } result = grouperWSRequest(http, grouper_ws_uri+"/groups", "POST", body) return result def grouperGetUuid(http, grouper_ws_uri, groupName): # get UUID for the specified group thisuuid = 0 body = { "WsRestFindGroupsRequest": { "wsQueryFilter": { "groupName": groupName, "queryFilterType": "FIND_BY_GROUP_NAME_EXACT", } } } findGroups = grouperWSRequest(http, grouper_ws_uri + "/groups", "POST", body) if findGroups and findGroups['WsFindGroupsResults']['resultMetadata']['success'] and 'groupResults' in findGroups['WsFindGroupsResults']: thisuuid = findGroups['WsFindGroupsResults']['groupResults'][0]['uuid'] return thisuuid def grouperWSRequest(http, url, method, body): # send a request to the Grouper Web Service # method can be GET, POST, or PUT content_type = 'application/x-www-form-urlencoded' if method == "POST" or method == "PUT": content_type = 'text/x-json; charset=UTF-8' try: resp, content = http.request(uri=url, method=method, body=json.dumps(body), headers={'Content-Type': content_type}) if resp.status == 200 or resp.status == 201: result = json.loads(content.decode('utf-8')) return result except httplib2.ServerNotFoundError as err: print("Unable to connect to Grouper Web Service") print(err) return None # http request failed, print the response status and content print("http response status "+str(resp.status)) print("http response content "+content) return None def grouperWSParameters(http): # set the Grouper Web Service username and password grouper_username = 'abc1234' grouper_password = 'xxxxxxxxxxxxxxxxxxxx' http.add_credentials(name=grouper_username, password=grouper_password) # the Grouper Web Service URI should point to dev or prod Grouper devGrouperURI = 'https://grouper-dev.cc.columbia.edu/grouper-ws/servicesRest/v2_4_000' prodGrouperURI = 'https://grouper.cc.columbia.edu/grouper-ws/servicesRest/v2_4_000' return devGrouperURI if __name__ == '__main__': main()