diff options
author | Michael Lando <ml636r@att.com> | 2017-02-19 10:28:42 +0200 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-02-19 10:51:01 +0200 |
commit | 451a3400b76511393c62a444f588a4ed15f4a549 (patch) | |
tree | e4f5873a863d1d3e55618eab48b83262f874719d /asdctool/src/main/resources/scripts/python/user | |
parent | 5abfe4e1fb5fae4bbd5fbc340519f52075aff3ff (diff) |
Initial OpenECOMP SDC commit
Change-Id: I0924d5a6ae9cdc161ae17c68d3689a30d10f407b
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'asdctool/src/main/resources/scripts/python/user')
-rw-r--r-- | asdctool/src/main/resources/scripts/python/user/exportUsers.py | 122 | ||||
-rw-r--r-- | asdctool/src/main/resources/scripts/python/user/importUsers.py | 215 |
2 files changed, 337 insertions, 0 deletions
diff --git a/asdctool/src/main/resources/scripts/python/user/exportUsers.py b/asdctool/src/main/resources/scripts/python/user/exportUsers.py new file mode 100644 index 0000000000..e32a3b0a21 --- /dev/null +++ b/asdctool/src/main/resources/scripts/python/user/exportUsers.py @@ -0,0 +1,122 @@ +import pycurl +import sys, getopt +from StringIO import StringIO +import json + + +################################################################################################################################################ +# # +# Export all active users to file - for 1602+ # +# # +# activation : # +# python exportUsers.py [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ] # +# # +# shortest activation (be host = localhost, be port = 8080): # # +# python exportUsers.py [-f <output file> | --ofile=<output file> ] # +# # +################################################################################################################################################ + +ALL_USERS_SUFFIX = '/sdc2/rest/v1/user/users' + +def errorAndExit(errorCode, errorDesc): + if ( errorCode > 0 ): + print("status=" + str(errorCode) + ". " + errorDesc) + else: + print("status=" + str(errorCode)) + sys.exit(errorCode) + +def getUsers(beHost, bePort, adminUser): + + try: + buffer = StringIO() + c = pycurl.Curl() + + url = 'http://' + beHost + ':' + bePort + ALL_USERS_SUFFIX + print(url) + c.setopt(c.URL, url) + c.setopt(c.WRITEFUNCTION, buffer.write) + #c.setopt(c.WRITEFUNCTION, lambda x: None) + adminHeader = 'USER_ID: ' + adminUser + c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) + res = c.perform() + #print(res) + + #print('Status: %d' % c.getinfo(c.RESPONSE_CODE)) + + c.close() + + body = buffer.getvalue() + + #print(body) + + return (body, None) + + except Exception as inst: + print inst + #print type(inst) # the exception instance + #print inst.args # arguments stored in .args + #print inst # __str__ allows args to be printed directly + #x, y = inst.args + #print 'x =', x + + return (None, inst) + + +def usage(): + print sys.argv[0], '[-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ]' + +def main(argv): + print 'Number of arguments:', len(sys.argv), 'arguments.' + + adminHeader = 'jh0003' + beHost = 'localhost' + bePort = '8080' + outputfile = None + + try: + opts, args = getopt.getopt(argv,"i:p:f:h:",["ip=","port=","ofile="]) + except getopt.GetoptError: + usage() + errorAndExit(2, 'Invalid input') + + for opt, arg in opts: + #print opt, arg + if opt == '-h': + usage() + sys.exit(3) + elif opt in ("-i", "--ip"): + beHost = arg + elif opt in ("-p", "--port"): + bePort = arg + elif opt in ("-f", "--ofile"): + outputfile = arg + + print 'be host =',beHost,', be port =', bePort,', output file =',outputfile + + if ( outputfile == None ): + usage() + sys.exit(3) + + users = getUsers(beHost, bePort, adminHeader) + error = users[1] + body = users[0] + + if ( error != None ): + errorAndExit(5, str(error)) + + #print body + + io = StringIO(body) + usersAsJson = json.load(io) + + writeFile = open(outputfile, 'w') + + json.dump(usersAsJson, writeFile) + + writeFile.close() + + print("-------------------------------------------") + errorAndExit(0, None) + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/asdctool/src/main/resources/scripts/python/user/importUsers.py b/asdctool/src/main/resources/scripts/python/user/importUsers.py new file mode 100644 index 0000000000..669cbbe6f2 --- /dev/null +++ b/asdctool/src/main/resources/scripts/python/user/importUsers.py @@ -0,0 +1,215 @@ +import pycurl +import sys, getopt +from StringIO import StringIO +import json +import copy + +################################################################################################################################################ +# # +# Import all users from a given file # +# # +# activation : # +# python importUsers.py [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ] # +# # +# shortest activation (be host = localhost, be port = 8080): # # +# python importUsers.py [-f <input file> | --ifile=<input file> ] # +# # +################################################################################################################################################ + + +def importUsers(beHost, bePort, users, adminUser): + + result = [] + + for user in users: + + #print("Going to add user " + user['userId']) + + getRes = getUser(beHost, bePort, user) + userId = getRes[0] + error = getRes[1] + #print error + if ( error != None and error == 404 ): + res = createUser(beHost, bePort, user ,adminUser) + result.append(res) + else: + if ( error == 200 ): + curResult = (userId, 409) + result.append(curResult) + else: + result.append(getRes) + + return result + + +def convertUsersToCreationObject(users): + + cloneUsers = copy.deepcopy(users) + for user in cloneUsers: + #print user + if (user.get('fullName') != None): + del user['fullName'] + #user['userId'] = user['userId'] + '1' + #print user + + return cloneUsers + +def getUser(beHost, bePort, user): + + userId = user['userId'] + try: + buffer = StringIO() + c = pycurl.Curl() + + #print type(userId) + url = 'http://' + beHost + ':' + bePort + '/sdc2/rest/v1/user/' + str(userId) + c.setopt(c.URL, url) + + #adminHeader = 'USER_ID: ' + adminUser + c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json']) + c.setopt(c.WRITEFUNCTION, lambda x: None) + res = c.perform() + + #print("Before get response code") + httpRes = c.getinfo(c.RESPONSE_CODE) + #print("After get response code") + responseCode = c.getinfo(c.RESPONSE_CODE) + + #print('Status: ' + str(responseCode)) + + c.close() + + return (userId, httpRes) + + except Exception as inst: + print(inst) + return (userId, None) + + + +def createUser(beHost, bePort, user, adminUser): + + userId = user['userId'] + try: + buffer = StringIO() + c = pycurl.Curl() + + url = 'http://' + beHost + ':' + bePort + '/sdc2/rest/v1/user' + c.setopt(c.URL, url) + c.setopt(c.POST, 1) + + adminHeader = 'USER_ID: ' + adminUser + c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) + + data = json.dumps(user) + c.setopt(c.POSTFIELDS, data) + + c.setopt(c.WRITEFUNCTION, lambda x: None) + #print("before perform") + res = c.perform() + #print(res) + + #print("Before get response code") + httpRes = c.getinfo(c.RESPONSE_CODE) + #print("After get response code") + responseCode = c.getinfo(c.RESPONSE_CODE) + + #print('Status: ' + str(responseCode)) + + c.close() + + return (userId, httpRes) + + except Exception as inst: + print(inst) + return (userId, None) + + +def errorAndExit(errorCode, errorDesc): + if ( errorCode > 0 ): + print("status=" + str(errorCode) + ". " + errorDesc) + else: + print("status=" + str(errorCode)) + sys.exit(errorCode) + +def usage(): + print sys.argv[0], '[-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]' + +def main(argv): + print 'Number of arguments:', len(sys.argv), 'arguments.' + + beHost = 'localhost' + bePort = '8080' + inputfile = None + + adminUser = 'jh0003' + + try: + opts, args = getopt.getopt(argv,"i:p:f:h:",["ip=","port=","ifile="]) + except getopt.GetoptError: + usage() + errorAndExit(2, 'Invalid input') + + for opt, arg in opts: + #print opt, arg + if opt == '-h': + usage() + sys.exit(3) + elif opt in ("-i", "--ip"): + beHost = arg + elif opt in ("-p", "--port"): + bePort = arg + elif opt in ("-f", "--ifile"): + inputfile = arg + + print 'be host =',beHost,', be port =', bePort,', users file =',inputfile + + if ( inputfile == None ): + usage() + sys.exit(3) + + print 'Input file is ', inputfile + + usersFile = open(inputfile) + + json_data = json.load(usersFile) + + #print json_data + + cloneUsers = convertUsersToCreationObject(json_data) + + activeUsers = filter(lambda x: x['status'] == 'ACTIVE', cloneUsers) + + #print activeUsers + + resultTable = importUsers(beHost, bePort, activeUsers, adminUser) + + g = lambda x: x[1] != 201 and x[1] != 409 + + result = filter(g, resultTable) + + if ( len(result) > 0 ): + #print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result))) + errorAndExit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0],result))) + + g = lambda x: x[1] == 409 + result = filter(g, resultTable) + + print("-------------------------------------------") + print("Existing users: " + ', '.join(map(lambda x: x[0],result))) + + result = filter(lambda x: x[1] == 201, resultTable) + if ( len(result) == 0 ): + print("-------------------------------------------") + print("No NEW user was loaded. All users are already exist") + print("-------------------------------------------") + else: + print("-------------------------------------------") + print("Loaded users: " + ', '.join(map(lambda x: x[0],result))) + print("-------------------------------------------") + + errorAndExit(0, None) + + +if __name__ == "__main__": + main(sys.argv[1:]) |