Add dcae-cli and component-json-schemas projects
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / commands / data_format / commands.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
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
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 data format commands
24 """
25 import json
26
27 import click
28
29 from dcae_cli.util import load_json
30 from dcae_cli.util.logger import get_logger
31 from dcae_cli.commands import util
32 from dcae_cli.commands.util import create_table, parse_input
33
34 from dcae_cli.catalog.exc import MissingEntry
35
36
37 logger = get_logger('DataFormatCommand')
38
39
40 @click.group()
41 def data_format():
42     pass
43
44
45 @data_format.command()
46 @click.option('--update', is_flag=True, help='Updates a locally added data format if it has not been already pushed')
47 @click.argument('specification', type=click.Path(resolve_path=True, exists=True))
48 @click.pass_obj
49 def add(obj, update, specification):
50     '''Tracks a data format file SPECIFICATION locally but does not push to the catalog'''
51     spec = load_json(specification)
52     user, catalog = obj['config']['user'], obj['catalog']
53     catalog.add_format(spec, user, update)
54
55
56 @data_format.command(name='list')
57 @click.option('--latest', is_flag=True, help='Only list the latest version of data formats')
58 @click.pass_obj
59 def list_format(obj, latest):
60     """Lists all your data formats"""
61     user, catalog = obj['config']['user'], obj['catalog']
62     dfs = catalog.list_formats(latest, user=user)
63
64     def format_record(df):
65         return (df["name"], df["version"],
66                 util.format_description(df["description"]),
67                 util.get_status_string(df), df["modified"])
68
69     dfs = [ format_record(df) for df in dfs ]
70
71     click.echo("")
72     click.echo("Data formats for {0}".format(user))
73     click.echo(create_table(('Name', 'Version', 'Description', 'Status', 'Modified'), dfs))
74
75
76 @data_format.command()
77 @click.argument('data-format', metavar="name:version")
78 @click.pass_obj
79 def show(obj, data_format):
80     '''Provides more information about FORMAT'''
81     name, ver = parse_input(data_format)
82     spec = obj['catalog'].get_format_spec(name, ver)
83
84     click.echo(util.format_json(spec))
85
86
87 @data_format.command()
88 @click.argument('data-format')
89 @click.pass_obj
90 def publish(obj, data_format):
91     """Publishes data format to make publicly available"""
92     name, version = parse_input(data_format)
93     user, catalog = obj['config']['user'], obj['catalog']
94
95     if catalog.publish_format(user, name, version):
96         click.echo("Data format has been published")
97     else:
98         click.echo("Data format could not be published")