[SDC] fix import zip issue
[sdc.git] / sdc-os-chef / sdc-backend / chef-repo / cookbooks / sdc-catalog-be / files / default / consumers.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 checkConsumer(consumerName):
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/consumers/" + consumerName
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 def createConsumer( consumerName, consumerSalt, consumerPass ):
40     print '[INFO] ' + consumerName
41     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/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"
42
43     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
44
45     (out, err) = proc.communicate()
46     result = out.strip()
47     return result
48
49
50
51
52 ##############################
53 #    Definitions
54 ##############################
55 consumersList = [ "aai" , "appc" , "dcae" , "mso" , "sdnc" , "vid" , "cognita" ]
56 salt = "9cd4c3ad2a6f6ce3f3414e68b5157e63"
57 password = "35371c046f88c603ccba152cb3db34ec4475cb2e5713f2fc0a43bf18a5243495"
58 beStat=0
59
60
61 ##############################
62 #    Main
63 ##############################
64
65 for i in range(1,10):
66     myResult = checkBackend()
67     if myResult == '200':
68         print '[INFO]: Backend is up and running'
69         beStat=1
70         break
71     else:
72         currentTime = datetime.now()
73         print '[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' Backend not responding, try #' + str(i) + bcolors.ENDC
74         time.sleep(10)
75
76 if beStat == 0:
77     print '[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'Backend is DOWN :-(' + bcolors.ENDC
78     exit()
79
80 for consumer in consumersList:
81     myResult = checkConsumer(consumer)
82     if myResult == '200':
83         print '[INFO]: ' + consumer + ' already exists'
84     else:
85         myResult = createConsumer( consumer, salt, password )
86         if myResult == '201':
87             print '[INFO]: ' + consumer + ' created, result: [' + myResult + ']'
88         else:
89             print '[ERROR]: ' + bcolors.FAIL + consumer + bcolors.ENDC + ' error creating , result: [' + myResult + ']'