9 #########################################################################################################################################################################################
11 # Import all users from a given YAML file #
14 # python importUsersFromYaml.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ] #
16 # shortest activation (be host = localhost, be port = 8080): #
17 # python importUsersFromYaml.py [-f <input file> | --ifile=<input file> ] #
19 # PyYAML module shall be added to python. #
20 # pip install PyYAML>=3.1.0 --proxy=http://one.proxy.att.com:8080 #
21 #########################################################################################################################################################################################
24 def importUsers(scheme, be_host, be_port, users, admin_user):
28 # print("Going to add user " + user['userId'])
29 get_res = getUser(scheme, be_host, be_port, user)
33 if error is not None and error == 404:
34 res = createUser(scheme, be_host, be_port, user, admin_user)
38 cur_result = user_id, 409
39 result.append(cur_result)
41 result.append(get_res)
46 def getUser(scheme, be_host, be_port, user):
47 if user.get('userId') is None:
48 print "Ignoring record", user
49 return 'NotExist', 200
50 user_id = user['userId']
55 url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/user/' + str(user_id)
59 c.setopt(pycurl.SSL_VERIFYPEER, 0)
60 c.setopt(pycurl.SSL_VERIFYHOST, 0)
62 # adminHeader = 'USER_ID: ' + adminUser
63 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
64 c.setopt(c.WRITEFUNCTION, lambda x: None)
67 # print("Before get response code")
68 http_res = c.getinfo(c.RESPONSE_CODE)
69 # print("After get response code")
70 # response_code = c.getinfo(c.RESPONSE_CODE)
71 # print('Status: ' + str(response_code))
75 return user_id, http_res
77 except Exception as inst:
82 def createUser(scheme, be_host, be_port, user, admin_user):
83 if user.get('userId') is None:
84 print "Ignoring record", user
85 return 'NotExist', 200
87 user_id = user['userId']
91 url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/user'
95 admin_header = 'USER_ID: ' + admin_user
96 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', admin_header])
98 data = json.dumps(user)
99 c.setopt(c.POSTFIELDS, data)
101 if scheme == 'https':
102 c.setopt(pycurl.SSL_VERIFYPEER, 0)
103 c.setopt(pycurl.SSL_VERIFYHOST, 0)
105 c.setopt(c.WRITEFUNCTION, lambda x: None)
106 # print("before perform")
109 # print("Before get response code")
110 http_res = c.getinfo(c.RESPONSE_CODE)
111 # print("After get response code")
112 # responseCode = c.getinfo(c.RESPONSE_CODE)
113 # print('Status: ' + str(responseCode))
117 return user_id, http_res
119 except Exception as inst:
124 def error_and_exit(error_code, error_desc):
126 print("status=" + str(error_code) + ". " + error_desc)
128 print("status=" + str(error_code))
134 '[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> ]'
138 print 'Number of arguments:', len(sys.argv), 'arguments.'
140 be_host = 'localhost'
144 admin_user = 'jh0003'
148 opts, args = getopt.getopt(argv, "i:p:f:h:s:", ["ip=", "port=", "ifile=", "scheme="])
149 except getopt.GetoptError:
151 error_and_exit(2, 'Invalid input')
153 for opt, arg in opts:
158 elif opt in ("-i", "--ip"):
160 elif opt in ("-p", "--port"):
162 elif opt in ("-f", "--ifile"):
164 elif opt in ("-s", "--scheme"):
167 print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', users file =', input_file
169 if input_file is None:
173 print 'Input file is ', input_file
175 users_as_yaml_file = open(input_file, 'r')
176 users_doc = yaml.load(users_as_yaml_file)
180 for users in users_doc.values():
181 for x, y in users.items():
183 copied_user['userId'] = x
185 clone_users.append(copied_user)
189 users_as_yaml_file.close()
191 # activeUsers = filter(lambda x: x.get('status') == None or x['status'] == 'ACTIVE', cloneUsers)
193 result_table = importUsers(scheme, be_host, be_port, clone_users, admin_user)
195 g = lambda x: x[1] != 201 and x[1] != 409
197 result = filter(g, result_table)
200 # print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result)))
201 error_and_exit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0], result)))
203 g = lambda x: x[1] == 409
204 result = filter(g, result_table)
206 print("-------------------------------------------")
207 print("Existing users: " + ', '.join(map(lambda x: x[0], result)))
209 result = filter(lambda x: x[1] == 201, result_table)
210 if len(list(result)) == 0:
211 print("-------------------------------------------")
212 print("No NEW user was loaded. All users are already exist")
213 print("-------------------------------------------")
215 print("-------------------------------------------")
216 print("Loaded users: " + ', '.join(map(lambda x: x[0], result)))
217 print("-------------------------------------------")
219 error_and_exit(0, None)
222 if __name__ == "__main__":