Decouple TXT Report file writing and formatting logic (6/6)
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importUsersFromYaml.py
1 import getopt
2 import json
3 import sys
4
5 import pycurl
6 import yaml
7
8
9 #########################################################################################################################################################################################
10 #                                                                                                                                                                                                                                                                                                                                                                       #
11 # Import all users from a given YAML file                                                                                                                                                                                                                                                                                               #
12 #                                                                                                                                                                                                                                                                                                                                                                               #
13 # activation :                                                                                                                                                                                                                                                                                                                                                  #
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> ]   #
15 #                                                                                                                                                                                                                                                                                                                                                                               #
16 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                                                                                                                    #
17 #               python importUsersFromYaml.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                         #
18 #                                                                                                                                                                                                                                                                                                                                                                       #
19 #   PyYAML module shall be added to python.                                                                                                                                                                                                                                                                                             #
20 #   pip install PyYAML>=3.1.0 --proxy=http://one.proxy.att.com:8080                                                                                                                                                                                                             #
21 #########################################################################################################################################################################################
22
23
24 def importUsers(scheme, be_host, be_port, users, admin_user):
25     result = []
26
27     for user in users:
28         # print("Going to add user " + user['userId'])
29         get_res = getUser(scheme, be_host, be_port, user)
30         user_id = get_res[0]
31         error = get_res[1]
32         # print error
33         if error is not None and error == 404:
34             res = createUser(scheme, be_host, be_port, user, admin_user)
35             result.append(res)
36         else:
37             if error == 200:
38                 cur_result = user_id, 409
39                 result.append(cur_result)
40             else:
41                 result.append(get_res)
42
43     return result
44
45
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']
51     try:
52         c = pycurl.Curl()
53
54         # print type(userId)
55         url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/user/' + str(user_id)
56         c.setopt(c.URL, url)
57
58         if scheme == 'https':
59             c.setopt(pycurl.SSL_VERIFYPEER, 0)
60             c.setopt(pycurl.SSL_VERIFYHOST, 0)
61
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)
65         res = c.perform()
66
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))
72
73         c.close()
74
75         return user_id, http_res
76
77     except Exception as inst:
78         print(inst)
79         return user_id, None
80
81
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
86
87     user_id = user['userId']
88     try:
89         c = pycurl.Curl()
90
91         url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/user'
92         c.setopt(c.URL, url)
93         c.setopt(c.POST, 1)
94
95         admin_header = 'USER_ID: ' + admin_user
96         c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', admin_header])
97
98         data = json.dumps(user)
99         c.setopt(c.POSTFIELDS, data)
100
101         if scheme == 'https':
102             c.setopt(pycurl.SSL_VERIFYPEER, 0)
103             c.setopt(pycurl.SSL_VERIFYHOST, 0)
104
105         c.setopt(c.WRITEFUNCTION, lambda x: None)
106         # print("before perform")
107         c.perform()
108
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))
114
115         c.close()
116
117         return user_id, http_res
118
119     except Exception as inst:
120         print(inst)
121         return user_id, None
122
123
124 def error_and_exit(error_code, error_desc):
125     if error_code > 0:
126         print("status=" + str(error_code) + ". " + error_desc)
127     else:
128         print("status=" + str(error_code))
129     sys.exit(error_code)
130
131
132 def usage():
133     print sys.argv[0], \
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> ]'
135
136
137 def main(argv):
138     print 'Number of arguments:', len(sys.argv), 'arguments.'
139
140     be_host = 'localhost'
141     be_port = '8080'
142     input_file = None
143
144     admin_user = 'jh0003'
145     scheme = 'http'
146
147     try:
148         opts, args = getopt.getopt(argv, "i:p:f:h:s:", ["ip=", "port=", "ifile=", "scheme="])
149     except getopt.GetoptError:
150         usage()
151         error_and_exit(2, 'Invalid input')
152
153     for opt, arg in opts:
154         # print opt, arg
155         if opt == '-h':
156             usage()
157             sys.exit(3)
158         elif opt in ("-i", "--ip"):
159             be_host = arg
160         elif opt in ("-p", "--port"):
161             be_port = arg
162         elif opt in ("-f", "--ifile"):
163             input_file = arg
164         elif opt in ("-s", "--scheme"):
165             scheme = arg
166
167     print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', users file =', input_file
168
169     if input_file is None:
170         usage()
171         sys.exit(3)
172
173     print 'Input file is ', input_file
174
175     users_as_yaml_file = open(input_file, 'r')
176     users_doc = yaml.load(users_as_yaml_file)
177     print users_doc
178
179     clone_users = []
180     for users in users_doc.values():
181         for x, y in users.items():
182             copied_user = y
183             copied_user['userId'] = x
184             # print copiedUser
185             clone_users.append(copied_user)
186
187     print clone_users
188
189     users_as_yaml_file.close()
190
191     # activeUsers = filter(lambda x: x.get('status') == None or x['status'] == 'ACTIVE', cloneUsers)
192
193     result_table = importUsers(scheme, be_host, be_port, clone_users, admin_user)
194
195     g = lambda x: x[1] != 201 and x[1] != 409
196
197     result = filter(g, result_table)
198
199     if len(result) > 0:
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)))
202
203     g = lambda x: x[1] == 409
204     result = filter(g, result_table)
205
206     print("-------------------------------------------")
207     print("Existing users: " + ', '.join(map(lambda x: x[0], result)))
208
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("-------------------------------------------")
214     else:
215         print("-------------------------------------------")
216         print("Loaded users: " + ', '.join(map(lambda x: x[0], result)))
217         print("-------------------------------------------")
218
219     error_and_exit(0, None)
220
221
222 if __name__ == "__main__":
223     main(sys.argv[1:])