55d475506c25ec90c02ebfd40c75930e44910eea
[pti/o2.git] / o2common / config / config.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 import os
16 import sys
17
18 from o2common.helper import o2logging
19 logger = o2logging.get_logger(__name__)
20
21
22 def get_postgres_uri():
23     host = os.environ.get("DB_HOST", "localhost")
24     port = 54321 if host == "localhost" else 5432
25     password = os.environ.get("DB_PASSWORD", "o2ims123")
26     user, db_name = "o2ims", "o2ims"
27     return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
28
29
30 def get_api_url():
31     host = os.environ.get("API_HOST", "localhost")
32     port = 5005 if host == "localhost" else 80
33     return f"http://{host}:{port}"
34
35
36 def get_root_api_base():
37     return "/"
38
39
40 def get_o2ims_api_base():
41     return get_root_api_base() + 'o2ims_infrastructureInventory/v1'
42
43
44 def get_o2dms_api_base():
45     return get_root_api_base() + "o2dms/v1"
46
47
48 def get_redis_host_and_port():
49     host = os.environ.get("REDIS_HOST", "localhost")
50     port = 63791 if host == "localhost" else 6379
51     return dict(host=host, port=port)
52
53
54 def get_smo_o2endpoint():
55     smo_o2endpoint = os.environ.get(
56         "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
57     return smo_o2endpoint
58
59
60 def get_stx_access_info():
61     # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
62     # username = os.environ.get("STX_USERNAME", "admin")
63     # pswd = os.environ.get("STX_PASSWORD", "passwd1")
64     # stx_access_info = (authurl, username, pswd)
65     try:
66         client_args = dict(
67             auth_url=os.environ.get('OS_AUTH_URL',
68                                     "http://192.168.204.1:5000/v3"),
69             username=os.environ.get('OS_USERNAME', "admin"),
70             api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
71             project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
72         )
73         # dc_client_args = dict(
74         #     auth_url=os.environ['OS_AUTH_URL'],
75         #     username=os.environ['OS_USERNAME'],
76         #     api_key=os.environ['OS_PASSWORD'],
77         #     project_name=os.environ['OS_PROJECT_NAME'],
78         #     user_domain_name=os.environ['OS_USER_DOMAIN_NAME'],
79         #     project_domain_name=os.environ['OS_PROJECT_NAME'],
80         #     project_domain_id=os.environ['OS_PROJECT_DOMAIN_ID']
81         # )
82     except KeyError:
83         logger.error('Please source your RC file before execution, '
84                      'e.g.: `source ~/downloads/admin-rc.sh`')
85         sys.exit(1)
86
87     os_client_args = {}
88     for key, val in client_args.items():
89         os_client_args['os_{key}'.format(key=key)] = val
90     os_client_args['os_password'] = os_client_args.pop('os_api_key')
91     os_client_args['os_region_name'] = 'RegionOne'
92     os_client_args['api_version'] = 1
93     return os_client_args