Fix bug 1230: show Helm and Juju status when listing K8s clusters
[osm/osmclient.git] / osmclient / scripts / osm.py
index 2cca98d..71506cd 100755 (executable)
@@ -469,28 +469,6 @@ def nsd_list2(ctx, filter, long):
     nsd_list(ctx, filter, long)
 
 
-def pkg_repo_list(ctx, pkgtype, filter, repo, long):
-    if filter:
-        filter='&'.join(filter)
-    resp = ctx.obj.osmrepo.pkg_list(pkgtype, filter, repo)
-    if long:
-        table = PrettyTable(['nfpkg name', 'vendor', 'version', 'latest', 'description', 'repository'])
-    else:
-        table = PrettyTable(['nfpkg name', 'repository'])
-    for vnfd in resp:
-        name = vnfd.get('name', '-')
-        repository = vnfd.get('repository')
-        if long:
-            vendor = vnfd.get('vendor')
-            version = vnfd.get('version')
-            description = vnfd.get('description')
-            latest = vnfd.get('latest')
-            table.add_row([name, vendor, version, latest, description, repository])
-        else:
-            table.add_row([name, repository])
-        table.align = 'l'
-    print(table)
-
 def vnfd_list(ctx, nf_type, filter, long):
     logger.debug("")
     if nf_type:
@@ -568,6 +546,7 @@ def vnfd_list2(ctx, nf_type, filter, long):
     logger.debug("")
     vnfd_list(ctx, nf_type, filter, long)
 
+
 @cli_osm.command(name='nfpkg-list', short_help='list all xNF packages (VNF, HNF, PNF)')
 @click.option('--nf_type', help='type of NF (vnf, pnf, hnf)')
 @click.option('--filter', default=None, multiple=True,
@@ -584,6 +563,30 @@ def nfpkg_list(ctx, nf_type, filter, long):
     #     print(str(e))
     #     exit(1)
 
+
+def pkg_repo_list(ctx, pkgtype, filter, repo, long):
+    if filter:
+        filter='&'.join(filter)
+    resp = ctx.obj.osmrepo.pkg_list(pkgtype, filter, repo)
+    if long:
+        table = PrettyTable(['nfpkg name', 'vendor', 'version', 'latest', 'description', 'repository'])
+    else:
+        table = PrettyTable(['nfpkg name', 'repository'])
+    for vnfd in resp:
+        name = vnfd.get('name', '-')
+        repository = vnfd.get('repository')
+        if long:
+            vendor = vnfd.get('vendor')
+            version = vnfd.get('version')
+            description = vnfd.get('description')
+            latest = vnfd.get('latest')
+            table.add_row([name, vendor, version, latest, description, repository])
+        else:
+            table.add_row([name, repository])
+        table.align = 'l'
+    print(table)
+
+
 @cli_osm.command(name='vnfpkg-repo-list', short_help='list all xNF from OSM repositories')
 @click.option('--filter', default=None, multiple=True,
               help='restricts the list to the NFpkg matching the filter')
@@ -596,6 +599,7 @@ def nfpkg_repo_list1(ctx, filter, repo, long):
     pkgtype = 'vnf'
     pkg_repo_list(ctx, pkgtype, filter, repo, long)
 
+
 @cli_osm.command(name='nfpkg-repo-list', short_help='list all xNF from OSM repositories')
 @click.option('--filter', default=None, multiple=True,
               help='restricts the list to the NFpkg matching the filter')
@@ -608,6 +612,33 @@ def nfpkg_repo_list2(ctx, filter, repo, long):
     pkgtype = 'vnf'
     pkg_repo_list(ctx, pkgtype, filter, repo, long)
 
+
+@cli_osm.command(name='nsd-repo-list', short_help='list all NS from OSM repositories')
+@click.option('--filter', default=None, multiple=True,
+              help='restricts the list to the NS matching the filter')
+@click.option('--repo', default=None,
+              help='restricts the list to a particular OSM repository')
+@click.option('--long', is_flag=True, help='get more details')
+@click.pass_context
+def nspkg_repo_list(ctx, filter, repo, long):
+    """list xNF packages from OSM repositories"""
+    pkgtype = 'ns'
+    pkg_repo_list(ctx, pkgtype, filter, repo, long)
+
+
+@cli_osm.command(name='nspkg-repo-list', short_help='list all NS from OSM repositories')
+@click.option('--filter', default=None, multiple=True,
+              help='restricts the list to the NS matching the filter')
+@click.option('--repo', default=None,
+              help='restricts the list to a particular OSM repository')
+@click.option('--long', is_flag=True, help='get more details')
+@click.pass_context
+def nspkg_repo_list2(ctx, filter, repo, long):
+    """list xNF packages from OSM repositories"""
+    pkgtype = 'ns'
+    pkg_repo_list(ctx, pkgtype, filter, repo, long)
+
+
 def vnf_list(ctx, ns, filter, long):
     # try:
     if ns or filter:
@@ -671,29 +702,6 @@ def vnf_list1(ctx, ns, filter, long):
     logger.debug("")
     vnf_list(ctx, ns, filter, long)
 
-@cli_osm.command(name='nsd-repo-list', short_help='list all NS from OSM repositories')
-@click.option('--filter', default=None, multiple=True,
-              help='restricts the list to the NS matching the filter')
-@click.option('--repo', default=None,
-              help='restricts the list to a particular OSM repository')
-@click.option('--long', is_flag=True, help='get more details')
-@click.pass_context
-def nspkg_repo_list(ctx, filter, repo, long):
-    """list xNF packages from OSM repositories"""
-    pkgtype = 'ns'
-    pkg_repo_list(ctx, pkgtype, filter, repo, long)
-
-@cli_osm.command(name='nspkg-repo-list', short_help='list all NS from OSM repositories')
-@click.option('--filter', default=None, multiple=True,
-              help='restricts the list to the NS matching the filter')
-@click.option('--repo', default=None,
-              help='restricts the list to a particular OSM repository')
-@click.option('--long', is_flag=True, help='get more details')
-@click.pass_context
-def nspkg_repo_list2(ctx, filter, repo, long):
-    """list xNF packages from OSM repositories"""
-    pkgtype = 'ns'
-    pkg_repo_list(ctx, pkgtype, filter, repo, long)
 
 @cli_osm.command(name='nf-list', short_help='list all NF instances')
 @click.option('--ns', default=None, help='NS instance id or name to restrict the NF list')
@@ -2597,8 +2605,10 @@ def vim_list(ctx, filter, long):
 
 @cli_osm.command(name='vim-show', short_help='shows the details of a VIM account')
 @click.argument('name')
+@click.option('--filter', multiple=True,
+              help='restricts the information to the fields in the filter')
 @click.pass_context
-def vim_show(ctx, name):
+def vim_show(ctx, name, filter):
     """shows the details of a VIM account
 
     NAME: name or ID of the VIM account
@@ -2614,7 +2624,8 @@ def vim_show(ctx, name):
 
     table = PrettyTable(['key', 'attribute'])
     for k, v in list(resp.items()):
-        table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)])
+        if not filter or k in filter:
+            table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)])
     table.align = 'l'
     print(table)
 
@@ -3118,8 +3129,9 @@ def k8scluster_delete(ctx, name, force):
               help='restricts the list to the K8s clusters matching the filter')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
+@click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def k8scluster_list(ctx, filter, literal):
+def k8scluster_list(ctx, filter, literal, long):
     """list all K8s clusters"""
     # try:
     check_client_version(ctx.obj, ctx.command.name)
@@ -3129,11 +3141,23 @@ def k8scluster_list(ctx, filter, literal):
     if literal:
         print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
-    table = PrettyTable(['Name', 'Id', 'Version', 'VIM', 'K8s-nets', 'Operational State', 'Description'])
+    if long:
+        table = PrettyTable(['Name', 'Id', 'Version', 'VIM', 'K8s-nets', 'Operational State', 'Op. state (details)',
+                             'Description', 'Detailed status'])
+    else:
+        table = PrettyTable(['Name', 'Id', 'VIM', 'Operational State', 'Op. state details'])
     for cluster in resp:
-        table.add_row([cluster['name'], cluster['_id'], cluster['k8s_version'], cluster['vim_account'],
-                       json.dumps(cluster['nets']), cluster["_admin"]["operationalState"],
-                       trunc_text(cluster.get('description') or '', 40)])
+        op_state_details = "Helm: {}\nJuju: {}".format(
+                           cluster["_admin"].get("helm-chart", "-").get("operationalState", "-"),
+                           cluster["_admin"].get("juju-bundle", "-").get("operationalState", "-"))
+        if long:
+            detailed_status = cluster["_admin"].get("detailedStatus","-")
+            table.add_row([cluster['name'], cluster['_id'], cluster['k8s_version'], cluster['vim_account'],
+                           json.dumps(cluster['nets']), cluster["_admin"]["operationalState"],
+                           op_state_details, trunc_text(cluster.get('description') or '', 40), detailed_status])
+        else:
+            table.add_row([cluster['name'], cluster['_id'], cluster['vim_account'],
+                           cluster["_admin"]["operationalState"], op_state_details])
     table.align = 'l'
     print(table)
     # except ClientException as e: