New translation commands for SOL006: recursive package-translate, descriptor-translate
[osm/osmclient.git] / osmclient / scripts / osm.py
index 4867e49..d9136f3 100755 (executable)
@@ -71,7 +71,7 @@ def check_client_version(obj, what, version='sol005'):
 
 def get_project(project_list, item):
     # project_list = ctx.obj.project.list()
-    item_project_list = item.get('_admin').get('projects_read')
+    item_project_list = item.get('_admin', {}).get('projects_read')
     project_id = 'None'
     project_name = 'None'
     if item_project_list:
@@ -543,7 +543,7 @@ def vnfd_list(ctx, nf_type, filter, long):
             table = PrettyTable(['nfpkg name', 'id', 'desc type'])
         for vnfd in resp:
             name = vnfd.get('id', vnfd.get('name','-'))
-            descriptor_type = 'sol005' if 'product-name' in vnfd else 'rel8'
+            descriptor_type = 'sol006' if 'product-name' in vnfd else 'rel8'
             if long:
                 onb_state = vnfd['_admin'].get('onboardingState','-')
                 op_state = vnfd['_admin'].get('operationalState','-')
@@ -3155,14 +3155,14 @@ def k8scluster_list(ctx, filter, literal, long):
     except:
         vim_list = []
     for cluster in resp:
+        logger.debug('Cluster details: {}'.format(yaml.safe_dump(cluster)))
         vim_name = get_vim_name(vim_list, cluster['vim_account'])
         #vim_info = '{} ({})'.format(vim_name,cluster['vim_account'])
         vim_info = vim_name
         op_state_details = "Helm: {}\nJuju: {}".format(
-                           cluster["_admin"].get("helm-chart", "-").get("operationalState", "-"),
-                           cluster["_admin"].get("juju-bundle", "-").get("operationalState", "-"))
+                           cluster["_admin"].get("helm-chart", {}).get("operationalState", "-"),
+                           cluster["_admin"].get("juju-bundle", {}).get("operationalState", "-"))
         if long:
-            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
@@ -4224,7 +4224,7 @@ def role_show(ctx, name):
 
 
 @cli_osm.command(name='package-create',
-             short_help='Create a package descriptor')
+             short_help='Create empty NS package structure')
 @click.argument('package-type')
 @click.argument('package-name')
 @click.option('--base-directory',
@@ -4290,6 +4290,7 @@ def package_create(ctx,
     """
 
     # try:
+    logger.debug("")
     check_client_version(ctx.obj, ctx.command.name)
     print("Creating the {} structure: {}/{}".format(package_type.upper(), base_directory, package_name))
     resp = ctx.obj.package_tool.create(package_type,
@@ -4312,7 +4313,7 @@ def package_create(ctx,
     #     exit(1)
 
 @cli_osm.command(name='package-validate',
-             short_help='Validate a package descriptor')
+                 short_help='Validate descriptors given a base directory')
 @click.argument('base-directory',
                 default=".",
                 required=False)
@@ -4320,19 +4321,25 @@ def package_create(ctx,
               default=True,
               help='The activated recursive option will validate the yaml files'
                    ' within the indicated directory and in its subdirectories')
+@click.option('--old',
+              is_flag=True,
+              default=False,
+              help='Validates also the descriptors using the previous OSM format (pre SOL006)')
 @click.pass_context
 def package_validate(ctx,
                      base_directory,
-                     recursive):
+                     recursive,
+                     old):
     """
     Validate descriptors given a base directory.
 
     \b
-    BASE_DIRECTORY: Stub folder for NS, VNF or NST package.
+    BASE_DIRECTORY: Base folder for NS, VNF or NST package.
     """
     # try:
+    logger.debug("")
     check_client_version(ctx.obj, ctx.command.name)
-    results = ctx.obj.package_tool.validate(base_directory, recursive)
+    results = ctx.obj.package_tool.validate(base_directory, recursive, old)
     table = PrettyTable()
     table.field_names = ["TYPE", "PATH", "VALID", "ERROR"]
     # Print the dictionary generated by the validation function
@@ -4347,6 +4354,47 @@ def package_validate(ctx,
     #     print("ERROR: {}".format(inst))
     #     exit(1)
 
+@cli_osm.command(name='package-translate',
+                 short_help='Translate descriptors given a base directory')
+@click.argument('base-directory',
+                default=".",
+                required=False)
+@click.option('--recursive/--no-recursive',
+              default=True,
+              help='The activated recursive option will translate the yaml files'
+                   ' within the indicated directory and in its subdirectories')
+@click.option('--dryrun',
+              is_flag=True,
+              default=False,
+              help='Do not translate yet, only make a dry-run to test translation')
+@click.pass_context
+def package_translate(ctx,
+                     base_directory,
+                     recursive,
+                     dryrun):
+    """
+    Translate descriptors given a base directory.
+
+    \b
+    BASE_DIRECTORY: Stub folder for NS, VNF or NST package.
+    """
+    logger.debug("")
+    check_client_version(ctx.obj, ctx.command.name)
+    results = ctx.obj.package_tool.translate(base_directory, recursive, dryrun)
+    table = PrettyTable()
+    table.field_names = ["CURRENT TYPE", "NEW TYPE", "PATH", "VALID", "TRANSLATED", "ERROR"]
+    # Print the dictionary generated by the validation function
+    for result in results:
+        table.add_row([result["current type"], result["new type"], result["path"], result["valid"], result["translated"], result["error"]])
+    table.sortby = "TRANSLATED"
+    table.align["PATH"] = "l"
+    table.align["TYPE"] = "l"
+    table.align["ERROR"] = "l"
+    print(table)
+    # except ClientException as inst:
+    #     print("ERROR: {}".format(inst))
+    #     exit(1)
+
 @cli_osm.command(name='package-build',
              short_help='Build the tar.gz of the package')
 @click.argument('package-folder')
@@ -4368,6 +4416,7 @@ def package_build(ctx,
     PACKAGE_FOLDER: Folder of the NS, VNF or NST to be packaged
     """
     # try:
+    logger.debug("")
     check_client_version(ctx.obj, ctx.command.name)
     results = ctx.obj.package_tool.build(package_folder,
                                          skip_validation=skip_validation,
@@ -4377,6 +4426,24 @@ def package_build(ctx,
     #     print("ERROR: {}".format(inst))
     #     exit(1)
 
+@cli_osm.command(name='descriptor-translate',
+                 short_help='Translate input descriptor file from Rel EIGHT OSM descriptors to SOL006 and prints in standard output')
+@click.argument('descriptor-file',
+                required=True)
+@click.pass_context
+def descriptor_translate(ctx,
+                         descriptor_file):
+    """
+    Translate input descriptor.
+
+    \b
+    DESCRIPTOR_FILE: Descriptor file for NS, VNF or Network Slice.
+    """
+    logger.debug("")
+    check_client_version(ctx.obj, ctx.command.name)
+    result = ctx.obj.package_tool.descriptor_translate(descriptor_file)
+    print(result)
+
 
 def cli():
     try: