osm.py fix bug in ns-action parameters, missing action_name
[osm/osmclient.git] / osmclient / scripts / osm.py
index c1a7f18..15b38e4 100755 (executable)
@@ -35,10 +35,10 @@ def check_client_version(obj, what, version='sol005'):
     :return: -
     :raises ClientError: if the specified version does not match the client version
     '''
-    fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
-    message = 'the following commands or options are only supported with the option "--sol005": {}'.format(what)
+    fullclassname = obj.__module__ + "." + obj.__class__.__name__
+    message = 'The following commands or options are only supported with the option "--sol005": {}'.format(what)
     if version == 'v1':
-        message = 'the following commands or options are not supported when using option "--sol005": {}'.format(what)
+        message = 'The following commands or options are not supported when using option "--sol005": {}'.format(what)
     if fullclassname != 'osmclient.{}.client.Client'.format(version):
         raise ClientException(message)
     return
@@ -104,7 +104,7 @@ def cli(ctx, hostname, so_port, so_project, ro_hostname, ro_port, sol005):
 def ns_list(ctx, filter):
     '''list all NS instances'''
     if filter:
-        check_client_version(ctx.obj, '--filter option')
+        check_client_version(ctx.obj, '--filter')
         resp = ctx.obj.ns.list(filter)
     else:
         resp = ctx.obj.ns.list()
@@ -112,23 +112,30 @@ def ns_list(ctx, filter):
         ['ns instance name',
          'id',
          'operational status',
-         'config status'])
+         'config status',
+         'detailed status'])
     for ns in resp:
         fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
         if fullclassname == 'osmclient.sol005.client.Client':
             nsr = ns
+            nsr_name = nsr['name']
+            nsr_id = nsr['_id']
         else:
             nsopdata = ctx.obj.ns.get_opdata(ns['id'])
             nsr = nsopdata['nsr:nsr']
+            nsr_name = nsr['name-ref']
+            nsr_id = nsr['ns-instance-config-ref']
         opstatus = nsr['operational-status'] if 'operational-status' in nsr else 'Not found'
         configstatus = nsr['config-status'] if 'config-status' in nsr else 'Not found'
+        detailed_status = nsr['detailed-status'] if 'detailed-status' in nsr else 'Not found'
         if configstatus == "config_not_needed":
             configstatus = "configured (no charms)"
         table.add_row(
-            [nsr['name'],
-             nsr['_id'],
+            [nsr_name,
+             nsr_id,
              opstatus,
-             configstatus])
+             configstatus,
+             detailed_status])
     table.align = 'l'
     print(table)
 
@@ -213,7 +220,12 @@ def vnfd_list2(ctx, filter):
 @click.pass_context
 def vnf_list(ctx):
     ''' list all VNF instances'''
-    resp = ctx.obj.vnf.list()
+    try:
+        check_client_version(ctx.obj, ctx.command.name, 'v1')
+        resp = ctx.obj.vnf.list()
+    except ClientException as inst:
+        print(inst.message)
+        exit(1)
     table = PrettyTable(
         ['vnf name',
          'id',
@@ -231,6 +243,27 @@ def vnf_list(ctx):
     table.align = 'l'
     print(table)
 
+@cli.command(name='ns-op-list')
+@click.argument('name')
+@click.pass_context
+def ns_op_list(ctx, name):
+    '''shows the history of operations over a NS instance
+
+    NAME: name or ID of the NS instance
+    '''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        resp = ctx.obj.ns.list_op(name)
+    except ClientException as inst:
+        print(inst.message)
+        exit(1)
+
+    table = PrettyTable(['id', 'operation', 'status'])
+    for op in resp:
+         table.add_row([op['id'], op['lcmOperationType'],
+                        op['operationState']])
+    table.align = 'l'
+    print(table)
 
 ####################
 # SHOW operations
@@ -439,6 +472,29 @@ def ns_monitoring_show(ctx, ns_name):
     table.align = 'l'
     print(table)
 
+@cli.command(name='ns-op-show', short_help='shows the info of an operation')
+@click.argument('id')
+@click.option('--filter', default=None)
+@click.pass_context
+def ns_op_show(ctx, id, filter):
+    '''shows the detailed info of an operation
+
+    ID: operation identifier
+    '''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        op_info = ctx.obj.ns.get_op(id)
+    except ClientException as inst:
+        print(inst.message)
+        exit(1)
+
+    table = PrettyTable(['field', 'value'])
+    for k, v in op_info.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
@@ -529,7 +585,8 @@ def vnfd_create2(ctx, filename, overwrite):
               help='comma separated list of keys to inject to vnfs')
 @click.option('--config',
               default=None,
-              help='ns specific yaml configuration')
+              help='ns specific yaml configuration:\nvnf: [member-vnf-index: TEXT, vim_account: TEXT]\n'
+              'vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]')
 @click.pass_context
 def ns_create(ctx,
               nsd_name,
@@ -540,8 +597,8 @@ def ns_create(ctx,
               config):
     '''creates a new NS instance'''
     try:
-        if config:
-            check_client_version(ctx.obj, '--config', 'v1')
+        if config:
+            check_client_version(ctx.obj, '--config', 'v1')
         ctx.obj.ns.create(
             nsd_name,
             ns_name,
@@ -856,6 +913,8 @@ def vim_show(ctx, name):
     '''
     try:
         resp = ctx.obj.vim.get(name)
+        if 'vim_password' in resp:
+            resp['vim_password']='********'
     except ClientException as inst:
         print(inst.message)
         exit(1)
@@ -1005,5 +1064,34 @@ def vcs_list(ctx):
     print(table)
 
 
+@cli.command(name='ns-action')
+@click.argument('ns_name')
+@click.option('--vnf_name', default=None)
+@click.option('--action_name', prompt=True)
+@click.option('--params', prompt=True)
+@click.pass_context
+def ns_action(ctx,
+              ns_name,
+              vnf_name,
+              action_name,
+              params):
+    '''executes an action/primitive over a NS instance
+
+    NS_NAME: name or ID of the NS instance
+    '''
+    try:
+        check_client_version(ctx.obj, ctx.command.name)
+        op_data={}
+        if vnf_name:
+            op_data['vnf_member_index'] = vnf_name
+        op_data['primitive'] = action_name
+        op_data['primitive_params'] = yaml.load(params)
+        ctx.obj.ns.exec_op(ns_name, op_name='action', op_data=op_data)
+
+    except ClientException as inst:
+        print(inst.message)
+        exit(1)
+
+
 if __name__ == '__main__':
     cli()