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