44771fa818ee30d640e770ee3c5a1719701318ba
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / commands / catalog / 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 """
22 Queries onboarding catalog
23 """
24 import click
25
26 from dcae_cli.commands import util
27
28
29 @click.group()
30 def catalog():
31     pass
32
33
34 @catalog.command(name="list")
35 @click.option("--expanded", is_flag=True, default=False, help="Display the expanded view - show all versions and all status")
36 #TODO: @click.argument('query')
37 @click.pass_obj
38 def action_list(obj, expanded):
39     # Query both components and data formats. Display both sets.
40
41     user, catalog = obj['config']['user'], obj['catalog']
42
43     only_latest = not expanded
44     only_published = not expanded
45
46     # TODO: Probably want to implement pagination
47     comps = catalog.list_components(latest=only_latest, only_published=only_published)
48     dfs = catalog.list_formats(latest=only_latest, only_published=only_published)
49
50     def format_record_component(obj):
51         when_published = obj["when_published"].date() \
52                 if obj["when_published"] else ""
53
54         return (obj["name"], obj["version"], obj["component_type"],
55                 util.format_description(obj["description"]), obj["owner"],
56                 util.get_status_string(obj), when_published)
57
58     comps = [ format_record_component(comp) for comp in comps ]
59
60     click.echo("")
61     click.echo("Components:")
62     click.echo(util.create_table(('Name', 'Version', 'Type', 'Description', 'Owner', 'Status',
63         'Published'), comps))
64
65     def format_record_format(obj):
66         when_published = obj["when_published"].date() \
67                 if obj["when_published"] else ""
68
69         return (obj["name"], obj["version"],
70                 util.format_description(obj["description"]), obj["owner"],
71                 util.get_status_string(obj), when_published)
72
73     dfs = [ format_record_format(df) for df in dfs ]
74
75     click.echo("")
76     click.echo("Data formats:")
77     click.echo(util.create_table(('Name', 'Version', 'Description', 'Owner', 'Status',
78         'Published'), dfs))
79
80
81 @catalog.command(name="show")
82 @click.argument("resource", metavar="name:version")
83 @click.pass_obj
84 def action_show(obj, resource):
85     # Query both components and data formats. Display both sets.
86     name, ver = util.parse_input(resource)
87     catalog = obj['catalog']
88     spec = None
89
90     try:
91         spec = catalog.get_component_spec(name, ver)
92
93         click.echo("")
94         click.echo("Component specification")
95         click.echo("-----------------------")
96         click.echo(util.format_json(spec))
97         click.echo("")
98     except:
99         pass
100
101     try:
102         spec = obj['catalog'].get_format_spec(name, ver)
103
104         click.echo("")
105         click.echo("Data format")
106         click.echo("-----------")
107         click.echo(util.format_json(spec))
108         click.echo("")
109     except:
110         pass
111
112     if not spec:
113         click.echo("No matching component nor data format found")