Added --long option for nfpkg-list and nspkg-list; wrap text for nfpkg-show and nspkg...
[osm/osmclient.git] / osmclient / scripts / osm.py
index 280264b..d922ce5 100755 (executable)
@@ -379,7 +379,7 @@ def ns_list(ctx, filter, long):
     print('To get the history of all operations over a NS, run "osm ns-op-list NS_ID"')
     print('For more details on the current operation, run "osm ns-op-show OPERATION_ID"')
 
-def nsd_list(ctx, filter):
+def nsd_list(ctx, filter, long):
     logger.debug("")
     if filter:
         check_client_version(ctx.obj, '--filter')
@@ -387,15 +387,28 @@ def nsd_list(ctx, filter):
     else:
         resp = ctx.obj.nsd.list()
     # print(yaml.safe_dump(resp))
-    table = PrettyTable(['nsd name', 'id'])
     fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
     if fullclassname == 'osmclient.sol005.client.Client':
-        for ns in resp:
-            name = ns['name'] if 'name' in ns else '-'
-            table.add_row([name, ns['_id']])
+        if long:
+            table = PrettyTable(['nsd name', 'id', 'onboarding state', 'operational state',
+                                 'usage state', 'date', 'last update'])
+        else:
+            table = PrettyTable(['nsd name', 'id'])
+        for nsd in resp:
+            name = nsd.get('name','-')
+            if long:
+                onb_state = nsd['_admin'].get('onboardingState','-')
+                op_state = nsd['_admin'].get('operationalState','-')
+                usage_state = nsd['_admin'].get('usageState','-')
+                date = datetime.fromtimestamp(nsd['_admin']['created']).strftime("%Y-%m-%dT%H:%M:%S")
+                last_update = datetime.fromtimestamp(nsd['_admin']['modified']).strftime("%Y-%m-%dT%H:%M:%S")
+                table.add_row([name, nsd['_id'], onb_state, op_state, usage_state, date, last_update])
+            else:
+                table.add_row([name, nsd['_id']])
     else:
-        for ns in resp:
-            table.add_row([ns['name'], ns['id']])
+        table = PrettyTable(['nsd name', 'id'])
+        for nsd in resp:
+            table.add_row([nsd['name'], nsd['id']])
     table.align = 'l'
     print(table)
 
@@ -403,24 +416,26 @@ def nsd_list(ctx, filter):
 @cli_osm.command(name='nsd-list', short_help='list all NS packages')
 @click.option('--filter', default=None,
               help='restricts the list to the NSD/NSpkg matching the filter')
+@click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def nsd_list1(ctx, filter):
+def nsd_list1(ctx, filter, long):
     """list all NSD/NS pkg in the system"""
     logger.debug("")
-    nsd_list(ctx, filter)
+    nsd_list(ctx, filter, long)
 
 
 @cli_osm.command(name='nspkg-list', short_help='list all NS packages')
 @click.option('--filter', default=None,
               help='restricts the list to the NSD/NSpkg matching the filter')
+@click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def nsd_list2(ctx, filter):
+def nsd_list2(ctx, filter, long):
     """list all NS packages"""
     logger.debug("")
-    nsd_list(ctx, filter)
+    nsd_list(ctx, filter, long)
 
 
-def vnfd_list(ctx, nf_type, filter):
+def vnfd_list(ctx, nf_type, filter, long):
     logger.debug("")
     if nf_type:
         check_client_version(ctx.obj, '--nf_type')
@@ -444,13 +459,26 @@ def vnfd_list(ctx, nf_type, filter):
     else:
         resp = ctx.obj.vnfd.list()
     # print(yaml.safe_dump(resp))
-    table = PrettyTable(['nfpkg name', 'id'])
     fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
     if fullclassname == 'osmclient.sol005.client.Client':
+        if long:
+            table = PrettyTable(['nfpkg name', 'id', 'onboarding state', 'operational state',
+                                  'usage state', 'date', 'last update'])
+        else:
+            table = PrettyTable(['nfpkg name', 'id'])
         for vnfd in resp:
             name = vnfd['name'] if 'name' in vnfd else '-'
-            table.add_row([name, vnfd['_id']])
+            if long:
+                onb_state = vnfd['_admin'].get('onboardingState','-')
+                op_state = vnfd['_admin'].get('operationalState','-')
+                usage_state = vnfd['_admin'].get('usageState','-')
+                date = datetime.fromtimestamp(vnfd['_admin']['created']).strftime("%Y-%m-%dT%H:%M:%S")
+                last_update = datetime.fromtimestamp(vnfd['_admin']['modified']).strftime("%Y-%m-%dT%H:%M:%S")
+                table.add_row([name, vnfd['_id'], onb_state, op_state, usage_state, date, last_update])
+            else:
+                table.add_row([name, vnfd['_id']])
     else:
+        table = PrettyTable(['nfpkg name', 'id'])
         for vnfd in resp:
             table.add_row([vnfd['name'], vnfd['id']])
     table.align = 'l'
@@ -461,35 +489,38 @@ def vnfd_list(ctx, nf_type, filter):
 @click.option('--nf_type', help='type of NF (vnf, pnf, hnf)')
 @click.option('--filter', default=None,
               help='restricts the list to the NF pkg matching the filter')
+@click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def vnfd_list1(ctx, nf_type, filter):
+def vnfd_list1(ctx, nf_type, filter, long):
     """list all xNF packages (VNF, HNF, PNF)"""
     logger.debug("")
-    vnfd_list(ctx, nf_type, filter)
+    vnfd_list(ctx, nf_type, filter, long)
 
 
 @cli_osm.command(name='vnfpkg-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,
               help='restricts the list to the NFpkg matching the filter')
+@click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def vnfd_list2(ctx, nf_type, filter):
+def vnfd_list2(ctx, nf_type, filter, long):
     """list all xNF packages (VNF, HNF, PNF)"""
     logger.debug("")
-    vnfd_list(ctx, nf_type, filter)
+    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,
               help='restricts the list to the NFpkg matching the filter')
+@click.option('--long', is_flag=True, help='get more details')
 @click.pass_context
-def nfpkg_list(ctx, nf_type, filter):
+def nfpkg_list(ctx, nf_type, filter, long):
     """list all xNF packages (VNF, HNF, PNF)"""
     logger.debug("")
     # try:
     check_client_version(ctx.obj, ctx.command.name)
-    vnfd_list(ctx, nf_type, filter)
+    vnfd_list(ctx, nf_type, filter, long)
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
@@ -867,7 +898,7 @@ def nsd_show(ctx, name, literal):
 
     table = PrettyTable(['field', 'value'])
     for k, v in list(resp.items()):
-        table.add_row([k, json.dumps(v, indent=2)])
+        table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)])
     table.align = 'l'
     print(table)
 
@@ -915,7 +946,7 @@ def vnfd_show(ctx, name, literal):
 
     table = PrettyTable(['field', 'value'])
     for k, v in list(resp.items()):
-        table.add_row([k, json.dumps(v, indent=2)])
+        table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)])
     table.align = 'l'
     print(table)
 
@@ -1343,11 +1374,11 @@ def pdu_show(ctx, name, literal, filter):
 # CREATE operations
 ####################
 
-def nsd_create(ctx, filename, overwrite):
+def nsd_create(ctx, filename, overwrite, skip_charm_build):
     logger.debug("")
     # try:
     check_client_version(ctx.obj, ctx.command.name)
-    ctx.obj.nsd.create(filename, overwrite)
+    ctx.obj.nsd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
@@ -1360,14 +1391,16 @@ def nsd_create(ctx, filename, overwrite):
 @click.option('--override', 'overwrite', default=None,
               help='overrides fields in descriptor, format: '
                    '"key1.key2...=value[;key3...=value;...]"')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='The charm will not be compiled, it is assumed to already exist')
 @click.pass_context
-def nsd_create1(ctx, filename, overwrite):
+def nsd_create1(ctx, filename, overwrite, skip_charm_build):
     """creates a new NSD/NSpkg
 
     FILENAME: NSD yaml file or NSpkg tar.gz file
     """
     logger.debug("")
-    nsd_create(ctx, filename, overwrite)
+    nsd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
 
 
 @cli_osm.command(name='nspkg-create', short_help='creates a new NSD/NSpkg')
@@ -1377,21 +1410,23 @@ def nsd_create1(ctx, filename, overwrite):
 @click.option('--override', 'overwrite', default=None,
               help='overrides fields in descriptor, format: '
                    '"key1.key2...=value[;key3...=value;...]"')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='The charm will not be compiled, it is assumed to already exist')
 @click.pass_context
-def nsd_create2(ctx, filename, overwrite):
+def nsd_create2(ctx, charm_folder, overwrite, skip_charm_build):
     """creates a new NSD/NSpkg
 
-    FILENAME: NSD yaml file or NSpkg tar.gz file
+    FILENAME: NSD folder, NSD yaml file or NSpkg tar.gz file
     """
     logger.debug("")
-    nsd_create(ctx, filename, overwrite)
+    nsd_create(ctx, charm_folder, overwrite=overwrite, skip_charm_build=skip_charm_build)
 
 
-def vnfd_create(ctx, filename, overwrite):
+def vnfd_create(ctx, filename, overwrite, skip_charm_build):
     logger.debug("")
     # try:
     check_client_version(ctx.obj, ctx.command.name)
-    ctx.obj.vnfd.create(filename, overwrite)
+    ctx.obj.vnfd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
@@ -1404,14 +1439,16 @@ def vnfd_create(ctx, filename, overwrite):
 @click.option('--override', 'overwrite', default=None,
               help='overrides fields in descriptor, format: '
                    '"key1.key2...=value[;key3...=value;...]"')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='The charm will not be compiled, it is assumed to already exist')
 @click.pass_context
-def vnfd_create1(ctx, filename, overwrite):
+def vnfd_create1(ctx, filename, overwrite, skip_charm_build):
     """creates a new VNFD/VNFpkg
 
     FILENAME: VNFD yaml file or VNFpkg tar.gz file
     """
     logger.debug("")
-    vnfd_create(ctx, filename, overwrite)
+    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
 
 
 @cli_osm.command(name='vnfpkg-create', short_help='creates a new VNFD/VNFpkg')
@@ -1421,14 +1458,16 @@ def vnfd_create1(ctx, filename, overwrite):
 @click.option('--override', 'overwrite', default=None,
               help='overrides fields in descriptor, format: '
                    '"key1.key2...=value[;key3...=value;...]"')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='The charm will not be compiled, it is assumed to already exist')
 @click.pass_context
-def vnfd_create2(ctx, filename, overwrite):
+def vnfd_create2(ctx, filename, overwrite, skip_charm_build):
     """creates a new VNFD/VNFpkg
 
-    FILENAME: VNFD yaml file or VNFpkg tar.gz file
+    FILENAME: NF Package Folder, NF Descriptor yaml file or NFpkg tar.gz file
     """
     logger.debug("")
-    vnfd_create(ctx, filename, overwrite)
+    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
 
 
 @cli_osm.command(name='nfpkg-create', short_help='creates a new NFpkg')
@@ -1438,14 +1477,16 @@ def vnfd_create2(ctx, filename, overwrite):
 @click.option('--override', 'overwrite', default=None,
               help='overrides fields in descriptor, format: '
                    '"key1.key2...=value[;key3...=value;...]"')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='The charm will not be compiled, it is assumed to already exist')
 @click.pass_context
-def nfpkg_create(ctx, filename, overwrite):
+def nfpkg_create(ctx, filename, overwrite, skip_charm_build):
     """creates a new NFpkg
 
-    FILENAME: NF Descriptor yaml file or NFpkg tar.gz file
+    FILENAME: NF Package Folder, NF Descriptor yaml file or NFpkg tar.gz filems to build
     """
     logger.debug("")
-    vnfd_create(ctx, filename, overwrite)
+    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
 
 
 @cli_osm.command(name='ns-create', short_help='creates a new Network Service instance')
@@ -1522,13 +1563,13 @@ def nst_create(ctx, filename, overwrite):
               help='overrides fields in descriptor, format: '
                    '"key1.key2...=value[;key3...=value;...]"')
 @click.pass_context
-def nst_create1(ctx, filename, overwrite):
+def nst_create1(ctx, charm_folder, overwrite):
     """creates a new Network Slice Template (NST)
 
-    FILENAME: NST yaml file or NSTpkg tar.gz file
+    FILENAME: NST package folder, NST yaml file or NSTpkg tar.gz file
     """
     logger.debug("")
-    nst_create(ctx, filename, overwrite)
+    nst_create(ctx, charm_folder, overwrite)
 
 
 @cli_osm.command(name='netslice-template-create', short_help='creates a new Network Slice Template (NST)')
@@ -2055,7 +2096,7 @@ def pdu_delete(ctx, name, force):
               default='openstack',
               help='VIM type')
 @click.option('--description',
-              default='no description',
+              default=None,
               help='human readable description')
 @click.option('--sdn_controller', default=None, help='Name or id of the SDN controller associated to this VIM account')
 @click.option('--sdn_port_mapping', default=None, help="File describing the port mapping between compute nodes' ports and switch ports")
@@ -2256,7 +2297,7 @@ def vim_show(ctx, name):
 @click.option('--wim_type',
               help='WIM type')
 @click.option('--description',
-              default='no description',
+              default=None,
               help='human readable description')
 @click.option('--wim_port_mapping', default=None,
               help="File describing the port mapping between DC edge (datacenters, switches, ports) and WAN edge "
@@ -2610,7 +2651,7 @@ def sdnc_show(ctx, name):
               prompt=True,
               help='list of VIM networks, in JSON inline format, where the cluster is accessible via L3 routing, e.g. "{(k8s_net1:vim_network1) [,(k8s_net2:vim_network2) ...]}"')
 @click.option('--description',
-              default='',
+              default=None,
               help='human readable description')
 @click.option('--namespace',
               default='kube-system',
@@ -2784,7 +2825,7 @@ def k8scluster_show(ctx, name, literal):
               prompt=True,
               help='type of repo (helm-chart for Helm Charts, juju-bundle for Juju Bundles)')
 @click.option('--description',
-              default='',
+              default=None,
               help='human readable description')
 #@click.option('--wait',
 #              is_flag=True,
@@ -3319,15 +3360,17 @@ def get_version(ctx):
 
 @cli_osm.command(name='upload-package', short_help='uploads a VNF package or NS package')
 @click.argument('filename')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='the charm will not be compiled, it is assumed to already exist')
 @click.pass_context
-def upload_package(ctx, filename):
-    """uploads a VNF package or NS package
+def upload_package(ctx, filename, skip_charm_build):
+    """uploads a vnf package or ns package
 
-    FILENAME: VNF or NS package file (tar.gz)
+    filename: vnf or ns package folder, or vnf or ns package file (tar.gz)
     """
     logger.debug("")
     # try:
-    ctx.obj.package.upload(filename)
+    ctx.obj.package.upload(filename, skip_charm_build=skip_charm_build)
     fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
     if fullclassname != 'osmclient.sol005.client.Client':
         ctx.obj.package.wait_for_upload(filename)
@@ -3794,9 +3837,14 @@ def package_create(ctx,
 @click.argument('base-directory',
                 default=".",
                 required=False)
+@click.option('--recursive/--no-recursive',
+              default=True,
+              help='The activated recursive option will validate the yaml files'
+                   ' within the indicated directory and in its subdirectories')
 @click.pass_context
 def package_validate(ctx,
-                     base_directory):
+                     base_directory,
+                     recursive):
     """
     Validate descriptors given a base directory.
 
@@ -3805,7 +3853,7 @@ def package_validate(ctx,
     """
     # try:
     check_client_version(ctx.obj, ctx.command.name)
-    results = ctx.obj.package_tool.validate(base_directory)
+    results = ctx.obj.package_tool.validate(base_directory, recursive)
     table = PrettyTable()
     table.field_names = ["TYPE", "PATH", "VALID", "ERROR"]
     # Print the dictionary generated by the validation function
@@ -3827,10 +3875,13 @@ def package_validate(ctx,
               default=False,
               is_flag=True,
               help='skip package validation')
+@click.option('--skip-charm-build', default=False, is_flag=True,
+              help='the charm will not be compiled, it is assumed to already exist')
 @click.pass_context
 def package_build(ctx,
                   package_folder,
-                  skip_validation):
+                  skip_validation,
+                  skip_charm_build):
     """
     Build the package NS, VNF given the package_folder.
 
@@ -3839,7 +3890,9 @@ def package_build(ctx,
     """
     # try:
     check_client_version(ctx.obj, ctx.command.name)
-    results = ctx.obj.package_tool.build(package_folder, skip_validation)
+    results = ctx.obj.package_tool.build(package_folder,
+                                         skip_validation=skip_validation,
+                                         skip_charm_build=skip_charm_build)
     print(results)
     # except ClientException as inst:
     #     print("ERROR: {}".format(inst))