3 from StringIO import StringIO
7 #####################################################################################################################################################################################
9 # Import all users from a given file #
12 # python importUsers.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ] #
14 # shortest activation (be host = localhost, be port = 8080): #
15 # python importUsers.py [-f <input file> | --ifile=<input file> ] #
17 #####################################################################################################################################################################################
20 def importUsers(scheme, beHost, bePort, users, adminUser):
26 #print("Going to add user " + user['userId'])
28 getRes = getUser(scheme, beHost, bePort, user)
32 if ( error != None and error == 404 ):
33 res = createUser(scheme, beHost, bePort, user ,adminUser)
37 curResult = (userId, 409)
38 result.append(curResult)
45 def convertUsersToCreationObject(users):
47 cloneUsers = copy.deepcopy(users)
48 for user in cloneUsers:
50 if (user.get('fullName') != None):
52 #user['userId'] = user['userId'] + '1'
57 def getUser(scheme, beHost, bePort, user):
59 userId = user['userId']
65 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user/' + str(userId)
68 #adminHeader = 'USER_ID: ' + adminUser
69 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
70 c.setopt(c.WRITEFUNCTION, lambda x: None)
73 c.setopt(c.SSL_VERIFYPEER, 0)
77 #print("Before get response code")
78 httpRes = c.getinfo(c.RESPONSE_CODE)
79 #print("After get response code")
80 responseCode = c.getinfo(c.RESPONSE_CODE)
82 #print('Status: ' + str(responseCode))
86 return (userId, httpRes)
88 except Exception as inst:
94 def createUser(scheme, beHost, bePort, user, adminUser):
96 userId = user['userId']
101 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user'
105 adminHeader = 'USER_ID: ' + adminUser
106 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
108 data = json.dumps(user)
109 c.setopt(c.POSTFIELDS, data)
111 c.setopt(c.WRITEFUNCTION, lambda x: None)
113 if scheme == 'https':
114 c.setopt(c.SSL_VERIFYPEER, 0)
116 #print("before perform")
120 #print("Before get response code")
121 httpRes = c.getinfo(c.RESPONSE_CODE)
122 #print("After get response code")
123 responseCode = c.getinfo(c.RESPONSE_CODE)
125 #print('Status: ' + str(responseCode))
129 return (userId, httpRes)
131 except Exception as inst:
133 return (userId, None)
136 def errorAndExit(errorCode, errorDesc):
137 if ( errorCode > 0 ):
138 print("status=" + str(errorCode) + ". " + errorDesc)
140 print("status=" + str(errorCode))
144 print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]'
147 print 'Number of arguments:', len(sys.argv), 'arguments.'
156 opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ifile=","scheme="])
157 except getopt.GetoptError:
159 errorAndExit(2, 'Invalid input')
161 for opt, arg in opts:
166 elif opt in ("-i", "--ip"):
168 elif opt in ("-p", "--port"):
170 elif opt in ("-f", "--ifile"):
172 elif opt in ("-s", "--scheme"):
175 print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', users file =',inputfile
177 if ( inputfile == None ):
181 print 'Input file is ', inputfile
183 usersFile = open(inputfile)
185 json_data = json.load(usersFile)
189 cloneUsers = convertUsersToCreationObject(json_data)
191 activeUsers = filter(lambda x: x['status'] == 'ACTIVE', cloneUsers)
195 resultTable = importUsers(scheme, beHost, bePort, activeUsers, adminUser)
197 g = lambda x: x[1] != 201 and x[1] != 409
199 result = filter(g, resultTable)
201 if ( len(result) > 0 ):
202 #print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result)))
203 errorAndExit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0],result)))
205 g = lambda x: x[1] == 409
206 result = filter(g, resultTable)
208 print("-------------------------------------------")
209 print("Existing users: " + ', '.join(map(lambda x: x[0],result)))
211 result = filter(lambda x: x[1] == 201, resultTable)
212 if ( len(result) == 0 ):
213 print("-------------------------------------------")
214 print("No NEW user was loaded. All users are already exist")
215 print("-------------------------------------------")
217 print("-------------------------------------------")
218 print("Loaded users: " + ', '.join(map(lambda x: x[0],result)))
219 print("-------------------------------------------")
221 errorAndExit(0, None)
224 if __name__ == "__main__":