|
|
@ -22,7 +22,8 @@ |
|
|
# Column 1 group id |
|
|
# Column 1 group id |
|
|
# Column 2 group name |
|
|
# Column 2 group name |
|
|
# Column 3 is a description |
|
|
# Column 3 is a description |
|
|
# Column 4 member id |
|
|
# Column 4 (optional) member id |
|
|
|
|
|
# Column 5 (optional) list of group names which are added to the group |
|
|
# |
|
|
# |
|
|
# For example: |
|
|
# For example: |
|
|
#group2,"Group 2","My Group 2" |
|
|
#group2,"Group 2","My Group 2" |
|
|
@ -48,6 +49,18 @@ import csv |
|
|
import os |
|
|
import os |
|
|
from sharedfunctions import callrestapi, getfolderid, file_accessible, getidsanduris |
|
|
from sharedfunctions import callrestapi, getfolderid, file_accessible, getidsanduris |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getMembersOfGroup(id): |
|
|
|
|
|
|
|
|
|
|
|
reqval="/identities/groups/"+id+"/members" |
|
|
|
|
|
rst=callrestapi(reqval,'get') |
|
|
|
|
|
|
|
|
|
|
|
lst = set() |
|
|
|
|
|
for member in rst['items']: |
|
|
|
|
|
lst.add(member['id']) |
|
|
|
|
|
|
|
|
|
|
|
return lst |
|
|
|
|
|
|
|
|
# setup command-line arguements |
|
|
# setup command-line arguements |
|
|
parser = argparse.ArgumentParser(description="Create custom groups and establish membership") |
|
|
parser = argparse.ArgumentParser(description="Create custom groups and establish membership") |
|
|
parser.add_argument("-f","--file", help="Full path to csv file containing groups ",required='True') |
|
|
parser.add_argument("-f","--file", help="Full path to csv file containing groups ",required='True') |
|
|
@ -63,16 +76,20 @@ allgroups=callrestapi("/identities/groups","get") |
|
|
# create a list of all groups |
|
|
# create a list of all groups |
|
|
|
|
|
|
|
|
groupslist=[] |
|
|
groupslist=[] |
|
|
|
|
|
groupNameDic = {} # dictionary for lookup group name -> group id |
|
|
|
|
|
|
|
|
if 'items' in allgroups: |
|
|
if 'items' in allgroups: |
|
|
|
|
|
|
|
|
total_items=allgroups['count'] |
|
|
total_items=allgroups['count'] |
|
|
returned_items=len(allgroups['items']) |
|
|
returned_items=len(allgroups['items']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(0,returned_items): |
|
|
for i in range(0,returned_items): |
|
|
|
|
|
|
|
|
groupslist.append(allgroups['items'][i]['id']) |
|
|
groupslist.append(allgroups['items'][i]['id']) |
|
|
|
|
|
groupNameDic[allgroups['items'][i]['name']] = allgroups['items'][i]['id'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# file can be read |
|
|
# file can be read |
|
|
if check: |
|
|
if check: |
|
|
|
|
|
|
|
|
@ -110,9 +127,9 @@ if check: |
|
|
|
|
|
|
|
|
if myresult != None: print("Note: Group: "+newgroup+" created" ) |
|
|
if myresult != None: print("Note: Group: "+newgroup+" created" ) |
|
|
|
|
|
|
|
|
# 4th column is group membership |
|
|
# 4th column is group membership (of user) |
|
|
if cols>=4: |
|
|
if cols>=4 and row[3]!=None and row[3]!="": |
|
|
|
|
|
|
|
|
member=row[3] |
|
|
member=row[3] |
|
|
print("Note: Trying to add user "+ member+ " to group "+newgroup ) |
|
|
print("Note: Trying to add user "+ member+ " to group "+newgroup ) |
|
|
|
|
|
|
|
|
@ -121,6 +138,21 @@ if check: |
|
|
myresult=callrestapi(reqval,reqtype,data=data,stoponerror=0) |
|
|
myresult=callrestapi(reqval,reqtype,data=data,stoponerror=0) |
|
|
if myresult != None: print("Note: user: "+member+" added to group "+newgroup ) |
|
|
if myresult != None: print("Note: user: "+member+" added to group "+newgroup ) |
|
|
|
|
|
|
|
|
|
|
|
if cols>=5 and row[4]!=None and row[4]!="": |
|
|
|
|
|
membergroups = row[4] |
|
|
|
|
|
currentMembers = getMembersOfGroup(id) |
|
|
|
|
|
|
|
|
|
|
|
for membergroup in membergroups.split(','): |
|
|
|
|
|
membergroupid = groupNameDic[membergroup] |
|
|
|
|
|
print ("Note: Trying to add group "+membergroup+" ("+membergroupid+") to group "+newgroup) |
|
|
|
|
|
|
|
|
|
|
|
if membergroupid in currentMembers: |
|
|
|
|
|
print (" Group "+membergroup+" ("+membergroupid+") is already a member. Skiping") |
|
|
|
|
|
else: |
|
|
|
|
|
reqval="/identities/groups/"+id+"/groupMembers/"+membergroupid |
|
|
|
|
|
reqtype='put' |
|
|
|
|
|
myresult=callrestapi(reqval,reqtype,data=data,stoponerror=0) |
|
|
|
|
|
if myresult != None: print("Note: group: "+membergroup+" added to group "+newgroup ) |
|
|
|
|
|
|
|
|
else: |
|
|
else: |
|
|
print("ERROR: cannot read "+file) |
|
|
print("ERROR: cannot read "+file) |
|
|
|