Catalog alignment
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importUsersFromYaml.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5 import copy
6 import yaml
7
8 #########################################################################################################################################################################################
9 #                                                                                                                                                                                                                                                                                                                                                                       #
10 # Import all users from a given YAML file                                                                                                                                                                                                                                                                                               #
11 #                                                                                                                                                                                                                                                                                                                                                                               #
12 # activation :                                                                                                                                                                                                                                                                                                                                                  #
13 #       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> ]   #
14 #                                                                                                                                                                                                                                                                                                                                                                               #
15 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                                                                                                                    #
16 #               python importUsersFromYaml.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                         #
17 #                                                                                                                                                                                                                                                                                                                                                                       #
18 #   PyYAML module shall be added to python.                                                                                                                                                                                                                                                                                             #
19 #   pip install PyYAML>=3.1.0 --proxy=http://one.proxy.att.com:8080                                                                                                                                                                                                             #
20 #########################################################################################################################################################################################
21
22
23 def importUsers(scheme, beHost, bePort, users, adminUser):
24         
25         result = []     
26
27         for user in users:
28                         
29                 #print("Going to add user " + user['userId'])
30         
31                 getRes = getUser(scheme, beHost, bePort, user)  
32                 userId = getRes[0]
33                 error = getRes[1]
34                 #print error
35                 if ( error != None and error == 404 ):
36                         res = createUser(scheme, beHost, bePort, user ,adminUser)                       
37                         result.append(res)                      
38                 else:
39                         if ( error == 200 ):
40                                 curResult = (userId, 409)
41                                 result.append(curResult)        
42                         else:
43                                 result.append(getRes)
44
45         return result                           
46
47
48 def getUser(scheme, beHost, bePort, user):
49
50         if (user.get('userId') == None):
51                 print "Ignoring record", user
52                 return ('NotExist', 200)
53         userId = user['userId']
54         try:
55                 buffer = StringIO()
56                 c = pycurl.Curl()
57
58                 #print type(userId)
59                 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user/' + str(userId)
60                 c.setopt(c.URL, url)
61
62                 if scheme == 'https':
63                         c.setopt(pycurl.SSL_VERIFYPEER, 0)
64                         c.setopt(pycurl.SSL_VERIFYHOST, 0)
65
66                 #adminHeader = 'USER_ID: ' + adminUser
67                 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
68                 c.setopt(c.WRITEFUNCTION, lambda x: None)
69                 res = c.perform()
70                                         
71                 #print("Before get response code")      
72                 httpRes = c.getinfo(c.RESPONSE_CODE)
73                 #print("After get response code")       
74                 responseCode = c.getinfo(c.RESPONSE_CODE)
75                 
76                 #print('Status: ' + str(responseCode))
77
78                 c.close()
79
80                 return (userId, httpRes)
81
82         except Exception as inst:
83                 print(inst)
84                 return (userId, None)                           
85
86                 
87
88 def createUser(scheme, beHost, bePort, user, adminUser):
89         
90         if (user.get('userId') == None):
91                 print "Ignoring record", user
92                 return ('NotExist', 200)
93         
94         userId = user['userId']
95         try:
96                 buffer = StringIO()
97                 c = pycurl.Curl()
98
99                 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user'
100                 c.setopt(c.URL, url)
101                 c.setopt(c.POST, 1)             
102
103                 adminHeader = 'USER_ID: ' + adminUser
104                 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
105
106                 data = json.dumps(user)
107                 c.setopt(c.POSTFIELDS, data)
108
109                 if scheme == 'https':
110                         c.setopt(pycurl.SSL_VERIFYPEER, 0)
111                         c.setopt(pycurl.SSL_VERIFYHOST, 0)
112
113                 c.setopt(c.WRITEFUNCTION, lambda x: None)
114                 #print("before perform")        
115                 res = c.perform()
116         #print(res)
117         
118                 #print("Before get response code")      
119                 httpRes = c.getinfo(c.RESPONSE_CODE)
120                 #print("After get response code")       
121                 responseCode = c.getinfo(c.RESPONSE_CODE)
122                 
123                 #print('Status: ' + str(responseCode))
124
125                 c.close()
126
127                 return (userId, httpRes)
128
129         except Exception as inst:
130                 print(inst)
131                 return (userId, None)                           
132
133
134 def error_and_exit(errorCode, errorDesc):
135         if ( errorCode > 0 ):
136                 print("status=" + str(errorCode) + ". " + errorDesc) 
137         else:
138                 print("status=" + str(errorCode))
139         sys.exit(errorCode)
140         
141 def usage():
142         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> ]'
143
144 def main(argv):
145         print 'Number of arguments:', len(sys.argv), 'arguments.'
146
147         beHost = 'localhost' 
148         bePort = '8080'
149         inputfile = None 
150
151         adminUser = 'jh0003'
152         scheme ='http'
153
154         try:
155                 opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ifile=","scheme="])
156         except getopt.GetoptError:
157                 usage()
158                 error_and_exit(2, 'Invalid input')
159                  
160         for opt, arg in opts:
161         #print opt, arg
162                 if opt == '-h':
163                         usage()                        
164                         sys.exit(3)
165                 elif opt in ("-i", "--ip"):
166                         beHost = arg
167                 elif opt in ("-p", "--port"):
168                         bePort = arg
169                 elif opt in ("-f", "--ifile"):
170                         inputfile = arg
171                 elif opt in ("-s", "--scheme"):
172                         scheme = arg
173
174         print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', users file =',inputfile
175         
176         if ( inputfile == None ):
177                 usage()
178                 sys.exit(3)
179
180         print 'Input file is ', inputfile
181
182         
183         usersAsYamlFile = open(inputfile, 'r')
184         usersDoc = yaml.load(usersAsYamlFile)   
185         print usersDoc 
186
187         cloneUsers = [] 
188         for users in usersDoc.values():
189                 for x,y in users.items():               
190                         copiedUser = y
191                         copiedUser['userId'] = x
192                         #print copiedUser 
193                         cloneUsers.append(copiedUser)
194         
195         print cloneUsers
196
197         usersAsYamlFile.close()
198
199         #activeUsers = filter(lambda x: x.get('status') == None or x['status'] == 'ACTIVE', cloneUsers)
200
201         resultTable = importUsers(scheme, beHost, bePort, cloneUsers, adminUser)
202
203         g = lambda x: x[1] != 201 and x[1] != 409
204
205         result = filter(g, resultTable)
206
207         if ( len(result) > 0 ):
208                 #print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result)))
209                 error_and_exit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0],result)))  
210
211         g = lambda x: x[1] == 409
212         result = filter(g, resultTable)
213
214         print("-------------------------------------------")
215         print("Existing users: " + ', '.join(map(lambda x: x[0],result)))
216
217         result = filter(lambda x: x[1] == 201, resultTable)
218         if ( len(result) == 0 ):
219                 print("-------------------------------------------")
220                 print("No NEW user was loaded. All users are already exist")
221                 print("-------------------------------------------")
222         else:
223                 print("-------------------------------------------")
224                 print("Loaded users: " + ', '.join(map(lambda x: x[0],result)))
225                 print("-------------------------------------------")
226
227         error_and_exit(0, None)
228
229
230 if __name__ == "__main__":
231         main(sys.argv[1:])
232