Fix bug 1231: k8scluster-list now shows the project name it belongs to 59/9859/3
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 23 Sep 2020 11:57:27 +0000 (11:57 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 26 Nov 2020 15:14:10 +0000 (15:14 +0000)
Similar code was already used in different places, so it has been
properly refactored through the function get_project

Change-Id: I5b1299f27ca3e2eab3b3ba109a09ad8e66abae5b
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/scripts/osm.py

index 71506cd..1df00d6 100755 (executable)
@@ -69,6 +69,21 @@ def check_client_version(obj, what, version='sol005'):
     return
 
 
     return
 
 
+def get_project(project_list, item):
+    # project_list = ctx.obj.project.list()
+    item_project_list = item.get('_admin').get('projects_read')
+    project_id = 'None'
+    project_name = 'None'
+    if item_project_list:
+        for p1 in item_project_list:
+            project_id = p1
+            for p2 in project_list:
+                if p2['_id'] == project_id:
+                    project_name = p2['name']
+                    return project_id, project_name
+    return project_id, project_name
+
+
 @click.group(context_settings=dict(help_option_names=['-h', '--help'], max_content_width=160))
 @click.option('--hostname',
               default="127.0.0.1",
 @click.group(context_settings=dict(help_option_names=['-h', '--help'], max_content_width=160))
 @click.option('--hostname',
               default="127.0.0.1",
@@ -2573,29 +2588,22 @@ def vim_list(ctx, filter, long):
 #        resp = ctx.obj.vim.list(ro_update)
     if long:
         table = PrettyTable(['vim name', 'uuid', 'project', 'operational state', 'error details'])
 #        resp = ctx.obj.vim.list(ro_update)
     if long:
         table = PrettyTable(['vim name', 'uuid', 'project', 'operational state', 'error details'])
+        project_list = ctx.obj.project.list()
     else:
         table = PrettyTable(['vim name', 'uuid', 'operational state'])
     for vim in resp:
         if long:
     else:
         table = PrettyTable(['vim name', 'uuid', 'operational state'])
     for vim in resp:
         if long:
-            vim_details = ctx.obj.vim.get(vim['uuid'])
-            if 'vim_password' in vim_details:
-                vim_details['vim_password']='********'
-            logger.debug('VIM details: {}'.format(yaml.safe_dump(vim_details)))
-            vim_state = vim_details['_admin'].get('operationalState', '-')
+            if 'vim_password' in vim:
+                vim['vim_password']='********'
+            logger.debug('VIM details: {}'.format(yaml.safe_dump(vim)))
+            vim_state = vim['_admin'].get('operationalState', '-')
             error_details = 'N/A'
             if vim_state == 'ERROR':
             error_details = 'N/A'
             if vim_state == 'ERROR':
-                error_details = vim_details['_admin'].get('detailed-status', 'Not found')
-            project_list = ctx.obj.project.list()
-            vim_project_list = vim_details.get('_admin').get('projects_read')
-            project_id = 'None'
-            project_name = 'None'
-            if vim_project_list:
-                project_id = vim_project_list[0]
-                for p in project_list:
-                    if p['_id'] == project_id:
-                        project_name = p['name']
-                        break
-            table.add_row([vim['name'], vim['uuid'], '{} ({})'.format(project_name, project_id),
+                error_details = vim['_admin'].get('detailed-status', 'Not found')
+            project_id, project_name = get_project(project_list, vim)
+            #project_info = '{} ({})'.format(project_name, project_id)
+            project_info = project_name
+            table.add_row([vim['name'], vim['uuid'], project_info,
                           vim_state, wrap_text(text=error_details, width=80)])
         else:
             table.add_row([vim['name'], vim['uuid'], vim['_admin'].get('operationalState', '-')])
                           vim_state, wrap_text(text=error_details, width=80)])
         else:
             table.add_row([vim['name'], vim['uuid'], vim['_admin'].get('operationalState', '-')])
@@ -3142,8 +3150,9 @@ def k8scluster_list(ctx, filter, literal, long):
         print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     if long:
         print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     if long:
-        table = PrettyTable(['Name', 'Id', 'Version', 'VIM', 'K8s-nets', 'Operational State', 'Op. state (details)',
-                             'Description', 'Detailed status'])
+        table = PrettyTable(['Name', 'Id', 'Project', 'Version', 'VIM', 'K8s-nets',
+                             'Operational State', 'Op. state (details)', 'Description', 'Detailed status'])
+        project_list = ctx.obj.project.list()
     else:
         table = PrettyTable(['Name', 'Id', 'VIM', 'Operational State', 'Op. state details'])
     for cluster in resp:
     else:
         table = PrettyTable(['Name', 'Id', 'VIM', 'Operational State', 'Op. state details'])
     for cluster in resp:
@@ -3151,10 +3160,16 @@ def k8scluster_list(ctx, filter, literal, long):
                            cluster["_admin"].get("helm-chart", "-").get("operationalState", "-"),
                            cluster["_admin"].get("juju-bundle", "-").get("operationalState", "-"))
         if long:
                            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'],
+            logger.debug('Cluster details: {}'.format(yaml.safe_dump(cluster)))
+            project_id, project_name = get_project(project_list, cluster)
+            #project_info = '{} ({})'.format(project_name, project_id)
+            project_info = project_name
+            detailed_status = cluster["_admin"].get("detailed-status","-")
+            table.add_row([cluster['name'], cluster['_id'], project_info,
+                           cluster['k8s_version'], cluster['vim_account'],
                            json.dumps(cluster['nets']), cluster["_admin"]["operationalState"],
                            json.dumps(cluster['nets']), cluster["_admin"]["operationalState"],
-                           op_state_details, trunc_text(cluster.get('description') or '', 40), detailed_status])
+                           op_state_details, trunc_text(cluster.get('description') or '', 40),
+                           wrap_text(text=detailed_status, width=40)])
         else:
             table.add_row([cluster['name'], cluster['_id'], cluster['vim_account'],
                            cluster["_admin"]["operationalState"], op_state_details])
         else:
             table.add_row([cluster['name'], cluster['_id'], cluster['vim_account'],
                            cluster["_admin"]["operationalState"], op_state_details])