Enhance: Enable O2 IMS for distributed cloud
[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 from urllib.parse import urlparse
18
19 from o2common.helper import o2logging
20 logger = o2logging.get_logger(__name__)
21
22
23 _DEFAULT_DCMANAGER_URL = "http://192.168.204.1:8119/v1.0"
24 _DEFAULT_STX_URL = "http://192.168.204.1:5000/v3"
25
26
27 def get_postgres_uri():
28     host = os.environ.get("DB_HOST", "localhost")
29     port = 54321 if host == "localhost" else 5432
30     password = os.environ.get("DB_PASSWORD", "o2ims123")
31     user, db_name = "o2ims", "o2ims"
32     return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
33
34
35 def get_api_url():
36     host_interal = os.environ.get("API_HOST", "localhost")
37     host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
38     host = host_interal if host_external is None or host_external == '' \
39         else host_external
40
41     port_internal = 5005 if host == "localhost" else 80
42     port_external = 30205
43     port = port_internal if host_external is None or host_external == '' \
44         else port_external
45     return f"http://{host}:{port}"
46
47
48 def get_root_api_base():
49     return "/"
50
51
52 def get_o2ims_api_base():
53     return get_root_api_base() + 'o2ims_infrastructureInventory/v1'
54
55
56 def get_provision_api_base():
57     return get_root_api_base() + 'provision/v1'
58
59
60 def get_o2dms_api_base():
61     return get_root_api_base() + "o2dms/v1"
62
63
64 def get_redis_host_and_port():
65     host = os.environ.get("REDIS_HOST", "localhost")
66     port = 63791 if host == "localhost" else 6379
67     return dict(host=host, port=port)
68
69
70 def get_smo_o2endpoint():
71     smo_o2endpoint = os.environ.get(
72         "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
73     return smo_o2endpoint
74
75
76 def get_stx_access_info(region_name="RegionOne", subcloud_hostname: str = ""):
77     # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
78     # username = os.environ.get("STX_USERNAME", "admin")
79     # pswd = os.environ.get("STX_PASSWORD", "passwd1")
80     # stx_access_info = (authurl, username, pswd)
81     try:
82         client_args = dict(
83             auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
84             username=os.environ.get('OS_USERNAME', "admin"),
85             api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
86             project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
87         )
88         # dc_client_args = dict(
89         #     auth_url=os.environ['OS_AUTH_URL'],
90         #     username=os.environ['OS_USERNAME'],
91         #     api_key=os.environ['OS_PASSWORD'],
92         #     project_name=os.environ['OS_PROJECT_NAME'],
93         #     user_domain_name=os.environ['OS_USER_DOMAIN_NAME'],
94         #     project_domain_name=os.environ['OS_PROJECT_NAME'],
95         #     project_domain_id=os.environ['OS_PROJECT_DOMAIN_ID']
96         # )
97     except KeyError:
98         logger.error('Please source your RC file before execution, '
99                      'e.g.: `source ~/downloads/admin-rc.sh`')
100         sys.exit(1)
101
102     os_client_args = {}
103     for key, val in client_args.items():
104         os_client_args['os_{key}'.format(key=key)] = val
105     if "" != subcloud_hostname:
106         orig_auth_url = urlparse(_DEFAULT_STX_URL)
107         new_auth_url = orig_auth_url._replace(
108             netloc=orig_auth_url.netloc.replace(
109                 orig_auth_url.hostname, subcloud_hostname))
110         # new_auth_url = new_auth_url._replace(
111         #     netloc=new_auth_url.netloc.replace(str(new_auth_url.port),
112         # "18002"))
113         new_auth_url = new_auth_url._replace(
114             scheme=new_auth_url.scheme.
115             replace(new_auth_url.scheme, 'https'))
116         os_client_args['os_auth_url'] = new_auth_url.geturl()
117         os_client_args['os_endpoint_type'] = 'public'
118         os_client_args['insecure'] = True
119     # os_client_args['system_url'] = os_client_args['os_auth_url']
120     os_client_args['os_password'] = os_client_args.pop('os_api_key')
121     os_client_args['os_region_name'] = region_name
122     os_client_args['api_version'] = 1
123     # os_client_args['user_domain_name'] = 'Default'
124     # os_client_args['project_domain_name'] = 'Default'
125     return os_client_args
126
127
128 def get_dc_access_info():
129     try:
130         client_args = dict(
131             auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
132             username=os.environ.get('OS_USERNAME', "admin"),
133             api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
134             project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
135         )
136     except KeyError:
137         logger.error('Please source your RC file before execution, '
138                      'e.g.: `source ~/downloads/admin-rc.sh`')
139         sys.exit(1)
140
141     os_client_args = {}
142     for key, val in client_args.items():
143         os_client_args['os_{key}'.format(key=key)] = val
144     auth_url = urlparse(os_client_args.pop('os_auth_url'))
145     dcmanager_url = urlparse(_DEFAULT_DCMANAGER_URL)
146     dcmanager_url = dcmanager_url._replace(netloc=dcmanager_url.netloc.replace(
147         dcmanager_url.hostname, auth_url.hostname))
148
149     os_client_args['dcmanager_url'] = dcmanager_url.geturl()
150     os_client_args['auth_url'] = auth_url.geturl()
151     os_client_args['username'] = os_client_args.pop('os_username')
152     os_client_args['api_key'] = os_client_args.pop('os_api_key')
153     os_client_args['project_name'] = os_client_args.pop('os_project_name')
154     os_client_args['user_domain_name'] = 'Default'
155     os_client_args['project_domain_name'] = 'Default'
156
157     return os_client_args
158
159
160 def get_k8s_api_endpoint():
161     K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
162     K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
163     K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
164     return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
165
166
167 def get_helm_cli():
168     return '/usr/local/bin/helm'
169
170
171 def get_system_controller_as_respool():
172     return True