Fix: the IMS and DMS endpoint URL not correct when started in helm chart
[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_interal = os.environ.get("API_HOST", "localhost")
32     host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
33     host = host_interal if host_external is None or host_external == '' \
34         else host_external
35
36     port_internal = 5005 if host == "localhost" else 80
37     port_external = 30205
38     port = port_internal if host_external is None or host_external == '' \
39         else port_external
40     return f"http://{host}:{port}"
41
42
43 def get_root_api_base():
44     return "/"
45
46
47 def get_o2ims_api_base():
48     return get_root_api_base() + 'o2ims_infrastructureInventory/v1'
49
50
51 def get_provision_api_base():
52     return get_root_api_base() + 'provision/v1'
53
54
55 def get_o2dms_api_base():
56     return get_root_api_base() + "o2dms/v1"
57
58
59 def get_redis_host_and_port():
60     host = os.environ.get("REDIS_HOST", "localhost")
61     port = 63791 if host == "localhost" else 6379
62     return dict(host=host, port=port)
63
64
65 def get_smo_o2endpoint():
66     smo_o2endpoint = os.environ.get(
67         "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
68     return smo_o2endpoint
69
70
71 def get_stx_access_info():
72     # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
73     # username = os.environ.get("STX_USERNAME", "admin")
74     # pswd = os.environ.get("STX_PASSWORD", "passwd1")
75     # stx_access_info = (authurl, username, pswd)
76     try:
77         client_args = dict(
78             auth_url=os.environ.get('OS_AUTH_URL',
79                                     "http://192.168.204.1:5000/v3"),
80             username=os.environ.get('OS_USERNAME', "admin"),
81             api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
82             project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
83         )
84         # dc_client_args = dict(
85         #     auth_url=os.environ['OS_AUTH_URL'],
86         #     username=os.environ['OS_USERNAME'],
87         #     api_key=os.environ['OS_PASSWORD'],
88         #     project_name=os.environ['OS_PROJECT_NAME'],
89         #     user_domain_name=os.environ['OS_USER_DOMAIN_NAME'],
90         #     project_domain_name=os.environ['OS_PROJECT_NAME'],
91         #     project_domain_id=os.environ['OS_PROJECT_DOMAIN_ID']
92         # )
93     except KeyError:
94         logger.error('Please source your RC file before execution, '
95                      'e.g.: `source ~/downloads/admin-rc.sh`')
96         sys.exit(1)
97
98     os_client_args = {}
99     for key, val in client_args.items():
100         os_client_args['os_{key}'.format(key=key)] = val
101     os_client_args['os_password'] = os_client_args.pop('os_api_key')
102     os_client_args['os_region_name'] = 'RegionOne'
103     os_client_args['api_version'] = 1
104     return os_client_args
105
106
107 def get_k8s_api_endpoint():
108     K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
109     K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
110     K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
111     return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
112
113
114 def get_helm_cli():
115     return '/usr/local/bin/helm'