Policy Reconfiguration, Component Spec, Help text
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / commands / catalog / commands.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017-2018 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 statuses")
36 #TODO: @click.argument('query')
37 @click.pass_obj
38 def action_list(obj, expanded):
39     """Lists resources in the onboarding catalog"""
40     # Query both components and data formats. Display both sets.
41
42     user, catalog = obj['config']['user'], obj['catalog']
43
44     only_latest = not expanded
45     only_published = not expanded
46
47     # TODO: Probably want to implement pagination
48     comps = catalog.list_components(latest=only_latest, only_published=only_published)
49     dfs = catalog.list_formats(latest=only_latest, only_published=only_published)
50
51     def format_record_component(obj):
52         when_published = obj["when_published"].date() \
53                 if obj["when_published"] else ""
54
55         return (obj["name"], obj["version"], obj["component_type"],
56                 util.format_description(obj["description"]), obj["owner"],
57                 util.get_status_string(obj), when_published)
58
59     comps = [ format_record_component(comp) for comp in comps ]
60
61     click.echo("")
62     click.echo("Components:")
63     click.echo(util.create_table(('Name', 'Version', 'Type', 'Description', 'Owner', 'Status',
64         'Published'), comps))
65
66     def format_record_format(obj):
67         when_published = obj["when_published"].date() \
68                 if obj["when_published"] else ""
69
70         return (obj["name"], obj["version"],
71                 util.format_description(obj["description"]), obj["owner"],
72                 util.get_status_string(obj), when_published)
73
74     dfs = [ format_record_format(df) for df in dfs ]
75
76     click.echo("")
77     click.echo("Data formats:")
78     click.echo(util.create_table(('Name', 'Version', 'Description', 'Owner', 'Status',
79         'Published'), dfs))
80
81
82 @catalog.command(name="show")
83 @click.argument("resource", metavar="name:version")
84 @click.pass_obj
85 def action_show(obj, resource):
86     """Provides more information about a resource"""
87     # Query both components and data formats. Display both sets.
88     name, ver = util.parse_input(resource)
89     catalog = obj['catalog']
90     spec = None
91
92     try:
93         spec = catalog.get_component_spec(name, ver)
94
95         click.echo("")
96         click.echo("Component specification")
97         click.echo("-----------------------")
98         click.echo(util.format_json(spec))
99         click.echo("")
100     except:
101         pass
102
103     try:
104         spec = obj['catalog'].get_format_spec(name, ver)
105
106         click.echo("")
107         click.echo("Data format")
108         click.echo("-----------")
109         click.echo(util.format_json(spec))
110         click.echo("")
111     except:
112         pass
113
114     if not spec:
115         click.echo("No matching component nor data format found")