1 # ============LICENSE_START=======================================================
3 # ================================================================================
4 # Copyright (c) 2017 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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 # -*- coding: utf-8 -*-
23 Provides dcae cli config utilities
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
35 class ConfigurationInitError(RuntimeError):
38 def get_config_path():
39 '''Returns the absolute configuration file path'''
40 return os.path.join(get_app_dir(), 'config.json')
43 def _init_config_user():
45 user = click.prompt('Please enter your user id', type=str).strip()
47 # There should be no special characters
48 if re.match("(?:\w*)\Z", user):
51 click.echo("Invalid user id. Please try again.")
53 def _init_config_server_url():
54 return click.prompt('Please enter the remote server url', type=str).strip()
57 '''Returns an initial dict for populating the config'''
58 # Grab the remote config and merge it in
60 server_url = _init_config_server_url()
61 new_config = util.fetch_file_from_web(server_url, "/dcae-cli/config.json")
62 new_config["server_url"] = server_url
64 # REVIEW: Should we allow users to manually setup their config if not
65 # able to pull from remote server?
66 raise ConfigurationInitError("Could not download configuration from remote server")
68 new_config["user"] = _init_config_user()
69 new_config["cli_version"] = _version.__version__
71 if "db_url" not in new_config or not new_config["db_url"]:
72 # Really you should never get to this point because the remote config
73 # should have a postgres db url.
74 fallback = ''.join(('sqlite:///', os.path.join(get_app_dir(), 'dcae_cli.db')))
75 new_config["db_url"] = fallback
80 def should_force_reinit(config):
81 """Configs older than 2.0.0 should be replaced"""
82 ver = config.get("cli_version", "0.0.0")
83 return int(ver.split(".")[0]) < 2
86 '''Returns the configuration dictionary'''
87 return get_pref(get_config_path(), _init_config)
90 """Returns the remote server url
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.
95 return get_config().get("server_url")
97 # These functions are used to fetch the configurable path to the various json
98 # schema files used in validation.
100 def get_path_component_spec():
101 return get_config().get("path_component_spec",
102 "/schemas/component-specification/dcae-cli-v1/component-spec-schema.json")
104 def get_path_data_format():
105 return get_config().get("path_data_format",
106 "/schemas/data-format/dcae-cli-v1/data-format-schema.json")
108 def get_active_profile():
109 return get_config().get("active_profile", None)
112 def update_config(**kwargs):
113 '''Updates and returns the configuration dictionary'''
114 return update_pref(path=get_config_path(), init_func=get_config, **kwargs)
117 def _reinit_config(init_func):
118 new_config = init_func()
119 config_path = get_config_path()
121 if pref_exists(config_path):
122 existing_config = get_config()
123 # Make sure to clobber existing values and not other way
124 existing_config.update(new_config)
125 new_config = existing_config
127 write_pref(new_config, config_path)
131 return _reinit_config(_init_config)