Refactoring the check backend, create consumers
[sdc.git] / catalog-be / src / main / resources / scripts / sdcBePy / consumers / run.py
1 import time
2
3 from sdcBePy.common.healthCheck import check_backend, RETRY_ATTEMPTS, get_args
4 from sdcBePy.common.sdcBeProxy import SdcBeProxy
5 from sdcBePy.consumers.models.consumerCandidateList import get_consumers
6 from sdcBePy.users.run import colors
7
8
9 def be_consumers_init(be_ip, be_port, protocol, consumer_candidate_list):
10     sdc_be_proxy = SdcBeProxy(be_ip, be_port, protocol)
11     if check_backend(sdc_be_proxy, RETRY_ATTEMPTS):
12         for consumer in consumer_candidate_list:
13             if sdc_be_proxy.check_user(consumer.consumer_name) != 200:
14                 result = sdc_be_proxy.create_consumer(*consumer.get_parameters())
15                 if result == 201:
16                     print('[INFO]: ' + consumer.consumer_name +
17                           ' created, result: [' + str(result) + ']')
18                 else:
19                     print('[ERROR]: ' + colors.FAIL + consumer.consumer_name + colors.END_C +
20                           ' error creating , result: [' + str(result) + ']')
21             else:
22                 print('[INFO]: ' + consumer.consumer_name + ' already exists')
23     else:
24         print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL
25               + ' Backend is DOWN :-(' + colors.END_C)
26         raise Exception("Cannot communicate with the backend!")
27
28
29 def main():
30     be_ip, be_port, protocol = get_args()
31     be_consumers_init(be_ip, be_port, protocol, get_consumers())
32
33
34 if __name__ == '__main__':
35     main()