osm.py: safe_dump with indent 4 and default_flow_style False
[osm/osmclient.git] / osmclient / scripts / osm.py
index 0523207..8dbf2b6 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:
@@ -992,7 +992,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'])
@@ -1040,7 +1040,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 +1056,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'
@@ -1218,7 +1218,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'])
@@ -1307,7 +1307,7 @@ def vnf_show(ctx, name, literal, filter, kdu):
         print ("Could not determine KDU status")
 
     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 +1388,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 +1410,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 +1458,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 +1564,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,7 +3091,7 @@ 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:
@@ -3118,7 +3118,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,7 +3259,7 @@ 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:
@@ -3290,7 +3290,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 +3317,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 +3344,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 +3421,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)