b77930d7dbeaa5b3d62a4419d0c8ccc7598bbd4a
[sdc.git] /
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
9 class bcolors:
10     HEADER    = '\033[95m'
11     OKBLUE    = '\033[94m'
12     OKGREEN   = '\033[92m'
13     WARNING   = '\033[93m'
14     FAIL      = '\033[91m'
15     ENDC      = '\033[0m'
16     BOLD      = '\033[1m'
17     UNDERLINE = '\033[4m'
18
19
20 ##############################
21 #    Functions
22 ##############################
23 def checkBackend():
24     command="curl -s -o /dev/null -I -w \"%{http_code}\" -i http://" + BE_IP + ":8080/sdc2/rest/v1/user/jh0003"
25
26     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
27     (out, err) = proc.communicate()
28     result = out.strip()
29     return result
30
31
32 def checkConsumer(consumerName):
33     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 + ":8080/sdc2/rest/v1/consumers/" + consumerName
34
35     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
36     (out, err) = proc.communicate()
37     result = out.strip()
38     return result
39
40
41 def createConsumer( consumerName, consumerSalt, consumerPass ):
42     print '[INFO] ' + consumerName
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://" + BE_IP + ":8080/sdc2/rest/v1/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"
44
45     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
46
47     (out, err) = proc.communicate()
48     result = out.strip()
49     return result
50
51
52
53
54 ##############################
55 #    Definitions
56 ##############################
57 consumersList = [ "aai" , "appc" , "dcae" , "mso" , "sdnc" , "vid" , "cognita", "clamp" , "vfc" , "workflow" , "policy" , "pomba" ]
58 salt = "9cd4c3ad2a6f6ce3f3414e68b5157e63"
59 password = "35371c046f88c603ccba152cb3db34ec4475cb2e5713f2fc0a43bf18a5243495"
60 beStat=0
61
62
63 ##############################
64 #    Main
65 ##############################
66
67 for i in range(1,10):
68     myResult = checkBackend()
69     if myResult == '200':
70         print '[INFO]: Backend is up and running'
71         beStat=1
72         break
73     else:
74         currentTime = datetime.now()
75         print '[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' Backend not responding, try #' + str(i) + bcolors.ENDC
76         time.sleep(10)
77
78 if beStat == 0:
79     print '[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'Backend is DOWN :-(' + bcolors.ENDC
80     exit()
81
82 for consumer in consumersList:
83     myResult = checkConsumer(consumer)
84     if myResult == '200':
85         print '[INFO]: ' + consumer + ' already exists'
86     else:
87         myResult = createConsumer( consumer, salt, password )
88         if myResult == '201':
89             print '[INFO]: ' + consumer + ' created, result: [' + myResult + ']'
90         else:
91             print '[ERROR]: ' + bcolors.FAIL + consumer + bcolors.ENDC + ' error creating , result: [' + myResult + ']'