#!/usr/bin/python2.7 # grouper_create_composite_group # create a composite group import httplib2 import json import sys,string def usage(): print('usage: grouper_create_composite_group -f -g -n -d -t -l -r ') print('-f group will be created in this folder') print('-g group ID') print('-n group name') print('-d group description') print('-t composite type can be intersection or complement') print('-l left group') print('-r right group') exit(1) def main(): if len(sys.argv) != 15: 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] if sys.argv[9] != '-t': usage() thisCompositeType = sys.argv[10] if sys.argv[11] != '-l': usage() thisLeftGroup = sys.argv[12] if sys.argv[13] != '-r': usage() thisRightGroup = sys.argv[14] 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) # verify that the left group exists and is accessible by this user if grouperGetUuid(http,grouper_ws_uri,thisLeftGroup) == 0: print(thisLeftGroup + " group not found (does not exist or is not accessible)") exit(1) # verify that the right group exists and is accessible by this user if grouperGetUuid(http,grouper_ws_uri,thisRightGroup) == 0: print(thisRightGroup + " group not found (does not exist or is not accessible)") exit(1) # create a composite group createGroup = grouperCreateCompositeGroup(http,grouper_ws_uri,thisFolder,thisGroupId,thisGroupName,thisGroupDesc, thisCompositeType,thisLeftGroup,thisRightGroup) if createGroup and createGroup['WsGroupSaveResults']['resultMetadata']['resultCode'] == 'SUCCESS': print(thisGroup + " group created") else: print("unable to create " + thisGroup) def grouperCreateCompositeGroup(http, grouper_ws_uri, stemIdPath, groupId, groupName, groupDescription, compositeType, leftGroupIdPath, rightGroupIdPath): # create composite group groupIdPath = stemIdPath+':'+groupId if compositeType != 'intersection' and compositeType != 'complement': print("composite type "+compositeType+" must be intersection or complement") exit(1) body = { "WsRestGroupSaveRequest":{ "wsGroupToSaves":[ { "wsGroupLookup":{ "groupName": groupIdPath }, "wsGroup":{ "extension": groupId, "displayExtension": groupName, "name": groupIdPath, "description": groupDescription, "detail":{ "hasComposite": "T", "compositeType": compositeType, "leftGroup":{ "name": leftGroupIdPath }, "rightGroup":{ "name": rightGroupIdPath } } } } ] } } 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()