11#!/usr/bin/env python3
22
33import os
4- import re
54import sys
65import json
76import getopt
8- import collections
97import comanage_utils as utils
108import comanage_person_schema_utils as schema_utils
119
1210SCRIPT = os .path .basename (__file__ )
1311ENDPOINT = "https://registry.cilogon.org/registry/"
1412OSG_CO_ID = 7
1513CMS_GROUP_ID = 4622
14+ CMS_COU_ID = 1785
15+ LDAP_TARGET_ID = 9
1616
1717_usage = f"""\
1818 usage: [PASS=...] { SCRIPT } [OPTIONS]
@@ -33,7 +33,8 @@ class Options:
3333 input_file = None
3434 mapping_file = None
3535 ssh_key_authenticator = 5
36- unix_cluster_id = 5
36+ unix_cluster_id = 10
37+ provisioning_target = LDAP_TARGET_ID
3738
3839
3940options = Options ()
@@ -101,7 +102,10 @@ def build_co_person_record(entry):
101102 #globus id
102103 identifiers .append (schema_utils .co_person_identifier (entry ["globus_id" ], "cmsglobusid" , status = "A" ))
103104 #cilogon id
104- identifiers .append (schema_utils .co_person_identifier (entry ["cilogon_id" ], "oidcsub" , status = "A" ))
105+ if not entry ["cilogon_id" ] is None :
106+ identifiers .append (schema_utils .co_person_identifier (entry ["cilogon_id" ], "oidcsub" , status = "A" ))
107+ else :
108+ print (f"Warning: user { entry ['username' ]} lacks a cilogon id." )
105109
106110 record .update ({"Identifier" : identifiers })
107111
@@ -111,6 +115,11 @@ def build_co_person_record(entry):
111115 # Group Memberships
112116 record .update ({"CoGroupMember" : group_memberships })
113117
118+ roles = []
119+
120+ roles .append (schema_utils .co_person_role (CMS_COU_ID , "CMS User" , "member" , 1 ))
121+ record .update ({"CoPersonRole" : roles })
122+
114123 emails = []
115124
116125 emails .append (schema_utils .co_person_email_address (entry ["email" ]))
@@ -136,10 +145,10 @@ def build_co_person_record(entry):
136145 return record
137146
138147
139- def add_unix_cluster_group (co_person_record ):
148+ def create_unix_cluster_group (co_person_record ):
140149 identifiers_list = co_person_record ["Identifier" ]
141- username = next ((item for item in identifiers_list if item ["type" ] == "osguser" ))
142- uid = next ((item for item in identifiers_list if item ["type" ] == "uid" ))
150+ username = next ((item [ "identifier" ] for item in identifiers_list if item ["type" ] == "osguser" ))
151+ uid = next ((item [ "identifier" ] for item in identifiers_list if item ["type" ] == "uid" ))
143152 description = f"Unix Cluster Group for { username } "
144153 result = utils .create_co_group (username , description , options .osg_co_id , options .endpoint , options .authstr )
145154 ucg = None
@@ -148,33 +157,32 @@ def add_unix_cluster_group(co_person_record):
148157 utils .add_identifier_to_group (group_id , "osggid" , uid , options .endpoint , options .authstr )
149158 utils .add_identifier_to_group (group_id , "osggroup" , username , options .endpoint , options .authstr )
150159 ucg = utils .add_unix_cluster_group (group_id , options .unix_cluster_id , options .endpoint , options .authstr )
160+ utils .provision_group (group_id , options .provisioning_target , options .endpoint , options .authstr )
151161 #TODO throw catch on new group creation
152162 if not (ucg is None ) and ("ResponseType" in ucg ) and (ucg ["ResponseType" ] == "NewObject" ):
153- return ucg ["Id" ]
163+ return ( result ["Id" ])
154164 else :
155165 raise ValueError (f"Failed to create CO Group for Unix Cluster Group, results were: { result } and { ucg } " )
156166
157167
158168def add_unix_cluster_account (co_person_record ):
159169 identifiers_list = co_person_record ["Identifier" ]
160170 names_list = co_person_record ["Name" ]
161- groups_list = co_person_record [ "CoGroupMember" ]
162- username = next ((item for item in identifiers_list if item ["type" ] == "osguser " ))
163- uid = next ((item for item in identifiers_list if item ["type " ] == "uid" ))
164- name = next (( item for item in names_list if item [ "primary_name" ] == True ) )
171+ username = next (( item [ "identifier" ] for item in identifiers_list if item [ "type" ] == "osguser" ))
172+ uid = next ((item [ "identifier" ] for item in identifiers_list if item ["type" ] == "uid " ))
173+ name_id = next ((item for item in names_list if item ["primary_name " ] == True ))
174+ name = schema_utils . name_unsplit ( name_id )
165175 default_group_id = - 1
166- for membership in groups_list :
167- gid = membership ["co_group_id" ]
168- co_group_info = utils .get_co_group (gid , options .endpoint , options .authstr )
169- group_name = co_group_info ["Name" ]
170- if group_name == username :
171- default_group_id = gid
172- break
176+ default_group_id = create_unix_cluster_group (co_person_record )
177+ ucg_membership = schema_utils .co_person_group_member (default_group_id )
178+ if "CoGroupMember" in co_person_record :
179+ co_person_record ["CoGroupMember" ].append (ucg_membership )
180+ else :
181+ co_person_record .update ({"CoGroupMember" : [ucg_membership ]})
173182 if default_group_id != - 1 :
174183 uca = schema_utils .co_person_unix_cluster_acc (options .unix_cluster_id , username , uid , name , default_group_id )
175- print (uca )
176184 if "UnixClusterAccount" in co_person_record :
177- co_person_record . update ({ "UnixClusterAccount" : co_person_record ["UnixClusterAccount" ].append (uca )} )
185+ co_person_record ["UnixClusterAccount" ].append (uca )
178186 else :
179187 co_person_record .update ({"UnixClusterAccount" : [uca ]})
180188 return co_person_record
@@ -183,24 +191,43 @@ def add_unix_cluster_account(co_person_record):
183191def main (args ):
184192 parse_options (args )
185193
186- co_person_records = []
194+ co_person_records = dict ()
187195
188196 data_dump_json = read_data_dump ()
189197 for entry in data_dump_json :
190- co_person_records .append (build_co_person_record (entry ))
191-
192- print (co_person_records [0 ])
193- print (len (co_person_records ))
194-
195- results = utils .core_api_co_person_create (data = co_person_records [0 ], coid = options .osg_co_id , endpoint = options .endpoint , authstr = options .authstr )
196-
197- print (results )
198-
199- co_person_read = utils .core_api_co_person_read ("abbashassani" , options .osg_co_id , options .endpoint , options .authstr )
200-
201- print (co_person_read )
198+ co_person_records .update ({entry ["username" ] : build_co_person_record (entry )})
199+
200+ usernames = list (co_person_records .keys ())
201+
202+ for user in usernames :
203+
204+ try :
205+ try :
206+ #If the CO Person record exists, stop creating/modifying (TODO: switch to modifying existing user rather than trying to create)
207+ if utils .core_api_co_person_read (user , options .osg_co_id , options .endpoint , options .authstr ):
208+ continue
209+ except utils .HTTPRequestError as e :
210+ # If the record *doesn't* exist, pass and make it. Else, some other error happened on our read, like 403 or 500 and we'll try again on another run.
211+ if e .code == 404 :
212+ pass
213+ else :
214+ break
215+ print (f"CREATING RECORDS FOR USER: { user } " )
216+ results_create = utils .core_api_co_person_create (data = co_person_records [user ], coid = options .osg_co_id , endpoint = options .endpoint , authstr = options .authstr )
217+
218+ co_person_data = utils .core_api_co_person_read (user , options .osg_co_id , options .endpoint , options .authstr )
219+
220+ co_person_data = add_unix_cluster_account (co_person_data )
221+
222+ utils .core_api_co_person_update (user , options .osg_co_id , co_person_data , options .endpoint , options .authstr )
223+ except Exception as e :
224+ print (f"\t Exception for user { user } ." )
225+ print (f"\t { e } " )
226+ if results_create :
227+ print (f"\t { results_create } " )
228+ if co_person_data :
229+ print (f"\t { co_person_data } " )
202230
203- print (add_unix_cluster_account (co_person_read ))
204231 # Read in dump to build / update users from
205232 # select which field of the dump co-responds to the identifier we'll use to index the corresponding CO Person
206233 # mapping file from dump attributes to COmanage object types, so we know what each dump attribute should become
0 commit comments