Fixes to docker run
[sdc.git] / catalog-be / sdc-backend-init / chef-repo / cookbooks / sdc-catalog-be-setup / templates / default / user.py.erb
1 #!/usr/bin/python
2 import subprocess
3 #from time import sleep
4 import time
5 from datetime import datetime
6
7 BE_IP="<%= @be_ip %>"
8 BE_PORT="<%= @be_port %>"
9
10 class bcolors:
11     HEADER    = '\033[95m'
12     OKBLUE    = '\033[94m'
13     OKGREEN   = '\033[92m'
14     WARNING   = '\033[93m'
15     FAIL      = '\033[91m'
16     ENDC      = '\033[0m'
17     BOLD      = '\033[1m'
18     UNDERLINE = '\033[4m'
19
20
21 ##############################
22 #    Functions
23 ##############################
24 def checkBackend():
25     command="curl -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
26
27     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
28     (out, err) = proc.communicate()
29     result = out.strip()
30     return result
31
32
33 def checkUser(userName):
34     command="curl -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\"   http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/" + userName
35
36     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
37     (out, err) = proc.communicate()
38     result = out.strip()
39     return result
40
41
42
43
44 def createUser( firstName, lastName, userId , email_dom , role ):
45     print '[INFO] create first:[' + firstName + '], last:[' + lastName + '], Id:[' + userId + '], email:[' + userId + '@' + email_dom + '], role:[' + role +']'
46     command="curl -s -o /dev/null -w \"%{http_code}\" -X POST -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" http://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/ -d '{\"firstName\": '" + firstName + "', \"lastName\": '" + lastName + "',\"userId\": '" + userId + "',\"email\": '" + userId + "@" + email_dom + "',\"role\": '" + role + "'}'"
47
48     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
49     (out, err) = proc.communicate()
50     result = out.strip()
51     return result
52
53
54
55
56 ##############################
57 #    Definitions
58 ##############################
59 userId    = [ "demo" , "op0001" , "gv0001" , "jh0003" , "jm0007" , "cs0008" ]
60 firstName = [ "demo" , "Oper" , "Giuseppe" , "Jimmy" , "Joni" , "Carlos" ]
61 lastName  = [ "demo" , "P" , "Verdi" , "Hendrix" , "Mitchell" , "Santana" ]
62 role      = [ "ADMIN" , "OPS" , "GOVERNOR" , "ADMIN" , "TESTER" , "DESIGNER" ]
63 email_dom = "openecomp.org"
64 beStat=0
65
66
67 ##############################
68 #    Main
69 ##############################
70
71 for i in range(1,10):
72     myResult = checkBackend()
73     if myResult == '200':
74         print '[INFO]: Backend is up and running'
75         beStat=1
76         break
77     else:
78         currentTime = datetime.now()
79         print '[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' Backend not responding, try #' + str(i) + bcolors.ENDC
80         time.sleep(10)
81
82 if beStat == 0:
83     print '[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'Backend is DOWN :-(' + bcolors.ENDC
84     exit()
85
86 for user in userId:
87     myResult = checkUser(user)
88     pos = userId.index(user)
89     if myResult == '200':
90         print '[INFO]: ' + user + ' already exists'
91     else:
92         myResult = createUser( firstName[pos], lastName[pos], userId[pos], email_dom, role[pos] )
93         if myResult == '201':
94             print '[INFO]: ' + userId[pos] + ' created, result: [' + myResult + ']'
95         else:
96             print '[ERROR]: ' + bcolors.FAIL + userId[pos] + bcolors.ENDC + ' error creating , result: [' + myResult + ']'