aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/resources/scripts/sdcBePy/users/run.py
blob: 35b9be77a3192e1fa181eea3c84f0d0520c017bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3

import json
import os
import time
from argparse import ArgumentParser

from sdcBePy import properties
from sdcBePy.common.bColors import BColors
from sdcBePy.common.healthCheck import check_backend
from sdcBePy.common.properties import init_properties
from sdcBePy.common.sdcBeProxy import SdcBeProxy

colors = BColors()


def load_users(conf_path):
    with open(conf_path, 'r', encoding='utf-8') as f:
        return json.load(f)


def be_user_init(be_ip, be_port, header, protocol, conf_path, tls_cert, tls_key, tls_key_pw, ca_cert):
    sdc_be_proxy = SdcBeProxy(be_ip, be_port, header, protocol, tls_cert, tls_key, tls_key_pw, ca_cert)
    if check_backend(sdc_be_proxy, properties.retry_attempts):
        users = load_users(conf_path)
        for user in users:
            if sdc_be_proxy.check_user(user['userId']) != 200:
                result = sdc_be_proxy.create_user(user['firstName'],
                                                  user['lastName'],
                                                  user['userId'],
                                                  user['email'],
                                                  user['role'])
                if result == 201:
                    print('[INFO]: ' + user['userId'] +
                          ' created, result: [' + str(result) + ']')
                else:
                    print('[ERROR]: ' + colors.FAIL + user['userId'] + colors.END_C +
                          ' error creating , result: [' + str(result) + ']')
            else:
                print('[INFO]: ' + user['userId'] + ' already exists')
    else:
        print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL
              + 'Backend is DOWN :-(' + colors.END_C)
        raise Exception("Cannot communicate with the backend!")


def get_args():
    parser = ArgumentParser()

    parser.add_argument('-i', '--ip', required=True)
    parser.add_argument('-p', '--port', required=True)
    parser.add_argument('--header')
    parser.add_argument('--https', action='store_true')
    path = os.path.dirname(__file__)
    parser.add_argument('--conf', default=os.path.join(path, 'data', 'users.json'))
    parser.add_argument('--tls_cert')
    parser.add_argument('--tls_key')
    parser.add_argument('--tls_key_pw')
    parser.add_argument('--ca_cert')

    args = parser.parse_args()

    init_properties(10, 10)
    return [args.ip, args.port, args.header, 'https' if args.https else 'http', args.conf, args.tls_cert, args.tls_key, args.tls_key_pw, args.ca_cert]


def main():
    be_user_init(*get_args())


if __name__ == "__main__":
    main()