Decouple TXT Report file writing and formatting logic (6/6)
[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 PROTOCOL="<%= @protocol %>"
10
11 class bcolors:
12     HEADER    = '\033[95m'
13     OKBLUE    = '\033[94m'
14     OKGREEN   = '\033[92m'
15     WARNING   = '\033[93m'
16     FAIL      = '\033[91m'
17     ENDC      = '\033[0m'
18     BOLD      = '\033[1m'
19     UNDERLINE = '\033[4m'
20
21
22 ##############################
23 #    Functions
24 ##############################
25 def checkBackend():
26     command="curl -k -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\" "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
27
28     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
29     (out, err) = proc.communicate()
30     result = out.strip()
31     return result
32
33
34 def checkUser(userName):
35     command="curl -k -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\" "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/" + userName
36
37     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
38     (out, err) = proc.communicate()
39     result = out.strip()
40     return result
41
42
43
44
45 def createUser( firstName, lastName, userId , email_dom , role ):
46     print '[INFO] create first:[' + firstName + '], last:[' + lastName + '], Id:[' + userId + '], email:[' + userId + '@' + email_dom + '], role:[' + role +']'
47     command="curl -k -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\" "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/ -d '{\"firstName\": \"" + firstName + "\", \"lastName\": \"" + lastName + "\",\"userId\": \"" + userId + "\",\"email\": \"" + userId + "@" + email_dom + "\",\"role\": \"" + role + "\"}'"
48
49     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
50     (out, err) = proc.communicate()
51     result = out.strip()
52     return result
53
54
55
56
57 ##############################
58 #    Definitions
59 ##############################
60 userId    = [ "demo" , "op0001" , "gv0001" , "jh0003" , "jm0007" , "cs0008" ]
61 firstName = [ "demo" , "Oper" , "Giuseppe" , "Jimmy" , "Joni" , "Carlos" ]
62 lastName  = [ "demo" , "P" , "Verdi" , "Hendrix" , "Mitchell" , "Santana" ]
63 role      = [ "ADMIN" , "OPS" , "GOVERNOR" , "ADMIN" , "TESTER" , "DESIGNER" ]
64 email_dom = "openecomp.org"
65 beStat=0
66
67
68 ##############################
69 #    Main
70 ##############################
71
72 for i in range(1,10):
73     myResult = checkBackend()
74     if myResult == '200':
75         print '[INFO]: Backend is up and running'
76         beStat=1
77         break
78     else:
79         currentTime = datetime.now()
80         print '[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' Backend not responding, try #' + str(i) + bcolors.ENDC
81         time.sleep(10)
82
83 if beStat == 0:
84     print '[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'Backend is DOWN :-(' + bcolors.ENDC
85     exit()
86
87 for user in userId:
88     myResult = checkUser(user)
89     pos = userId.index(user)
90     if myResult == '200':
91         print '[INFO]: ' + user + ' already exists'
92     else:
93         myResult = createUser( firstName[pos], lastName[pos], userId[pos], email_dom, role[pos] )
94         if myResult == '201':
95             print '[INFO]: ' + userId[pos] + ' created, result: [' + myResult + ']'
96         else:
97             print '[ERROR]: ' + bcolors.FAIL + userId[pos] + bcolors.ENDC + ' error creating , result: [' + myResult + ']'