Solved bug indicating vdu_count for osm-action command
[osm/osmclient.git] / osmclient / scripts / osm.py
index 402a09b..06d77e0 100755 (executable)
@@ -290,7 +290,7 @@ def ns_list(ctx, filter, long):
             if ee['elementType'] not in status_ee:
                 status_ee[ee['elementType']] = {}
                 status_ee[ee['elementType']][ee['status']] = 1
-                continue;
+                continue
             if ee['status'] in status_ee[ee['elementType']]:
                 status_ee[ee['elementType']][ee['status']] += 1
             else:
@@ -368,7 +368,8 @@ def ns_list(ctx, filter, long):
             else:
                 current_operation = "{} ({})".format(nsr['_admin'].get('current-operation','-'), nsr['_admin']['nslcmop'])
             error_details = "N/A"
-            if ns_state == "BROKEN" or ns_state == "DEGRADED" or nsr.get('errorDescription'):
+            if ns_state == "BROKEN" or ns_state == "DEGRADED" or \
+                ('currentOperation' not in nsr and nsr.get('errorDescription')):
                 error_details = "{}\nDetail: {}".format(nsr['errorDescription'], nsr['errorDetail'])
         else:
             nsopdata = ctx.obj.ns.get_opdata(ns['id'])
@@ -561,18 +562,6 @@ def vnfd_list2(ctx, nf_type, filter, long):
     logger.debug("")
     vnfd_list(ctx, nf_type, filter, long)
 
-@cli_osm.command(name='vnfpkg-repo-list', short_help='list all xNF from OSM repositories')
-@click.option('--filter', default=None,
-              help='restricts the list to the NFpkg 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 vnfd_list3(ctx, filter, repo, long):
-    """list xNF packages from OSM repositories"""
-    pkgtype = 'vnf'
-    pkg_repo_list(ctx, pkgtype, filter, repo, 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,
@@ -589,6 +578,18 @@ def nfpkg_list(ctx, nf_type, filter, long):
     #     print(str(e))
     #     exit(1)
 
+@cli_osm.command(name='vnfpkg-repo-list', short_help='list all xNF from OSM repositories')
+@click.option('--filter', default=None,
+              help='restricts the list to the NFpkg 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 nfpkg_repo_list1(ctx, filter, repo, long):
+    """list xNF packages from OSM repositories"""
+    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,
               help='restricts the list to the NFpkg matching the filter')
@@ -596,7 +597,7 @@ def nfpkg_list(ctx, nf_type, filter, long):
               help='restricts the list to a particular OSM repository')
 @click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def vnfd_list4(ctx, filter, repo, long):
+def nfpkg_repo_list2(ctx, filter, repo, long):
     """list xNF packages from OSM repositories"""
     pkgtype = 'vnf'
     pkg_repo_list(ctx, pkgtype, filter, repo, long)
@@ -670,7 +671,7 @@ def vnf_list1(ctx, ns, filter, long):
               help='restricts the list to a particular OSM repository')
 @click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def nsd_list3(ctx, filter, repo, long):
+def nspkg_repo_list(ctx, filter, repo, long):
     """list xNF packages from OSM repositories"""
     pkgtype = 'ns'
     pkg_repo_list(ctx, pkgtype, filter, repo, long)
@@ -682,7 +683,7 @@ def nsd_list3(ctx, filter, repo, long):
               help='restricts the list to a particular OSM repository')
 @click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def nspkg_list(ctx, filter, repo, long):
+def nspkg_repo_list2(ctx, filter, repo, long):
     """list xNF packages from OSM repositories"""
     pkgtype = 'ns'
     pkg_repo_list(ctx, pkgtype, filter, repo, long)
@@ -786,12 +787,12 @@ def ns_op_list(ctx, name, long):
         if op['lcmOperationType']=='action':
             action_name = op['operationParams']['primitive']
         detail = "-"
-        if op['operationState']=='PROCESSING':
-            if op['lcmOperationType'] in ('instantiate', 'terminate'):
+        if op['operationState'] == 'PROCESSING':
+            if op['queuePosition'] is not None and op['queuePosition'] > 0:
+                detail = "In queue. Current position: {}".format(op['queuePosition'])
+            elif op['lcmOperationType'] in ('instantiate', 'terminate'):
                 if op['stage']:
                     detail = op['stage']
-            else:
-                detail = "In queue. Current position: {}".format(op['queuePosition'])
         elif op['operationState'] in ('FAILED', 'FAILED_TEMP'):
             detail = op.get('errorMessage','-')
         date = datetime.fromtimestamp(op['startTime']).strftime("%Y-%m-%dT%H:%M:%S")
@@ -992,7 +993,7 @@ def nsd_show(ctx, name, literal):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1002,7 +1003,7 @@ def nsd_show(ctx, name, literal):
     print(table)
 
 
-@cli_osm.command(name='nsd-show', short_help='shows the content of a NSD')
+@cli_osm.command(name='nsd-show', short_help='shows the details of a NS package')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.argument('name')
@@ -1016,7 +1017,7 @@ def nsd_show1(ctx, name, literal):
     nsd_show(ctx, name, literal)
 
 
-@cli_osm.command(name='nspkg-show', short_help='shows the content of a NSD')
+@cli_osm.command(name='nspkg-show', short_help='shows the details of a NS package')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.argument('name')
@@ -1040,7 +1041,7 @@ def vnfd_show(ctx, name, literal):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1056,7 +1057,7 @@ def pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal):
     resp = ctx.obj.osmrepo.pkg_get(pkgtype, name, repo, version, filter)
 
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     pkgtype += 'd'
     catalog = pkgtype + '-catalog'
@@ -1072,7 +1073,7 @@ def pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal):
     table.align = 'l'
     print(table)
 
-@cli_osm.command(name='vnfd-show', short_help='shows the content of a VNFD')
+@cli_osm.command(name='vnfd-show', short_help='shows the details of a NF package')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.argument('name')
@@ -1086,7 +1087,7 @@ def vnfd_show1(ctx, name, literal):
     vnfd_show(ctx, name, literal)
 
 
-@cli_osm.command(name='vnfpkg-show', short_help='shows the content of a VNFD')
+@cli_osm.command(name='vnfpkg-show', short_help='shows the details of a NF package')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.argument('name')
@@ -1099,7 +1100,7 @@ def vnfd_show2(ctx, name, literal):
     logger.debug("")
     vnfd_show(ctx, name, literal)
 
-@cli_osm.command(name='vnfpkg-repo-show', short_help='shows the content of a VNFD')
+@cli_osm.command(name='vnfpkg-repo-show', short_help='shows the details of a NF package in an OSM repository')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.option('--repo',
@@ -1121,7 +1122,7 @@ def vnfd_show3(ctx, name, repo, version, literal=None, filter=None):
     pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal)
 
 
-@cli_osm.command(name='nsd-repo-show', short_help='shows the content of a NSD')
+@cli_osm.command(name='nsd-repo-show', short_help='shows the details of a NS package in an OSM repository')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.option('--repo',
@@ -1142,7 +1143,7 @@ def nsd_repo_show(ctx, name, repo, version, literal=None, filter=None):
     pkgtype = 'ns'
     pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal)
 
-@cli_osm.command(name='nspkg-repo-show', short_help='shows the content of a NSD')
+@cli_osm.command(name='nspkg-repo-show', short_help='shows the details of a NS package in an OSM repository')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.option('--repo',
@@ -1163,7 +1164,7 @@ def nsd_repo_show2(ctx, name, repo, version, literal=None, filter=None):
     pkgtype = 'ns'
     pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal)
 
-@cli_osm.command(name='nfpkg-show', short_help='shows the content of a NF Descriptor')
+@cli_osm.command(name='nfpkg-show', short_help='shows the details of a NF package')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.argument('name')
@@ -1177,7 +1178,7 @@ def nfpkg_show(ctx, name, literal):
     vnfd_show(ctx, name, literal)
 
 
-@cli_osm.command(name='nfpkg-repo-show', short_help='shows the content of a VNFD')
+@cli_osm.command(name='nfpkg-repo-show', short_help='shows the details of a NF package in an OSM repository')
 @click.option('--literal', is_flag=True,
               help='print literally, no pretty table')
 @click.option('--repo',
@@ -1218,7 +1219,7 @@ def ns_show(ctx, name, literal, filter):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(ns))
+        print(yaml.safe_dump(ns, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1305,9 +1306,10 @@ def vnf_show(ctx, name, literal, filter, kdu):
             time.sleep(5)
             t += 5
         print ("Could not determine KDU status")
+        return
 
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1388,7 +1390,7 @@ def ns_op_show(ctx, id, filter, literal):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(op_info))
+        print(yaml.safe_dump(op_info, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1410,7 +1412,7 @@ def nst_show(ctx, name, literal):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1458,7 +1460,7 @@ def nsi_show(ctx, name, literal, filter):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(nsi))
+        print(yaml.safe_dump(nsi, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -1564,7 +1566,7 @@ def pdu_show(ctx, name, literal, filter):
     #     exit(1)
 
     if literal:
-        print(yaml.safe_dump(pdu))
+        print(yaml.safe_dump(pdu, indent=4, default_flow_style=False))
         return
 
     table = PrettyTable(['field', 'value'])
@@ -3091,13 +3093,13 @@ def k8scluster_list(ctx, filter, literal):
     check_client_version(ctx.obj, ctx.command.name)
     resp = ctx.obj.k8scluster.list(filter)
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     table = PrettyTable(['Name', 'Id', 'Version', 'VIM', 'K8s-nets', 'Operational State', 'Description'])
     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',''),40)])
+                       trunc_text(cluster.get('description') or '', 40)])
     table.align = 'l'
     print(table)
     # except ClientException as e:
@@ -3118,7 +3120,7 @@ def k8scluster_show(ctx, name, literal):
     # try:
     resp = ctx.obj.k8scluster.get(name)
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     table = PrettyTable(['key', 'attribute'])
     for k, v in list(resp.items()):
@@ -3259,12 +3261,12 @@ def repo_list(ctx, filter, literal):
     resp = ctx.obj.repo.list(filter)
     resp += ctx.obj.osmrepo.list(filter)
     if literal:
-        print(yaml.safe_dump(resp))
+        print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     table = PrettyTable(['Name', 'Id', 'Type', 'URI', 'Description'])
     for repo in resp:
         #cluster['k8s-nets'] = json.dumps(yaml.safe_load(cluster['k8s-nets']))
-        table.add_row([repo['name'], repo['_id'], repo['type'], repo['url'], trunc_text(repo.get('description',''),40)])
+        table.add_row([repo['name'], repo['_id'], repo['type'], repo['url'], trunc_text(repo.get('description') or '',40)])
     table.align = 'l'
     print(table)
 
@@ -3290,7 +3292,7 @@ def repo_show(ctx, name, literal):
 
     if literal:
         if resp:
-            print(yaml.safe_dump(resp))
+            print(yaml.safe_dump(resp, indent=4, default_flow_style=False))
         return
     table = PrettyTable(['key', 'attribute'])
     if resp:
@@ -3317,18 +3319,25 @@ def repo_show(ctx, name, literal):
 @click.option('--domain-name', 'domain_name',
               default=None,
               help='assign to a domain')
+@click.option('--quotas', 'quotas', multiple=True, default=None,
+              help="provide quotas. Can be used several times: 'quota1=number[,quota2=number,...]'. Quotas can be one "
+                   "of vnfds, nsds, nsts, pdus, nsrs, nsis, vim_accounts, wim_accounts, sdns, k8sclusters, k8srepos")
 @click.pass_context
-def project_create(ctx, name, domain_name):
+def project_create(ctx, name, domain_name, quotas):
     """Creates a new project
 
     NAME: name of the project
     DOMAIN_NAME: optional domain name for the project when keystone authentication is used
+    QUOTAS: set quotas for the project
     """
     logger.debug("")
-    project = {}
-    project['name'] = name
+    project = {'name': name}
     if domain_name:
         project['domain_name'] = domain_name
+    quotas_dict = _process_project_quotas(quotas)
+    if quotas_dict:
+        project['quotas'] = quotas_dict
+
     # try:
     check_client_version(ctx.obj, ctx.command.name)
     ctx.obj.project.create(name, project)
@@ -3337,6 +3346,20 @@ def project_create(ctx, name, domain_name):
     #     exit(1)
 
 
+def _process_project_quotas(quota_list):
+    quotas_dict = {}
+    if not quota_list:
+        return quotas_dict
+    try:
+        for quota in quota_list:
+            for single_quota in quota.split(","):
+                k, v = single_quota.split("=")
+                quotas_dict[k] = None if v in ('None', 'null', '') else int(v)
+    except (ValueError, TypeError):
+        raise ClientException("invalid format for 'quotas'. Use 'k1=v1,v1=v2'. v must be a integer or null")
+    return quotas_dict
+
+
 @cli_osm.command(name='project-delete', short_help='deletes a project')
 @click.argument('name')
 #@click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions')
@@ -3400,23 +3423,29 @@ def project_show(ctx, name):
 
 @cli_osm.command(name='project-update', short_help='updates a project (only the name can be updated)')
 @click.argument('project')
-@click.option('--name',
-              prompt=True,
+@click.option('--name', default=None,
               help='new name for the project')
-
+@click.option('--quotas', 'quotas', multiple=True, default=None,
+              help="change quotas. Can be used several times: 'quota1=number|empty[,quota2=...]' "
+                   "(use empty to reset quota to default")
 @click.pass_context
-def project_update(ctx, project, name):
+def project_update(ctx, project, name, quotas):
     """
     Update a project name
 
     :param ctx:
     :param project: id or name of the project to modify
     :param name:  new name for the project
+    :param quotas:  change quotas of the project
     :return:
     """
     logger.debug("")
     project_changes = {}
-    project_changes['name'] = name
+    if name:
+        project_changes['name'] = name
+    quotas_dict = _process_project_quotas(quotas)
+    if quotas_dict:
+        project_changes['quotas'] = quotas_dict
 
     # try:
     check_client_version(ctx.obj, ctx.command.name)
@@ -3892,7 +3921,7 @@ def upload_package(ctx, filename, skip_charm_build):
 @click.option('--vnf_name', default=None, help='member-vnf-index if the target is a vnf instead of a ns)')
 @click.option('--kdu_name', default=None, help='kdu-name if the target is a kdu)')
 @click.option('--vdu_id', default=None, help='vdu-id if the target is a vdu')
-@click.option('--vdu_count', default=None, help='number of vdu instance of this vdu_id')
+@click.option('--vdu_count', default=None, type=int, help='number of vdu instance of this vdu_id')
 @click.option('--action_name', prompt=True, help='action name')
 @click.option('--params', default=None, help='action params in YAML/JSON inline string')
 @click.option('--params_file', default=None, help='YAML/JSON file with action params')
@@ -3928,7 +3957,7 @@ def ns_action(ctx,
         op_data['kdu_name'] = kdu_name
     if vdu_id:
         op_data['vdu_id'] = vdu_id
-    if vdu_count:
+    if vdu_count is not None:
         op_data['vdu_count_index'] = vdu_count
     if timeout:
         op_data['timeout_ns_action'] = timeout