2dbd941cc34171ec7371c0e4cc1c5da0640e4666
[sdc.git] / catalog-be / src / main / resources / scripts / sdcBePy / users / run.py
1 #!/usr/bin/env python3
2
3 import json
4 import os
5 import time
6 from argparse import ArgumentParser
7
8 from sdcBePy import properties
9 from sdcBePy.common.bColors import BColors
10 from sdcBePy.common.healthCheck import check_backend
11 from sdcBePy.common.properties import init_properties
12 from sdcBePy.common.sdcBeProxy import SdcBeProxy
13
14 colors = BColors()
15
16
17 def load_users(conf_path):
18     with open(conf_path, 'r', encoding='utf-8') as f:
19         return json.load(f)
20
21
22 def be_user_init(be_ip, be_port, header, protocol, conf_path):
23     sdc_be_proxy = SdcBeProxy(be_ip, be_port, header, protocol)
24     if check_backend(sdc_be_proxy, properties.retry_attempts):
25         users = load_users(conf_path)
26         for user in users:
27             if sdc_be_proxy.check_user(user['userId']) != 200:
28                 result = sdc_be_proxy.create_user(user['firstName'],
29                                                   user['lastName'],
30                                                   user['userId'],
31                                                   user['email'],
32                                                   user['role'])
33                 if result == 201:
34                     print('[INFO]: ' + user['userId'] +
35                           ' created, result: [' + str(result) + ']')
36                 else:
37                     print('[ERROR]: ' + colors.FAIL + user['userId'] + colors.END_C +
38                           ' error creating , result: [' + str(result) + ']')
39             else:
40                 print('[INFO]: ' + user['userId'] + ' already exists')
41     else:
42         print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL
43               + 'Backend is DOWN :-(' + colors.END_C)
44         raise Exception("Cannot communicate with the backend!")
45
46
47 def get_args():
48     parser = ArgumentParser()
49
50     parser.add_argument('-i', '--ip', required=True)
51     parser.add_argument('-p', '--port', required=True)
52     parser.add_argument('--header')
53     parser.add_argument('--https', action='store_true')
54     path = os.path.dirname(__file__)
55     parser.add_argument('--conf', default=os.path.join(path, 'data', 'users.json'))
56
57     args = parser.parse_args()
58
59     init_properties(10, 10)
60     return [args.ip, args.port, args.header, 'https' if args.https else 'http', args.conf]
61
62
63 def main():
64     be_user_init(*get_args())
65
66
67 if __name__ == "__main__":
68     main()