Add cds consumer to the SDC.
[sdc.git] / catalog-be / sdc-backend-init / chef-repo / cookbooks / sdc-catalog-be-setup / templates / default / consumers.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 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 checkConsumer(consumerName):
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/consumers/" + consumerName
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 def createConsumer( consumerName, consumerSalt, consumerPass ):
43     print '[INFO] ' + consumerName
44     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/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"
45
46     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
47
48     (out, err) = proc.communicate()
49     result = out.strip()
50     return result
51
52
53
54
55 ##############################
56 #    Definitions
57 ##############################
58 consumersList = [ "aai" , "appc" , "dcae" , "mso" , "sdnc" , "vid" , "cognita", "clamp" , "vfc" , "workflow" , "policy" , "pomba" , "multicloud", "cds" ]
59 salt = "9cd4c3ad2a6f6ce3f3414e68b5157e63"
60 password = "35371c046f88c603ccba152cb3db34ec4475cb2e5713f2fc0a43bf18a5243495"
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 consumer in consumersList:
84     myResult = checkConsumer(consumer)
85     if myResult == '200':
86         print '[INFO]: ' + consumer + ' already exists'
87     else:
88         myResult = createConsumer( consumer, salt, password )
89         if myResult == '201':
90             print '[INFO]: ' + consumer + ' created, result: [' + myResult + ']'
91         else:
92             print '[ERROR]: ' + bcolors.FAIL + consumer + bcolors.ENDC + ' error creating , result: [' + myResult + ']'