fix pdu_create and nfpkg_list
[osm/osmclient.git] / osmclient / scripts / osm.py
index 94a503e..a12cfb2 100755 (executable)
@@ -285,6 +285,32 @@ def vnfd_list2(ctx, filter):
     vnfd_list(ctx,filter)
 
 
+@cli.command(name='nfpkg-list')
+@click.option('--nf_type', help='type of NFPKG (vnf, pnf, hnf)')
+@click.option('--filter', default=None,
+              help='restricts the list to the xNFpkg matching the filter')
+@click.pass_context
+def nfpkg_list(ctx, nf_type, filter):
+    '''list all NFpkg (VNFpkg, PNFpkg, HNFpkg) in the system'''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        if nf_type:
+            if nf_type == "vnf":
+                nf_filter = "_admin.type=vnfd"
+            elif nf_type == "pnf":
+                nf_filter = "_admin.type=pnfd"
+            elif nf_type == "hnf":
+                nf_filter = "_admin.type=hnfd"
+            else:
+                raise ClientException('wrong value for "--nf_type" option, allowed values: vnf, pnf, hnf')
+            if filter:
+                filter = '{}&{}'.format(nf_filter, filter)
+        vnfd_list(ctx,filter)
+    except ClientException as inst:
+        print((inst.message))
+        exit(1)
+
+
 @cli.command(name='vnf-list')
 @click.option('--ns', default=None, help='NS instance id or name to restrict the VNF list')
 @click.option('--filter', default=None,
@@ -532,6 +558,41 @@ def nsi_op_list2(ctx, name):
     nsi_op_list(ctx,name)
 
 
+@cli.command(name='pdu-list')
+@click.option('--filter', default=None,
+              help='restricts the list to the Physical Deployment Units matching the filter')
+@click.pass_context
+def pdu_list(ctx, filter):
+    '''list all Physical Deployment Units (PDU)'''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        resp = ctx.obj.pdu.list(filter)
+    except ClientException as inst:
+        print((inst.message))
+        exit(1)
+    table = PrettyTable(
+        ['pdu name',
+         'id',
+         'type',
+         'mgmt ip address'])
+    for pdu in resp:
+        pdu_name = pdu['name']
+        pdu_id = pdu['_id']
+        pdu_type = pdu['type']
+        pdu_ipaddress = "None"
+        for iface in pdu['interfaces']:
+            if iface['mgmt']:
+                pdu_ipaddress = iface['ip-address']
+                break
+        table.add_row(
+            [pdu_name,
+             pdu_id,
+             pdu_type,
+             pdu_ipaddress])
+    table.align = 'l'
+    print(table)
+
+
 ####################
 # SHOW operations
 ####################
@@ -626,6 +687,19 @@ def vnfd_show2(ctx, name, literal):
     vnfd_show(ctx, name, literal)
 
 
+@cli.command(name='nfpkg-show', short_help='shows the content of a NF Descriptor')
+@click.option('--literal', is_flag=True,
+              help='print literally, no pretty table')
+@click.argument('name')
+@click.pass_context
+def nfpkg_show(ctx, name, literal):
+    '''shows the content of a NF Descriptor
+
+    NAME: name or ID of the NFpkg
+    '''
+    vnfd_show(ctx, name, literal)
+
+
 @cli.command(name='ns-show', short_help='shows the info of a NS instance')
 @click.argument('name')
 @click.option('--literal', is_flag=True,
@@ -900,6 +974,38 @@ def nsi_op_show2(ctx, id, filter):
     nsi_op_show(ctx, id, filter)
 
 
+@cli.command(name='pdu-show', short_help='shows the content of a Physical Deployment Unit (PDU)')
+@click.argument('name')
+@click.option('--literal', is_flag=True,
+              help='print literally, no pretty table')
+@click.option('--filter', default=None)
+@click.pass_context
+def pdu_show(ctx, name, literal, filter):
+    '''shows the content of a Physical Deployment Unit (PDU)
+
+    NAME: name or ID of the PDU
+    '''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        pdu = ctx.obj.pdu.get(name)
+    except ClientException as inst:
+        print((inst.message))
+        exit(1)
+
+    if literal:
+        print(yaml.safe_dump(pdu))
+        return
+
+    table = PrettyTable(['field', 'value'])
+
+    for k, v in list(pdu.items()):
+        if filter is None or filter in k:
+            table.add_row([k, json.dumps(v, indent=2)])
+
+    table.align = 'l'
+    print(table)
+
+
 ####################
 # CREATE operations
 ####################
@@ -974,6 +1080,19 @@ def vnfd_create2(ctx, filename, overwrite):
     vnfd_create(ctx, filename, overwrite)
 
 
+@cli.command(name='nfpkg-create', short_help='creates a new NFpkg')
+@click.argument('filename')
+@click.option('--overwrite', default=None,
+              help='overwrites some fields in NFD')
+@click.pass_context
+def nfpkg_create(ctx, filename, overwrite):
+    '''creates a new NFpkg
+
+    FILENAME: NF Descriptor yaml file or NFpkg tar.gz file
+    '''
+    vnfd_create(ctx, filename, overwrite)
+
+
 @cli.command(name='ns-create', short_help='creates a new Network Service instance')
 @click.option('--ns_name',
               prompt=True, help='name of the NS instance')
@@ -1056,10 +1175,15 @@ def nst_create2(ctx, filename, overwrite):
     nst_create(ctx, filename, overwrite)
 
 
-def nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config):
+def nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file):
     '''creates a new Network Slice Instance (NSI)'''
     try:
         check_client_version(ctx.obj, ctx.command.name)
+        if config_file:
+            if config:
+                raise ClientException('"--config" option is incompatible with "--config_file" option')
+            with open(config_file, 'r') as cf:
+                config=cf.read()
         ctx.obj.nsi.create(nst_name, nsi_name, config=config, ssh_keys=ssh_keys,
             account=vim_account)
     except ClientException as inst:
@@ -1082,10 +1206,13 @@ def nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config):
               '],\n'
               'netslice-vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]'
               )
+@click.option('--config_file',
+              default=None,
+              help='nsi specific yaml configuration file')
 @click.pass_context
-def nsi_create1(ctx, nst_name, nsi_name, vim_account, ssh_keys, config):
+def nsi_create1(ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file):
     '''creates a new Network Slice Instance (NSI)'''
-    nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config)
+    nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file)
 
 
 @cli.command(name='netslice-instance-create', short_help='creates a new Network Slice Instance')
@@ -1103,10 +1230,58 @@ def nsi_create1(ctx, nst_name, nsi_name, vim_account, ssh_keys, config):
               '],\n'
               'netslice-vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]'
               )
+@click.option('--config_file',
+              default=None,
+              help='nsi specific yaml configuration file')
 @click.pass_context
-def nsi_create2(ctx, nst_name, nsi_name, vim_account, ssh_keys, config):
+def nsi_create2(ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file):
     '''creates a new Network Slice Instance (NSI)'''
-    nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config)
+    nsi_create(ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file)
+
+
+@cli.command(name='pdu-create', short_help='adds a new Physical Deployment Unit to the catalog')
+@click.option('--name', help='name of the Physical Deployment Unit')
+@click.option('--pdu_type', help='type of PDU (e.g. router, firewall, FW001)')
+@click.option('--interface',
+              help='interface(s) of the PDU: name=<NAME>,mgmt=<true|false>,ip-address=<IP_ADDRESS>'+
+                   '[,type=<overlay|underlay>][,mac-address=<MAC_ADDRESS>][,vim-network-name=<VIM_NET_NAME>]',
+              multiple=True)
+@click.option('--description', help='human readable description')
+@click.option('--vim_account', help='list of VIM accounts (in the same VIM) that can reach this PDU', multiple=True)
+@click.option('--descriptor_file', default=None, help='PDU descriptor file (as an alternative to using the other arguments')
+@click.pass_context
+def pdu_create(ctx, name, pdu_type, interface, description, vim_account, descriptor_file):
+    '''creates a new Physical Deployment Unit (PDU)'''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        pdu = {}
+        if not descriptor_file:
+            if not name:
+                raise ClientException('in absence of descriptor file, option "--name" is mandatory')
+            if not pdu_type:
+                raise ClientException('in absence of descriptor file, option "--pdu_type" is mandatory')
+            if not interface:
+                raise ClientException('in absence of descriptor file, option "--interface" is mandatory (at least once)')
+            if not vim_account:
+                raise ClientException('in absence of descriptor file, option "--vim_account" is mandatory (at least once)')
+        else:
+            with open(descriptor_file, 'r') as df:
+                pdu = yaml.load(df.read())
+        if name: pdu["name"] = name
+        if pdu_type: pdu["type"] = pdu_type
+        if description: pdu["description"] = description
+        if vim_account: pdu["vim_accounts"] = vim_account
+        if interface:
+            ifaces_list = []
+            for iface in interface:
+                new_iface={k:v for k,v in [i.split('=') for i in iface.split(',')]}
+                new_iface["mgmt"] = (new_iface.get("mgmt","false").lower() == "true")
+                ifaces_list.append(new_iface)
+            pdu["interfaces"] = ifaces_list
+        ctx.obj.pdu.create(pdu)
+    except ClientException as inst:
+        print((inst.message))
+        exit(1)
 
 
 ####################
@@ -1183,6 +1358,19 @@ def vnfd_update2(ctx, name, content):
     vnfd_update(ctx, name, content)
 
 
+@cli.command(name='nfpkg-update', short_help='updates a NFpkg')
+@click.argument('name')
+@click.option('--content', default=None,
+              help='filename with the NFpkg replacing the current one')
+@click.pass_context
+def nfpkg_update(ctx, name, content):
+    '''updates a NFpkg
+
+    NAME: NF Descriptor yaml file or NFpkg tar.gz file
+    '''
+    vnfd_update(ctx, name, content)
+
+
 def nst_update(ctx, name, content):
     try:
         check_client_version(ctx.obj, ctx.command.name)
@@ -1294,6 +1482,18 @@ def vnfd_delete2(ctx, name, force):
     vnfd_delete(ctx, name, force)
 
 
+@cli.command(name='nfpkg-delete', short_help='deletes a NFpkg')
+@click.argument('name')
+@click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions')
+@click.pass_context
+def nfpkg_delete(ctx, name, force):
+    '''deletes a NFpkg
+
+    NAME: name or ID of the NFpkg to be deleted
+    '''
+    vnfd_delete(ctx, name, force)
+
+
 @cli.command(name='ns-delete', short_help='deletes a NS instance')
 @click.argument('name')
 @click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions')
@@ -1380,6 +1580,23 @@ def nsi_delete2(ctx, name, force):
     nsi_delete(ctx, name, force)
 
 
+@cli.command(name='pdu-delete', short_help='deletes a Physical Deployment Unit (PDU)')
+@click.argument('name')
+@click.option('--force', is_flag=True, help='forces the deletion bypassing pre-conditions')
+@click.pass_context
+def pdu_delete(ctx, name, force):
+    '''deletes a Physical Deployment Unit (PDU)
+
+    NAME: name or ID of the PDU to be deleted
+    '''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        ctx.obj.pdu.delete(name, force)
+    except ClientException as inst:
+        print((inst.message))
+        exit(1)
+
+
 ####################
 # VIM operations
 ####################