Sync Integ to Master
[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(c.SSL_VERIFYPEER, 0)
64
65                 #adminHeader = 'USER_ID: ' + adminUser
66                 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
67                 c.setopt(c.WRITEFUNCTION, lambda x: None)
68                 res = c.perform()
69                                         
70                 #print("Before get response code")      
71                 httpRes = c.getinfo(c.RESPONSE_CODE)
72                 #print("After get response code")       
73                 responseCode = c.getinfo(c.RESPONSE_CODE)
74                 
75                 #print('Status: ' + str(responseCode))
76
77                 c.close()
78
79                 return (userId, httpRes)
80
81         except Exception as inst:
82                 print(inst)
83                 return (userId, None)                           
84
85                 
86
87 def createUser(scheme, beHost, bePort, user, adminUser):
88         
89         if (user.get('userId') == None):
90                 print "Ignoring record", user
91                 return ('NotExist', 200)
92         
93         userId = user['userId']
94         try:
95                 buffer = StringIO()
96                 c = pycurl.Curl()
97
98                 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user'
99                 c.setopt(c.URL, url)
100                 c.setopt(c.POST, 1)             
101
102                 adminHeader = 'USER_ID: ' + adminUser
103                 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
104
105                 data = json.dumps(user)
106                 c.setopt(c.POSTFIELDS, data)
107
108                 if scheme == 'https':
109                         c.setopt(c.SSL_VERIFYPEER, 0)
110
111                 c.setopt(c.WRITEFUNCTION, lambda x: None)
112                 #print("before perform")        
113                 res = c.perform()
114         #print(res)
115         
116                 #print("Before get response code")      
117                 httpRes = c.getinfo(c.RESPONSE_CODE)
118                 #print("After get response code")       
119                 responseCode = c.getinfo(c.RESPONSE_CODE)
120                 
121                 #print('Status: ' + str(responseCode))
122
123                 c.close()
124
125                 return (userId, httpRes)
126
127         except Exception as inst:
128                 print(inst)
129                 return (userId, None)                           
130
131
132 def errorAndExit(errorCode, errorDesc):
133         if ( errorCode > 0 ):
134                 print("status=" + str(errorCode) + ". " + errorDesc) 
135         else:
136                 print("status=" + str(errorCode))
137         sys.exit(errorCode)
138         
139 def usage():
140         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> ]'
141
142 def main(argv):
143         print 'Number of arguments:', len(sys.argv), 'arguments.'
144
145         beHost = 'localhost' 
146         bePort = '8080'
147         inputfile = None 
148
149         adminUser = 'jh0003'
150         scheme ='http'
151
152         try:
153                 opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ifile=","scheme="])
154         except getopt.GetoptError:
155                 usage()
156                 errorAndExit(2, 'Invalid input')
157                  
158         for opt, arg in opts:
159         #print opt, arg
160                 if opt == '-h':
161                         usage()                        
162                         sys.exit(3)
163                 elif opt in ("-i", "--ip"):
164                         beHost = arg
165                 elif opt in ("-p", "--port"):
166                         bePort = arg
167                 elif opt in ("-f", "--ifile"):
168                         inputfile = arg
169                 elif opt in ("-s", "--scheme"):
170                         scheme = arg
171
172         print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', users file =',inputfile
173         
174         if ( inputfile == None ):
175                 usage()
176                 sys.exit(3)
177
178         print 'Input file is ', inputfile
179
180         
181         usersAsYamlFile = open(inputfile, 'r')
182         usersDoc = yaml.load(usersAsYamlFile)   
183         print usersDoc 
184
185         cloneUsers = [] 
186         for users in usersDoc.values():
187                 for x,y in users.items():               
188                         copiedUser = y
189                         copiedUser['userId'] = x
190                         #print copiedUser 
191                         cloneUsers.append(copiedUser)
192         
193         print cloneUsers
194
195         usersAsYamlFile.close()
196
197         #activeUsers = filter(lambda x: x.get('status') == None or x['status'] == 'ACTIVE', cloneUsers)
198
199         resultTable = importUsers(scheme, beHost, bePort, cloneUsers, adminUser)
200
201         g = lambda x: x[1] != 201 and x[1] != 409
202
203         result = filter(g, resultTable)
204
205         if ( len(result) > 0 ):
206                 #print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result)))
207                 errorAndExit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0],result)))    
208
209         g = lambda x: x[1] == 409
210         result = filter(g, resultTable)
211
212         print("-------------------------------------------")
213         print("Existing users: " + ', '.join(map(lambda x: x[0],result)))
214
215         result = filter(lambda x: x[1] == 201, resultTable)
216         if ( len(result) == 0 ):
217                 print("-------------------------------------------")
218                 print("No NEW user was loaded. All users are already exist")
219                 print("-------------------------------------------")
220         else:
221                 print("-------------------------------------------")
222                 print("Loaded users: " + ', '.join(map(lambda x: x[0],result)))
223                 print("-------------------------------------------")
224
225         errorAndExit(0, None)
226
227
228 if __name__ == "__main__":
229         main(sys.argv[1:])
230