Pull JSON schemas at build/test not run time
[dcaegen2/platform.git] / mod / onboardingapi / dcae_cli / util / config.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 """
23 Provides dcae cli config utilities
24 """
25 import os, re
26
27 import click
28 import six
29
30 from dcae_cli import util
31 from dcae_cli import _version
32 from dcae_cli.util import get_app_dir, get_pref, update_pref, write_pref, pref_exists
33
34
35 class ConfigurationInitError(RuntimeError):
36     pass
37
38 def get_config_path():
39     '''Returns the absolute configuration file path'''
40     return os.path.join(get_app_dir(), 'config.json')
41
42
43 def _init_config_server_url():
44     return click.prompt("Please enter the remote server url", type=str).strip()
45
46 def _init_config_db_url():
47     click.echo("Now we need to set up access to the onboarding catalog")
48     hostname = click.prompt("Please enter the onboarding catalog hostname").strip()
49     user = click.prompt("Please enter the onboarding catalog user").strip()
50     password = click.prompt("Please enter the onboarding catalog password").strip()
51     return "postgresql://{user}:{password}@{hostname}:5432/dcae_onboarding_db".format(
52             hostname=hostname, user=user, password=password)
53
54 def _init_config():
55     '''Returns an initial dict for populating the config'''
56     # Grab the remote config and merge it in
57     new_config = {}
58
59     try:
60         server_url = _init_config_server_url()
61         new_config = util.fetch_file_from_web(server_url, "/dcae-cli/config.json")
62     except:
63         # Failing to pull seed configuration from remote server is not considered
64         # a problem. Just continue and give user the option to set it up
65         # themselves.
66         if not click.confirm("Could not download initial configuration from remote server. Attempt manually setting up?"):
67             raise ConfigurationInitError("Could not setup dcae-cli configuration")
68
69     # UPDATE: Keeping the server url even though the config was not found there.
70     new_config["server_url"] = server_url
71
72     if "db_url" not in new_config or not new_config["db_url"]:
73         # The seed configuration was not provided so manually set up the db
74         # connection
75         new_config["db_url"] = _init_config_db_url()
76
77     if "active_profile" not in new_config:
78         # The seed configuration was not provided which means the profiles will
79         # be the same. The profile will be hardcoded to a an empty default.
80         new_config["active_profile"] = "default"
81
82     return new_config
83
84
85 def get_config():
86     '''Returns the configuration dictionary'''
87     return get_pref(get_config_path(), _init_config)
88
89 def get_server_url():
90     """Returns the remote server url
91
92     The remote server holds the artifacts that the dcae-cli requires like the
93     seed config json and seed profiles json, and json schemas.
94     """
95     return get_config().get("server_url")
96
97 def get_docker_logins_key():
98     """Returns the Consul key that Docker logins are stored under
99
100     Default is "docker_plugin/docker_logins" which matches up with the docker
101     plugin default.
102     """
103     return get_config().get("docker_logins_key", "docker_plugin/docker_logins")
104
105 # These functions are used to fetch the configurable path to the various json
106 # schema files used in validation.
107
108 def get_path_component_spec():
109     return get_config().get("path_component_spec",
110             "/schemas/component-specification/dcae-cli-v2/component-spec-schema.json")
111
112 def get_path_data_format():
113     return get_config().get("path_data_format",
114             "/schemas/data-format/dcae-cli-v1/data-format-schema.json")
115
116 def get_active_profile():
117     return get_config().get("active_profile", None)
118
119
120 def update_config(**kwargs):
121     '''Updates and returns the configuration dictionary'''
122     return update_pref(path=get_config_path(), init_func=get_config, **kwargs)
123
124
125 def _reinit_config(init_func):
126     new_config = init_func()
127     config_path = get_config_path()
128
129     if  pref_exists(config_path):
130         existing_config = get_config()
131         # Make sure to clobber existing values and not other way
132         existing_config.update(new_config)
133         new_config = existing_config
134
135     write_pref(new_config, config_path)
136     return new_config
137
138 def reinit_config():
139     return _reinit_config(_init_config)