summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py')
-rw-r--r--catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py407
1 files changed, 199 insertions, 208 deletions
diff --git a/catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py b/catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py
index 09fe726cec..8cc8926ac5 100644
--- a/catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py
+++ b/catalog-be/src/main/resources/scripts/import/tosca/importUsersFromYaml.py
@@ -1,10 +1,11 @@
-import pycurl
-import sys, getopt
-from StringIO import StringIO
+import getopt
import json
-import copy
+import sys
+
+import pycurl
import yaml
+
#########################################################################################################################################################################################
# #
# Import all users from a given YAML file #
@@ -20,213 +21,203 @@ import yaml
#########################################################################################################################################################################################
-def importUsers(scheme, beHost, bePort, users, adminUser):
-
- result = []
-
- for user in users:
-
- #print("Going to add user " + user['userId'])
-
- getRes = getUser(scheme, beHost, bePort, user)
- userId = getRes[0]
- error = getRes[1]
- #print error
- if ( error != None and error == 404 ):
- res = createUser(scheme, beHost, bePort, user ,adminUser)
- result.append(res)
- else:
- if ( error == 200 ):
- curResult = (userId, 409)
- result.append(curResult)
- else:
- result.append(getRes)
-
- return result
-
-
-def getUser(scheme, beHost, bePort, user):
-
- if (user.get('userId') == None):
- print "Ignoring record", user
- return ('NotExist', 200)
- userId = user['userId']
- try:
- buffer = StringIO()
- c = pycurl.Curl()
-
- #print type(userId)
- url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user/' + str(userId)
- c.setopt(c.URL, url)
-
- if scheme == 'https':
- c.setopt(pycurl.SSL_VERIFYPEER, 0)
- c.setopt(pycurl.SSL_VERIFYHOST, 0)
-
- #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(scheme, beHost, bePort, user, adminUser):
-
- if (user.get('userId') == None):
- print "Ignoring record", user
- return ('NotExist', 200)
-
- userId = user['userId']
- try:
- buffer = StringIO()
- c = pycurl.Curl()
-
- url = scheme + '://' + 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)
-
- if scheme == 'https':
- c.setopt(pycurl.SSL_VERIFYPEER, 0)
- c.setopt(pycurl.SSL_VERIFYHOST, 0)
-
- 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 error_and_exit(errorCode, errorDesc):
- if ( errorCode > 0 ):
- print("status=" + str(errorCode) + ". " + errorDesc)
- else:
- print("status=" + str(errorCode))
- sys.exit(errorCode)
-
+def importUsers(scheme, be_host, be_port, users, admin_user):
+ result = []
+
+ for user in users:
+ # print("Going to add user " + user['userId'])
+ get_res = getUser(scheme, be_host, be_port, user)
+ user_id = get_res[0]
+ error = get_res[1]
+ # print error
+ if error is not None and error == 404:
+ res = createUser(scheme, be_host, be_port, user, admin_user)
+ result.append(res)
+ else:
+ if error == 200:
+ cur_result = user_id, 409
+ result.append(cur_result)
+ else:
+ result.append(get_res)
+
+ return result
+
+
+def getUser(scheme, be_host, be_port, user):
+ if user.get('userId') is None:
+ print "Ignoring record", user
+ return 'NotExist', 200
+ user_id = user['userId']
+ try:
+ c = pycurl.Curl()
+
+ # print type(userId)
+ url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/user/' + str(user_id)
+ c.setopt(c.URL, url)
+
+ if scheme == 'https':
+ c.setopt(pycurl.SSL_VERIFYPEER, 0)
+ c.setopt(pycurl.SSL_VERIFYHOST, 0)
+
+ # 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")
+ http_res = c.getinfo(c.RESPONSE_CODE)
+ # print("After get response code")
+ # response_code = c.getinfo(c.RESPONSE_CODE)
+ # print('Status: ' + str(response_code))
+
+ c.close()
+
+ return user_id, http_res
+
+ except Exception as inst:
+ print(inst)
+ return user_id, None
+
+
+def createUser(scheme, be_host, be_port, user, admin_user):
+ if user.get('userId') is None:
+ print "Ignoring record", user
+ return 'NotExist', 200
+
+ user_id = user['userId']
+ try:
+ c = pycurl.Curl()
+
+ url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/user'
+ c.setopt(c.URL, url)
+ c.setopt(c.POST, 1)
+
+ admin_header = 'USER_ID: ' + admin_user
+ c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', admin_header])
+
+ data = json.dumps(user)
+ c.setopt(c.POSTFIELDS, data)
+
+ if scheme == 'https':
+ c.setopt(pycurl.SSL_VERIFYPEER, 0)
+ c.setopt(pycurl.SSL_VERIFYHOST, 0)
+
+ c.setopt(c.WRITEFUNCTION, lambda x: None)
+ # print("before perform")
+ c.perform()
+
+ # print("Before get response code")
+ http_res = c.getinfo(c.RESPONSE_CODE)
+ # print("After get response code")
+ # responseCode = c.getinfo(c.RESPONSE_CODE)
+ # print('Status: ' + str(responseCode))
+
+ c.close()
+
+ return user_id, http_res
+
+ except Exception as inst:
+ print(inst)
+ return user_id, None
+
+
+def error_and_exit(error_code, error_desc):
+ if error_code > 0:
+ print("status=" + str(error_code) + ". " + error_desc)
+ else:
+ print("status=" + str(error_code))
+ sys.exit(error_code)
+
+
def usage():
- 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> ]'
+ 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> ]'
+
def main(argv):
- print 'Number of arguments:', len(sys.argv), 'arguments.'
-
- beHost = 'localhost'
- bePort = '8080'
- inputfile = None
-
- adminUser = 'jh0003'
- scheme ='http'
-
- try:
- opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ifile=","scheme="])
- except getopt.GetoptError:
- usage()
- error_and_exit(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
- elif opt in ("-s", "--scheme"):
- scheme = arg
-
- print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', users file =',inputfile
-
- if ( inputfile == None ):
- usage()
- sys.exit(3)
-
- print 'Input file is ', inputfile
-
-
- usersAsYamlFile = open(inputfile, 'r')
- usersDoc = yaml.load(usersAsYamlFile)
- print usersDoc
-
- cloneUsers = []
- for users in usersDoc.values():
- for x,y in users.items():
- copiedUser = y
- copiedUser['userId'] = x
- #print copiedUser
- cloneUsers.append(copiedUser)
-
- print cloneUsers
-
- usersAsYamlFile.close()
-
- #activeUsers = filter(lambda x: x.get('status') == None or x['status'] == 'ACTIVE', cloneUsers)
-
- resultTable = importUsers(scheme, beHost, bePort, cloneUsers, 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)))
- error_and_exit(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("-------------------------------------------")
-
- error_and_exit(0, None)
+ print 'Number of arguments:', len(sys.argv), 'arguments.'
+ be_host = 'localhost'
+ be_port = '8080'
+ input_file = None
-if __name__ == "__main__":
- main(sys.argv[1:])
+ admin_user = 'jh0003'
+ scheme = 'http'
+
+ try:
+ opts, args = getopt.getopt(argv, "i:p:f:h:s:", ["ip=", "port=", "ifile=", "scheme="])
+ except getopt.GetoptError:
+ usage()
+ error_and_exit(2, 'Invalid input')
+
+ for opt, arg in opts:
+ # print opt, arg
+ if opt == '-h':
+ usage()
+ sys.exit(3)
+ elif opt in ("-i", "--ip"):
+ be_host = arg
+ elif opt in ("-p", "--port"):
+ be_port = arg
+ elif opt in ("-f", "--ifile"):
+ input_file = arg
+ elif opt in ("-s", "--scheme"):
+ scheme = arg
+
+ print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', users file =', input_file
+
+ if input_file is None:
+ usage()
+ sys.exit(3)
+
+ print 'Input file is ', input_file
+
+ users_as_yaml_file = open(input_file, 'r')
+ users_doc = yaml.load(users_as_yaml_file)
+ print users_doc
+
+ clone_users = []
+ for users in users_doc.values():
+ for x, y in users.items():
+ copied_user = y
+ copied_user['userId'] = x
+ # print copiedUser
+ clone_users.append(copied_user)
+ print clone_users
+
+ users_as_yaml_file.close()
+
+ # activeUsers = filter(lambda x: x.get('status') == None or x['status'] == 'ACTIVE', cloneUsers)
+
+ result_table = importUsers(scheme, be_host, be_port, clone_users, admin_user)
+
+ g = lambda x: x[1] != 201 and x[1] != 409
+
+ result = filter(g, result_table)
+
+ if len(result) > 0:
+ # print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result)))
+ error_and_exit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0], result)))
+
+ g = lambda x: x[1] == 409
+ result = filter(g, result_table)
+
+ print("-------------------------------------------")
+ print("Existing users: " + ', '.join(map(lambda x: x[0], result)))
+
+ result = filter(lambda x: x[1] == 201, result_table)
+ if len(list(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("-------------------------------------------")
+
+ error_and_exit(0, None)
+
+
+if __name__ == "__main__":
+ main(sys.argv[1:])