summaryrefslogtreecommitdiffstats
path: root/dcae-cli/dcae_cli/commands/catalog/commands.py
blob: dc6b27a495f17e246ed09551db692c96af9ecb20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# ============LICENSE_START=======================================================
# org.onap.dcae
# ================================================================================
# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

"""
Queries onboarding catalog
"""
import click

from dcae_cli.commands import util


@click.group()
def catalog():
    pass


@catalog.command(name="list")
@click.option("--expanded", is_flag=True, default=False, help="Display the expanded view - show all versions and all statuses")
#TODO: @click.argument('query')
@click.pass_obj
def action_list(obj, expanded):
    """Lists resources in the onboarding catalog"""
    # Query both components and data formats. Display both sets.

    user, catalog = obj['config']['user'], obj['catalog']

    only_latest = not expanded
    only_published = not expanded

    # TODO: Probably want to implement pagination
    comps = catalog.list_components(latest=only_latest, only_published=only_published)
    dfs = catalog.list_formats(latest=only_latest, only_published=only_published)

    def format_record_component(obj):
        when_published = obj["when_published"].date() \
                if obj["when_published"] else ""

        return (obj["name"], obj["version"], obj["component_type"],
                util.format_description(obj["description"]), obj["owner"],
                util.get_status_string(obj), when_published)

    comps = [ format_record_component(comp) for comp in comps ]

    click.echo("")
    click.echo("Components:")
    click.echo(util.create_table(('Name', 'Version', 'Type', 'Description', 'Owner', 'Status',
        'Published'), comps))

    def format_record_format(obj):
        when_published = obj["when_published"].date() \
                if obj["when_published"] else ""

        return (obj["name"], obj["version"],
                util.format_description(obj["description"]), obj["owner"],
                util.get_status_string(obj), when_published)

    dfs = [ format_record_format(df) for df in dfs ]

    click.echo("")
    click.echo("Data formats:")
    click.echo(util.create_table(('Name', 'Version', 'Description', 'Owner', 'Status',
        'Published'), dfs))


@catalog.command(name="show")
@click.argument("resource", metavar="name:version")
@click.pass_obj
def action_show(obj, resource):
    """Provides more information about a resource"""
    # Query both components and data formats. Display both sets.
    name, ver = util.parse_input(resource)
    catalog = obj['catalog']
    spec = None

    try:
        spec = catalog.get_component_spec(name, ver)

        click.echo("")
        click.echo("Component specification")
        click.echo("-----------------------")
        click.echo(util.format_json(spec))
        click.echo("")
    except:
        pass

    try:
        spec = obj['catalog'].get_format_spec(name, ver)

        click.echo("")
        click.echo("Data format")
        click.echo("-----------")
        click.echo(util.format_json(spec))
        click.echo("")
    except:
        pass

    if not spec:
        click.echo("No matching component nor data format found")