#!/usr/bin/python2.7 # grouper_add_members # add a list of UNIs to the target group # replacing current members if the replace flag is T import httplib2 import json import sys,string import os def usage(): print('usage: grouper_add_members -f -g -r ') print('-f file containing a list of UNIs') print('-g the members will be added to this group') print('-r replace flag can be T or F, replace current members if replace flag is T') exit(1) def main(): # parse the command line arguments if len(sys.argv) != 7: usage() if sys.argv[1] != '-f': usage() inputFileName = sys.argv[2] if sys.argv[3] != '-g': usage() thisGroup = sys.argv[4] if sys.argv[5] != '-r': usage() replaceExisting = sys.argv[6] if replaceExisting != 'T' and replaceExisting != 'F': print('replace flag must be T or F') exit(1) # initialize httplib2 http = httplib2.Http() # set the grouper URI (dev or prod), username, password grouper_ws_uri = grouperWSParameters(http) # verify that the target group exists and is accessible by this user if grouperGetUuid(http,grouper_ws_uri,thisGroup) == 0: print(thisGroup + " group not found (does not exist or is not accessible)") exit(1) # verify that the file exists if not os.path.exists(inputFileName): print('input file not found: ' + inputFileName) exit(1) # read the list of UNIs from the input file infil = open(inputFileName, 'r') subjectList = [] while 1: line = infil.readline() if not line: break line = line.rstrip() # remove the newline character subjectList.append(line) # add this UNI to the list infil.close() # add multiple group members using a single Web Service call addMembers = grouperAddMembers(http,grouper_ws_uri,thisGroup,subjectList,replaceExisting) if addMembers and addMembers['WsAddMemberResults']['results'][0]['resultMetadata']['success']: print('UNIs have been added to '+thisGroup) else: print('unable to add UNIs to '+thisGroup) def grouperAddMembers(http, grouper_ws_uri, groupName, subjectList, replaceFlag): # add list of group members, replace current members if replace flag is T if replaceFlag != 'T' and replaceFlag != 'F': print("replaceFlag "+replaceFlag+" must be T or F") return None # listOfSubjectDictionaries contains a list of dictionaries listOfSubjectDictionaries = [] for nsubjects in range(0,len(subjectList)): listOfSubjectDictionaries.append({'subjectId':subjectList[nsubjects]}) body = { "WsRestAddMemberRequest": { "wsGroupLookup": { "groupName": groupName }, "replaceAllExisting": replaceFlag, "subjectLookups": listOfSubjectDictionaries } } result = grouperWSRequest(http, grouper_ws_uri+"/groups/"+groupName+"/members", "PUT", 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()