1 # ============LICENSE_START=======================================================
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
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 entry-level logic for building the CLI. Commands and heavy-lifting logic should be in their own module.
27 from dcae_cli import util
28 from dcae_cli.commands.catalog import catalog
29 from dcae_cli.commands.component import component
30 from dcae_cli.commands.data_format import data_format
31 from dcae_cli.commands.profiles import profiles
32 from dcae_cli.catalog import get_catalog
33 from dcae_cli.util.exc import DcaeException
34 from dcae_cli.util.logger import get_logger
35 from dcae_cli.util import config as conf
36 from dcae_cli.util import profiles as prof
39 log = get_logger('cli')
44 click.echo("Warning! Reinitializing your dcae-cli configuration")
47 prof.reinit_profiles()
48 except Exception as e:
49 raise DcaeException("Failed to reinitialize configuration: {0}".format(e))
51 def _reinit_callback(ctx, param, value):
52 """Callback used for the eager --reinit option"""
53 if not value or ctx.resilient_parsing:
56 click.echo("Reinitialize done")
62 @click.option('--verbose', '-v', is_flag=True, default=False, help='Prints INFO-level logs to screen.')
63 # This is following the same pattern as --version
64 # http://click.pocoo.org/5/options/#callbacks-and-eager-options
65 @click.option('--reinit', is_flag=True, callback=_reinit_callback, expose_value=False,
66 is_eager=True, help='Re-initialize dcae-cli configuration')
67 @click.version_option()
69 def cli(ctx, verbose):
74 if 'config' not in ctx.obj:
75 config = conf.get_config()
77 ctx.obj['config'] = config
79 config = ctx.obj['config']
81 if 'catalog' not in ctx.obj:
83 ctx.obj['catalog'] = get_catalog(**config)
84 except Exception as e:
86 raise DcaeException("Having issues connecting to the onboarding catalog")
89 util.logger.set_verbose()
92 @cli.command(name="http", help="Run HTTP API")
93 @click.option('--live', is_flag=True, default=False, help='Starts up the HTTP API in live mode which means it binds to 80')
95 def run_http_api(ctx, live):
96 catalog = ctx.obj['catalog']
97 should_debug = not live
98 # Importing http module has to be here otherwise unit tests will break
99 # because http module makes config calls when the module is loaded (global).
100 # Config calls must always be done lazily as much as possible in order for the
101 # mock_cli_config pytest.fixture to kick in.
102 from dcae_cli import http
103 http.start_http_server(catalog, debug=should_debug)
107 cli.add_command(catalog)
108 cli.add_command(component)
109 cli.add_command(data_format)
110 cli.add_command(profiles)