Feature 8047: osmclient package creation and validation tool
[osm/osmclient.git] / osmclient / scripts / osm.py
index ee102e6..b1df0a5 100755 (executable)
@@ -3001,6 +3001,152 @@ def role_show(ctx, name):
     table.align = 'l'
     print(table)
 
+
+@cli.command(name='package-create',
+             short_help='Create a package descriptor')
+@click.argument('package-type')
+@click.argument('package-name')
+@click.option('--base-directory',
+              default='.',
+              help=('(NS/VNF/NST) Set the location for package creation. Default: "."'))
+@click.option('--image',
+              default="image-name",
+              help='(VNF) Set the name of the vdu image. Default "image-name"')
+@click.option('--vdus',
+              default=1,
+              help='(VNF) Set the number of vdus in a VNF. Default 1')
+@click.option('--vcpu',
+              default=1,
+              help='(VNF) Set the number of virtual CPUs in a vdu. Default 1')
+@click.option('--memory',
+              default=1024,
+              help='(VNF) Set the memory size (MB) of the vdu. Default 1024')
+@click.option('--storage',
+              default=10,
+              help='(VNF) Set the disk size (GB) of the vdu. Default 10')
+@click.option('--interfaces',
+              default=0,
+              help='(VNF) Set the number of additional interfaces apart from the management interface. Default 0')
+@click.option('--vendor',
+              default="OSM",
+              help='(NS/VNF) Set the descriptor vendor. Default "OSM"')
+@click.option('--override',
+              default=False,
+              is_flag=True,
+              help='(NS/VNF/NST) Flag for overriding the package if exists.')
+@click.option('--detailed',
+              is_flag=True,
+              default=False,
+              help='(NS/VNF/NST) Flag for generating descriptor .yaml with all possible commented options')
+@click.option('--netslice-subnets',
+              default=1,
+              help='(NST) Number of netslice subnets. Default 1')
+@click.option('--netslice-vlds',
+              default=1,
+              help='(NST) Number of netslice vlds. Default 1')
+@click.pass_context
+def package_create(ctx,
+                   package_type,
+                   base_directory,
+                   package_name,
+                   override,
+                   image,
+                   vdus,
+                   vcpu,
+                   memory,
+                   storage,
+                   interfaces,
+                   vendor,
+                   detailed,
+                   netslice_subnets,
+                   netslice_vlds):
+    """
+    Creates an OSM NS, VNF, NST package
+
+    \b
+    PACKAGE_TYPE: Package to be created: NS, VNF or NST.
+    PACKAGE_NAME: Name of the package to create the folder with the content.
+    """
+
+    try:
+        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,
+                                           base_directory,
+                                           package_name,
+                                           override=override,
+                                           image=image,
+                                           vdus=vdus,
+                                           vcpu=vcpu,
+                                           memory=memory,
+                                           storage=storage,
+                                           interfaces=interfaces,
+                                           vendor=vendor,
+                                           detailed=detailed,
+                                           netslice_subnets=netslice_subnets,
+                                           netslice_vlds=netslice_vlds)
+        print(resp)
+    except ClientException as inst:
+        print("ERROR: {}".format(inst))
+        exit(1)
+
+@cli.command(name='package-validate',
+             short_help='Validate a package descriptor')
+@click.argument('base-directory',
+                default=".",
+                required=False)
+@click.pass_context
+def package_validate(ctx,
+                     base_directory):
+    """
+    Validate descriptors given a base directory.
+
+    \b
+    BASE_DIRECTORY: Stub folder for NS, VNF or NST package.
+    """
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        results = ctx.obj.package_tool.validate(base_directory)
+        table = PrettyTable()
+        table.field_names = ["TYPE", "PATH", "VALID", "ERROR"]
+        # Print the dictionary generated by the validation function
+        for result in results:
+            table.add_row([result["type"], result["path"], result["valid"], result["error"]])
+        table.sortby = "VALID"
+        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.command(name='package-build',
+             short_help='Build the tar.gz of the package')
+@click.argument('package-folder')
+@click.option('--skip-validation',
+              default=False,
+              is_flag=True,
+              help='skip package validation')
+@click.pass_context
+def package_build(ctx,
+                  package_folder,
+                  skip_validation):
+    """
+    Build the package NS, VNF given the package_folder.
+
+    \b
+    PACKAGE_FOLDER: Folder of the NS, VNF or NST to be packaged
+    """
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        results = ctx.obj.package_tool.build(package_folder, skip_validation)
+        print(results)
+    except ClientException as inst:
+        print("ERROR: {}".format(inst))
+        exit(1)
+
+
 if __name__ == '__main__':
     try:
         cli()