Pull JSON schemas at build/test not run time
[dcaegen2/platform.git] / mod / onboardingapi / dcae_cli / cli.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 entry-level logic for building the CLI. Commands and heavy-lifting logic should be in their own module.
24 """
25 import click
26
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
37
38
39 log = get_logger('cli')
40
41
42 def _reinit_cli():
43     """Reinit cli"""
44     click.echo("Warning! Reinitializing your dcae-cli configuration")
45     try:
46         conf.reinit_config()
47         prof.reinit_profiles()
48     except Exception as e:
49         raise DcaeException("Failed to reinitialize configuration: {0}".format(e))
50
51 def _reinit_callback(ctx, param, value):
52     """Callback used for the eager --reinit option"""
53     if not value or ctx.resilient_parsing:
54         return
55     _reinit_cli()
56     click.echo("Reinitialize done")
57     ctx.exit()
58
59
60
61 @click.group()
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()
68 @click.pass_context
69 def cli(ctx, verbose):
70
71     if ctx.obj is None:
72         ctx.obj = dict()
73
74     if 'config' not in ctx.obj:
75         config = conf.get_config()
76
77         ctx.obj['config'] = config
78     else:
79         config = ctx.obj['config']
80
81     if 'catalog' not in ctx.obj:
82         try:
83             ctx.obj['catalog'] = get_catalog(**config)
84         except Exception as e:
85             log.error(e)
86             raise DcaeException("Having issues connecting to the onboarding catalog")
87
88     if verbose:
89         util.logger.set_verbose()
90
91
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')
94 @click.pass_context
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)
104
105
106
107 cli.add_command(catalog)
108 cli.add_command(component)
109 cli.add_command(data_format)
110 cli.add_command(profiles)