X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fscripts%2Fosm.py;h=7b3bfebb74b0cf29777a182bd84c64f466ca4667;hb=392b8e2585079e268809f79f5aeb9429d500e1fe;hp=57fb6b34566cd5ea53e78a6605079712d7f141d7;hpb=880fc142421ab5f24f5a297388c3a57f762ffce7;p=osm%2Fosmclient.git diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 57fb6b3..7b3bfeb 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -69,6 +69,30 @@ def check_client_version(obj, what, version='sol005'): return +def get_project(project_list, item): + # project_list = ctx.obj.project.list() + item_project_list = item.get('_admin').get('projects_read') + project_id = 'None' + project_name = 'None' + if item_project_list: + for p1 in item_project_list: + project_id = p1 + for p2 in project_list: + if p2['_id'] == project_id: + project_name = p2['name'] + return project_id, project_name + return project_id, project_name + + +def get_vim_name(vim_list, vim_id): + vim_name = '-' + for v in vim_list: + if v['uuid'] == vim_id: + vim_name = v['name'] + break + return vim_name + + @click.group(context_settings=dict(help_option_names=['-h', '--help'], max_content_width=160)) @click.option('--hostname', default="127.0.0.1", @@ -172,7 +196,7 @@ def cli_osm(ctx, **kwargs): #################### @cli_osm.command(name='ns-list', short_help='list all NS instances') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NS instances matching the filter.') @click.option('--long', is_flag=True, help='get more details of the NS (project, vim, deployment status, configuration status.') @@ -290,7 +314,7 @@ def ns_list(ctx, filter, long): if ee['elementType'] not in status_ee: status_ee[ee['elementType']] = {} status_ee[ee['elementType']][ee['status']] = 1 - continue; + continue if ee['status'] in status_ee[ee['elementType']]: status_ee[ee['elementType']][ee['status']] += 1 else: @@ -310,6 +334,7 @@ def ns_list(ctx, filter, long): logger.debug("") if filter: check_client_version(ctx.obj, '--filter') + filter='&'.join(filter) resp = ctx.obj.ns.list(filter) else: resp = ctx.obj.ns.list() @@ -326,7 +351,10 @@ def ns_list(ctx, filter, long): 'deployment status', 'configuration status']) project_list = ctx.obj.project.list() - vim_list = ctx.obj.vim.list() + try: + vim_list = ctx.obj.vim.list() + except: + vim_list = [] else: table = PrettyTable( ['ns instance name', @@ -347,20 +375,11 @@ def ns_list(ctx, filter, long): if long: deployment_status = summarize_deployment_status(nsr.get('deploymentStatus')) config_status = summarize_config_status(nsr.get('configurationStatus')) - project_id = nsr.get('_admin').get('projects_read')[0] - project_name = '-' - for p in project_list: - if p['_id'] == project_id: - project_name = p['name'] - break + project_id, project_name = get_project(project_list, nsr) #project = '{} ({})'.format(project_name, project_id) project = project_name vim_id = nsr.get('datacenter') - vim_name = '-' - for v in vim_list: - if v['uuid'] == vim_id: - vim_name = v['name'] - break + vim_name = get_vim_name(vim_list, vim_id) #vim = '{} ({})'.format(vim_name, vim_id) vim = vim_name if 'currentOperation' in nsr: @@ -368,7 +387,8 @@ def ns_list(ctx, filter, long): else: current_operation = "{} ({})".format(nsr['_admin'].get('current-operation','-'), nsr['_admin']['nslcmop']) error_details = "N/A" - if ns_state == "BROKEN" or ns_state == "DEGRADED" or nsr.get('errorDescription'): + if ns_state == "BROKEN" or ns_state == "DEGRADED" or \ + ('currentOperation' not in nsr and nsr.get('errorDescription')): error_details = "{}\nDetail: {}".format(nsr['errorDescription'], nsr['errorDetail']) else: nsopdata = ctx.obj.ns.get_opdata(ns['id']) @@ -414,6 +434,7 @@ def nsd_list(ctx, filter, long): logger.debug("") if filter: check_client_version(ctx.obj, '--filter') + filter='&'.join(filter) resp = ctx.obj.nsd.list(filter) else: resp = ctx.obj.nsd.list() @@ -445,7 +466,7 @@ def nsd_list(ctx, filter, long): @cli_osm.command(name='nsd-list', short_help='list all NS packages') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NSD/NSpkg matching the filter') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -456,7 +477,7 @@ def nsd_list1(ctx, filter, long): @cli_osm.command(name='nspkg-list', short_help='list all NS packages') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NSD/NSpkg matching the filter') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -466,32 +487,14 @@ def nsd_list2(ctx, filter, long): nsd_list(ctx, filter, long) -def pkg_repo_list(ctx, pkgtype, filter, repo, long): - resp = ctx.obj.osmrepo.pkg_list(pkgtype, filter, repo) - if long: - table = PrettyTable(['nfpkg name', 'vendor', 'version', 'latest', 'description', 'repository']) - else: - table = PrettyTable(['nfpkg name', 'repository']) - for vnfd in resp: - name = vnfd.get('name', '-') - repository = vnfd.get('repository') - if long: - vendor = vnfd.get('vendor') - version = vnfd.get('version') - description = vnfd.get('description') - latest = vnfd.get('latest') - table.add_row([name, vendor, version, latest, description, repository]) - else: - table.add_row([name, repository]) - table.align = 'l' - print(table) - def vnfd_list(ctx, nf_type, filter, long): logger.debug("") if nf_type: check_client_version(ctx.obj, '--nf_type') elif filter: check_client_version(ctx.obj, '--filter') + if filter: + filter='&'.join(filter) if nf_type: if nf_type == "vnf": nf_filter = "_admin.type=vnfd" @@ -540,7 +543,7 @@ def vnfd_list(ctx, nf_type, filter, long): @cli_osm.command(name='vnfd-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, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NF pkg matching the filter') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -552,7 +555,7 @@ def vnfd_list1(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, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NFpkg matching the filter') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -561,21 +564,10 @@ def vnfd_list2(ctx, nf_type, filter, long): logger.debug("") vnfd_list(ctx, nf_type, filter, long) -@cli_osm.command(name='vnfpkg-repo-list', short_help='list all xNF from OSM repositories') -@click.option('--filter', default=None, - help='restricts the list to the NFpkg matching the filter') -@click.option('--repo', default=None, - help='restricts the list to a particular OSM repository') -@click.option('--long', is_flag=True, help='get more details') -@click.pass_context -def vnfd_list3(ctx, filter, repo, long): - """list xNF packages from OSM repositories""" - pkgtype = 'vnf' - pkg_repo_list(ctx, pkgtype, filter, repo, 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, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NFpkg matching the filter') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -589,24 +581,89 @@ def nfpkg_list(ctx, nf_type, filter, long): # print(str(e)) # exit(1) + +def pkg_repo_list(ctx, pkgtype, filter, repo, long): + if filter: + filter='&'.join(filter) + resp = ctx.obj.osmrepo.pkg_list(pkgtype, filter, repo) + if long: + table = PrettyTable(['nfpkg name', 'vendor', 'version', 'latest', 'description', 'repository']) + else: + table = PrettyTable(['nfpkg name', 'repository']) + for vnfd in resp: + name = vnfd.get('name', '-') + repository = vnfd.get('repository') + if long: + vendor = vnfd.get('vendor') + version = vnfd.get('version') + description = vnfd.get('description') + latest = vnfd.get('latest') + table.add_row([name, vendor, version, latest, description, repository]) + else: + table.add_row([name, repository]) + table.align = 'l' + print(table) + + +@cli_osm.command(name='vnfpkg-repo-list', short_help='list all xNF from OSM repositories') +@click.option('--filter', default=None, multiple=True, + help='restricts the list to the NFpkg matching the filter') +@click.option('--repo', default=None, + help='restricts the list to a particular OSM repository') +@click.option('--long', is_flag=True, help='get more details') +@click.pass_context +def nfpkg_repo_list1(ctx, filter, repo, long): + """list xNF packages from OSM repositories""" + pkgtype = 'vnf' + pkg_repo_list(ctx, pkgtype, filter, repo, long) + + @cli_osm.command(name='nfpkg-repo-list', short_help='list all xNF from OSM repositories') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NFpkg matching the filter') @click.option('--repo', default=None, help='restricts the list to a particular OSM repository') @click.option('--long', is_flag=True, help='get more details') @click.pass_context -def vnfd_list4(ctx, filter, repo, long): +def nfpkg_repo_list2(ctx, filter, repo, long): """list xNF packages from OSM repositories""" pkgtype = 'vnf' pkg_repo_list(ctx, pkgtype, filter, repo, long) + +@cli_osm.command(name='nsd-repo-list', short_help='list all NS from OSM repositories') +@click.option('--filter', default=None, multiple=True, + help='restricts the list to the NS matching the filter') +@click.option('--repo', default=None, + help='restricts the list to a particular OSM repository') +@click.option('--long', is_flag=True, help='get more details') +@click.pass_context +def nspkg_repo_list(ctx, filter, repo, long): + """list xNF packages from OSM repositories""" + pkgtype = 'ns' + pkg_repo_list(ctx, pkgtype, filter, repo, long) + + +@cli_osm.command(name='nspkg-repo-list', short_help='list all NS from OSM repositories') +@click.option('--filter', default=None, multiple=True, + help='restricts the list to the NS matching the filter') +@click.option('--repo', default=None, + help='restricts the list to a particular OSM repository') +@click.option('--long', is_flag=True, help='get more details') +@click.pass_context +def nspkg_repo_list2(ctx, filter, repo, long): + """list xNF packages from OSM repositories""" + pkgtype = 'ns' + pkg_repo_list(ctx, pkgtype, filter, repo, long) + + def vnf_list(ctx, ns, filter, long): # try: if ns or filter: if ns: check_client_version(ctx.obj, '--ns') if filter: + filter='&'.join(filter) check_client_version(ctx.obj, '--filter') resp = ctx.obj.vnf.list(ns, filter) else: @@ -654,7 +711,7 @@ def vnf_list(ctx, ns, filter, long): @cli_osm.command(name='vnf-list', short_help='list all NF instances') @click.option('--ns', default=None, help='NS instance id or name to restrict the NF list') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NF instances matching the filter.') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -663,33 +720,10 @@ def vnf_list1(ctx, ns, filter, long): logger.debug("") vnf_list(ctx, ns, filter, long) -@cli_osm.command(name='nsd-repo-list', short_help='list all NS from OSM repositories') -@click.option('--filter', default=None, - help='restricts the list to the NS matching the filter') -@click.option('--repo', default=None, - help='restricts the list to a particular OSM repository') -@click.option('--long', is_flag=True, help='get more details') -@click.pass_context -def nsd_list3(ctx, filter, repo, long): - """list xNF packages from OSM repositories""" - pkgtype = 'ns' - pkg_repo_list(ctx, pkgtype, filter, repo, long) - -@cli_osm.command(name='nspkg-repo-list', short_help='list all NS from OSM repositories') -@click.option('--filter', default=None, - help='restricts the list to the NS matching the filter') -@click.option('--repo', default=None, - help='restricts the list to a particular OSM repository') -@click.option('--long', is_flag=True, help='get more details') -@click.pass_context -def nspkg_list(ctx, filter, repo, long): - """list xNF packages from OSM repositories""" - pkgtype = 'ns' - pkg_repo_list(ctx, pkgtype, filter, repo, long) @cli_osm.command(name='nf-list', short_help='list all NF instances') @click.option('--ns', default=None, help='NS instance id or name to restrict the NF list') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NF instances matching the filter.') @click.option('--long', is_flag=True, help='get more details') @click.pass_context @@ -786,12 +820,12 @@ def ns_op_list(ctx, name, long): if op['lcmOperationType']=='action': action_name = op['operationParams']['primitive'] detail = "-" - if op['operationState']=='PROCESSING': - if op['lcmOperationType'] in ('instantiate', 'terminate'): + if op['operationState'] == 'PROCESSING': + if op['queuePosition'] is not None and op['queuePosition'] > 0: + detail = "In queue. Current position: {}".format(op['queuePosition']) + elif op['lcmOperationType'] in ('instantiate', 'terminate'): if op['stage']: detail = op['stage'] - else: - detail = "In queue. Current position: {}".format(op['queuePosition']) elif op['operationState'] in ('FAILED', 'FAILED_TEMP'): detail = op.get('errorMessage','-') date = datetime.fromtimestamp(op['startTime']).strftime("%Y-%m-%dT%H:%M:%S") @@ -817,6 +851,8 @@ def nsi_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.nsi.list(filter) # except ClientException as e: # print(str(e)) @@ -846,7 +882,7 @@ def nsi_list(ctx, filter): @cli_osm.command(name='nsi-list', short_help='list all Network Slice Instances (NSI)') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the Network Slice Instances matching the filter') @click.pass_context def nsi_list1(ctx, filter): @@ -856,7 +892,7 @@ def nsi_list1(ctx, filter): @cli_osm.command(name='netslice-instance-list', short_help='list all Network Slice Instances (NSI)') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the Network Slice Instances matching the filter') @click.pass_context def nsi_list2(ctx, filter): @@ -869,6 +905,8 @@ def nst_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.nst.list(filter) # except ClientException as e: # print(str(e)) @@ -883,7 +921,7 @@ def nst_list(ctx, filter): @cli_osm.command(name='nst-list', short_help='list all Network Slice Templates (NST)') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NST matching the filter') @click.pass_context def nst_list1(ctx, filter): @@ -893,7 +931,7 @@ def nst_list1(ctx, filter): @cli_osm.command(name='netslice-template-list', short_help='list all Network Slice Templates (NST)') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the NST matching the filter') @click.pass_context def nst_list2(ctx, filter): @@ -943,7 +981,7 @@ def nsi_op_list2(ctx, name): @cli_osm.command(name='pdu-list', short_help='list all Physical Deployment Units (PDU)') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the Physical Deployment Units matching the filter') @click.pass_context def pdu_list(ctx, filter): @@ -951,6 +989,8 @@ def pdu_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.pdu.list(filter) # except ClientException as e: # print(str(e)) @@ -992,7 +1032,7 @@ def nsd_show(ctx, name, literal): # exit(1) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) @@ -1002,7 +1042,7 @@ def nsd_show(ctx, name, literal): print(table) -@cli_osm.command(name='nsd-show', short_help='shows the content of a NSD') +@cli_osm.command(name='nsd-show', short_help='shows the details of a NS package') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.argument('name') @@ -1016,7 +1056,7 @@ def nsd_show1(ctx, name, literal): nsd_show(ctx, name, literal) -@cli_osm.command(name='nspkg-show', short_help='shows the content of a NSD') +@cli_osm.command(name='nspkg-show', short_help='shows the details of a NS package') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.argument('name') @@ -1040,7 +1080,7 @@ def vnfd_show(ctx, name, literal): # exit(1) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) @@ -1052,11 +1092,13 @@ def vnfd_show(ctx, name, literal): def pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal): logger.debug("") + if filter: + filter='&'.join(filter) # try: resp = ctx.obj.osmrepo.pkg_get(pkgtype, name, repo, version, filter) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return pkgtype += 'd' catalog = pkgtype + '-catalog' @@ -1072,7 +1114,8 @@ def pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal): table.align = 'l' print(table) -@cli_osm.command(name='vnfd-show', short_help='shows the content of a VNFD') + +@cli_osm.command(name='vnfd-show', short_help='shows the details of a NF package') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.argument('name') @@ -1086,7 +1129,7 @@ def vnfd_show1(ctx, name, literal): vnfd_show(ctx, name, literal) -@cli_osm.command(name='vnfpkg-show', short_help='shows the content of a VNFD') +@cli_osm.command(name='vnfpkg-show', short_help='shows the details of a NF package') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.argument('name') @@ -1099,14 +1142,15 @@ def vnfd_show2(ctx, name, literal): logger.debug("") vnfd_show(ctx, name, literal) -@cli_osm.command(name='vnfpkg-repo-show', short_help='shows the content of a VNFD') + +@cli_osm.command(name='vnfpkg-repo-show', short_help='shows the details of a NF package in an OSM repository') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.option('--repo', required=True, help='Repository name') @click.argument('name') -@click.option('--filter', +@click.option('--filter', default=None, multiple=True, help='filter by fields') @click.option('--version', default='latest', @@ -1121,14 +1165,14 @@ def vnfd_show3(ctx, name, repo, version, literal=None, filter=None): pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal) -@cli_osm.command(name='nsd-repo-show', short_help='shows the content of a NSD') +@cli_osm.command(name='nsd-repo-show', short_help='shows the details of a NS package in an OSM repository') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.option('--repo', required=True, help='Repository name') @click.argument('name') -@click.option('--filter', +@click.option('--filter', default=None, multiple=True, help='filter by fields') @click.option('--version', default='latest', @@ -1142,14 +1186,15 @@ def nsd_repo_show(ctx, name, repo, version, literal=None, filter=None): pkgtype = 'ns' pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal) -@cli_osm.command(name='nspkg-repo-show', short_help='shows the content of a NSD') + +@cli_osm.command(name='nspkg-repo-show', short_help='shows the details of a NS package in an OSM repository') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.option('--repo', required=True, help='Repository name') @click.argument('name') -@click.option('--filter', +@click.option('--filter', default=None, multiple=True, help='filter by fields') @click.option('--version', default='latest', @@ -1163,7 +1208,8 @@ def nsd_repo_show2(ctx, name, repo, version, literal=None, filter=None): pkgtype = 'ns' pkg_repo_show(ctx, pkgtype, name, repo, version, filter, literal) -@cli_osm.command(name='nfpkg-show', short_help='shows the content of a NF Descriptor') + +@cli_osm.command(name='nfpkg-show', short_help='shows the details of a NF package') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.argument('name') @@ -1177,14 +1223,14 @@ def nfpkg_show(ctx, name, literal): vnfd_show(ctx, name, literal) -@cli_osm.command(name='nfpkg-repo-show', short_help='shows the content of a VNFD') +@cli_osm.command(name='nfpkg-repo-show', short_help='shows the details of a NF package in an OSM repository') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.option('--repo', required=True, help='Repository name') @click.argument('name') -@click.option('--filter', +@click.option('--filter', default=None, multiple=True, help='filter by fields') @click.option('--version', default='latest', @@ -1203,7 +1249,8 @@ def vnfd_show4(ctx, name, repo, version, literal=None, filter=None): @click.argument('name') @click.option('--literal', is_flag=True, help='print literally, no pretty table') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context def ns_show(ctx, name, literal, filter): """shows the info of a NS instance @@ -1218,13 +1265,13 @@ def ns_show(ctx, name, literal, filter): # exit(1) if literal: - print(yaml.safe_dump(ns)) + print(yaml.safe_dump(ns, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) for k, v in list(ns.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)]) fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__ @@ -1232,7 +1279,7 @@ def ns_show(ctx, name, literal, filter): nsopdata = ctx.obj.ns.get_opdata(ns['id']) nsr_optdata = nsopdata['nsr:nsr'] for k, v in list(nsr_optdata.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, wrap_text(json.dumps(v, indent=2),width=100)]) table.align = 'l' print(table) @@ -1242,7 +1289,8 @@ def ns_show(ctx, name, literal, filter): @click.argument('name') @click.option('--literal', is_flag=True, help='print literally, no pretty table') -@click.option('--filter', default=None, help='restricts the information to the fields in the filter') +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.option('--kdu', default=None, help='KDU name (whose status will be shown)') @click.pass_context def vnf_show(ctx, name, literal, filter, kdu): @@ -1305,15 +1353,16 @@ def vnf_show(ctx, name, literal, filter, kdu): time.sleep(5) t += 5 print ("Could not determine KDU status") + return if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) for k, v in list(resp.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, wrap_text(text=json.dumps(v,indent=2),width=100)]) table.align = 'l' print(table) @@ -1370,7 +1419,8 @@ def vnf_show(ctx, name, literal, filter, kdu): @cli_osm.command(name='ns-op-show', short_help='shows the info of a NS operation') @click.argument('id') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @click.pass_context @@ -1388,12 +1438,12 @@ def ns_op_show(ctx, id, filter, literal): # exit(1) if literal: - print(yaml.safe_dump(op_info)) + print(yaml.safe_dump(op_info, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) for k, v in list(op_info.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, wrap_text(json.dumps(v, indent=2), 100)]) table.align = 'l' print(table) @@ -1410,7 +1460,7 @@ def nst_show(ctx, name, literal): # exit(1) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) @@ -1458,13 +1508,13 @@ def nsi_show(ctx, name, literal, filter): # exit(1) if literal: - print(yaml.safe_dump(nsi)) + print(yaml.safe_dump(nsi, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) for k, v in list(nsi.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, json.dumps(v, indent=2)]) table.align = 'l' @@ -1475,7 +1525,8 @@ def nsi_show(ctx, name, literal, filter): @click.argument('name') @click.option('--literal', is_flag=True, help='print literally, no pretty table') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context def nsi_show1(ctx, name, literal, filter): """shows the content of a Network Slice Instance (NSI) @@ -1490,7 +1541,8 @@ def nsi_show1(ctx, name, literal, filter): @click.argument('name') @click.option('--literal', is_flag=True, help='print literally, no pretty table') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context def nsi_show2(ctx, name, literal, filter): """shows the content of a Network Slice Instance (NSI) @@ -1512,7 +1564,7 @@ def nsi_op_show(ctx, id, filter): table = PrettyTable(['field', 'value']) for k, v in list(op_info.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, json.dumps(v, indent=2)]) table.align = 'l' print(table) @@ -1520,7 +1572,8 @@ def nsi_op_show(ctx, id, filter): @cli_osm.command(name='nsi-op-show', short_help='shows the info of an operation over a Network Slice Instance(NSI)') @click.argument('id') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context def nsi_op_show1(ctx, id, filter): """shows the info of an operation over a Network Slice Instance(NSI) @@ -1533,7 +1586,8 @@ def nsi_op_show1(ctx, id, filter): @cli_osm.command(name='netslice-instance-op-show', short_help='shows the info of an operation over a Network Slice Instance(NSI)') @click.argument('id') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context def nsi_op_show2(ctx, id, filter): """shows the info of an operation over a Network Slice Instance(NSI) @@ -1548,7 +1602,8 @@ def nsi_op_show2(ctx, id, filter): @click.argument('name') @click.option('--literal', is_flag=True, help='print literally, no pretty table') -@click.option('--filter', default=None) +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context def pdu_show(ctx, name, literal, filter): """shows the content of a Physical Deployment Unit (PDU) @@ -1564,13 +1619,13 @@ def pdu_show(ctx, name, literal, filter): # exit(1) if literal: - print(yaml.safe_dump(pdu)) + print(yaml.safe_dump(pdu, indent=4, default_flow_style=False)) return table = PrettyTable(['field', 'value']) for k, v in list(pdu.items()): - if filter is None or filter in k: + if not filter or k in filter: table.add_row([k, json.dumps(v, indent=2)]) table.align = 'l' @@ -2516,7 +2571,7 @@ def vim_delete(ctx, name, force, wait): #@click.option('--ro_update/--no_ro_update', # default=False, # help='update list from RO') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the VIM accounts matching the filter') @click.option('--long', is_flag=True, help='get more details of the NS (project, vim, deployment status, configuration status.') @@ -2525,6 +2580,7 @@ def vim_list(ctx, filter, long): """list all VIM accounts""" logger.debug("") if filter: + filter='&'.join(filter) check_client_version(ctx.obj, '--filter') # if ro_update: # check_client_version(ctx.obj, '--ro_update', 'v1') @@ -2535,40 +2591,35 @@ def vim_list(ctx, filter, long): # resp = ctx.obj.vim.list(ro_update) if long: table = PrettyTable(['vim name', 'uuid', 'project', 'operational state', 'error details']) + project_list = ctx.obj.project.list() else: - table = PrettyTable(['vim name', 'uuid']) + table = PrettyTable(['vim name', 'uuid', 'operational state']) for vim in resp: if long: - vim_details = ctx.obj.vim.get(vim['uuid']) - if 'vim_password' in vim_details: - vim_details['vim_password']='********' - logger.debug('VIM details: {}'.format(yaml.safe_dump(vim_details))) - vim_state = vim_details['_admin'].get('operationalState', '-') + if 'vim_password' in vim: + vim['vim_password']='********' + logger.debug('VIM details: {}'.format(yaml.safe_dump(vim))) + vim_state = vim['_admin'].get('operationalState', '-') error_details = 'N/A' if vim_state == 'ERROR': - error_details = vim_details['_admin'].get('detailed-status', 'Not found') - project_list = ctx.obj.project.list() - vim_project_list = vim_details.get('_admin').get('projects_read') - project_id = 'None' - project_name = 'None' - if vim_project_list: - project_id = vim_project_list[0] - for p in project_list: - if p['_id'] == project_id: - project_name = p['name'] - break - table.add_row([vim['name'], vim['uuid'], '{} ({})'.format(project_name, project_id), + error_details = vim['_admin'].get('detailed-status', 'Not found') + project_id, project_name = get_project(project_list, vim) + #project_info = '{} ({})'.format(project_name, project_id) + project_info = project_name + table.add_row([vim['name'], vim['uuid'], project_info, vim_state, wrap_text(text=error_details, width=80)]) else: - table.add_row([vim['name'], vim['uuid']]) + table.add_row([vim['name'], vim['uuid'], vim['_admin'].get('operationalState', '-')]) table.align = 'l' print(table) @cli_osm.command(name='vim-show', short_help='shows the details of a VIM account') @click.argument('name') +@click.option('--filter', multiple=True, + help='restricts the information to the fields in the filter') @click.pass_context -def vim_show(ctx, name): +def vim_show(ctx, name, filter): """shows the details of a VIM account NAME: name or ID of the VIM account @@ -2584,7 +2635,8 @@ def vim_show(ctx, name): table = PrettyTable(['key', 'attribute']) for k, v in list(resp.items()): - table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)]) + if not filter or k in filter: + table.add_row([k, wrap_text(text=json.dumps(v, indent=2),width=100)]) table.align = 'l' print(table) @@ -2732,7 +2784,7 @@ def wim_delete(ctx, name, force, wait): @cli_osm.command(name='wim-list', short_help='list all WIM accounts') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the WIM accounts matching the filter') @click.pass_context def wim_list(ctx, filter): @@ -2740,6 +2792,8 @@ def wim_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.wim.list(filter) table = PrettyTable(['wim name', 'uuid']) for wim in resp: @@ -2907,7 +2961,7 @@ def sdnc_delete(ctx, name, force, wait): @cli_osm.command(name='sdnc-list', short_help='list all SDN controllers') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help="restricts the list to the SDN controllers matching the filter with format: 'k[.k..]=v[&k[.k]=v2]'") @click.pass_context def sdnc_list(ctx, filter): @@ -2915,6 +2969,8 @@ def sdnc_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.sdnc.list(filter) # except ClientException as e: # print(str(e)) @@ -3080,24 +3136,53 @@ def k8scluster_delete(ctx, name, force): @cli_osm.command(name='k8scluster-list') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the K8s clusters matching the filter') @click.option('--literal', is_flag=True, help='print literally, no pretty table') +@click.option('--long', is_flag=True, help='get more details') @click.pass_context -def k8scluster_list(ctx, filter, literal): +def k8scluster_list(ctx, filter, literal, long): """list all K8s clusters""" # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.k8scluster.list(filter) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return - table = PrettyTable(['Name', 'Id', 'Version', 'VIM', 'K8s-nets', 'Operational State', 'Description']) + if long: + table = PrettyTable(['Name', 'Id', 'Project', 'Version', 'VIM', 'K8s-nets', + 'Operational State', 'Op. state (details)', 'Description', 'Detailed status']) + project_list = ctx.obj.project.list() + else: + table = PrettyTable(['Name', 'Id', 'VIM', 'Operational State', 'Op. state details']) + try: + vim_list = ctx.obj.vim.list() + except: + vim_list = [] for cluster in resp: - table.add_row([cluster['name'], cluster['_id'], cluster['k8s_version'], cluster['vim_account'], - json.dumps(cluster['nets']), cluster["_admin"]["operationalState"], - trunc_text(cluster.get('description') or '', 40)]) + 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", "-")) + if long: + project_id, project_name = get_project(project_list, cluster) + #project_info = '{} ({})'.format(project_name, project_id) + project_info = project_name + detailed_status = cluster["_admin"].get("detailed-status","-") + table.add_row([cluster['name'], cluster['_id'], project_info, + cluster['k8s_version'], vim_info, + json.dumps(cluster['nets']), cluster["_admin"]["operationalState"], + op_state_details, trunc_text(cluster.get('description') or '', 40), + wrap_text(text=detailed_status, width=40)]) + else: + table.add_row([cluster['name'], cluster['_id'], vim_info, + cluster["_admin"]["operationalState"], op_state_details]) table.align = 'l' print(table) # except ClientException as e: @@ -3118,7 +3203,7 @@ def k8scluster_show(ctx, name, literal): # try: resp = ctx.obj.k8scluster.get(name) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['key', 'attribute']) for k, v in list(resp.items()): @@ -3246,7 +3331,7 @@ def repo_delete(ctx, name, force): @cli_osm.command(name='repo-list') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the repos matching the filter') @click.option('--literal', is_flag=True, help='print literally, no pretty table') @@ -3256,10 +3341,12 @@ def repo_list(ctx, filter, literal): # try: # K8s Repositories check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.repo.list(filter) resp += ctx.obj.osmrepo.list(filter) if literal: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['Name', 'Id', 'Type', 'URI', 'Description']) for repo in resp: @@ -3290,7 +3377,7 @@ def repo_show(ctx, name, literal): if literal: if resp: - print(yaml.safe_dump(resp)) + print(yaml.safe_dump(resp, indent=4, default_flow_style=False)) return table = PrettyTable(['key', 'attribute']) if resp: @@ -3377,7 +3464,7 @@ def project_delete(ctx, name): @cli_osm.command(name='project-list', short_help='list all projects') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the projects matching the filter') @click.pass_context def project_list(ctx, filter): @@ -3385,6 +3472,8 @@ def project_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.project.list(filter) # except ClientException as e: # print(str(e)) @@ -3575,13 +3664,15 @@ def user_delete(ctx, name): @cli_osm.command(name='user-list', short_help='list all users') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the users matching the filter') @click.pass_context def user_list(ctx, filter): """list all users""" # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.user.list(filter) # except ClientException as e: # print(str(e)) @@ -3919,7 +4010,7 @@ def upload_package(ctx, filename, skip_charm_build): @click.option('--vnf_name', default=None, help='member-vnf-index if the target is a vnf instead of a ns)') @click.option('--kdu_name', default=None, help='kdu-name if the target is a kdu)') @click.option('--vdu_id', default=None, help='vdu-id if the target is a vdu') -@click.option('--vdu_count', default=None, help='number of vdu instance of this vdu_id') +@click.option('--vdu_count', default=None, type=int, help='number of vdu instance of this vdu_id') @click.option('--action_name', prompt=True, help='action name') @click.option('--params', default=None, help='action params in YAML/JSON inline string') @click.option('--params_file', default=None, help='YAML/JSON file with action params') @@ -3955,7 +4046,7 @@ def ns_action(ctx, op_data['kdu_name'] = kdu_name if vdu_id: op_data['vdu_id'] = vdu_id - if vdu_count: + if vdu_count is not None: op_data['vdu_count_index'] = vdu_count if timeout: op_data['timeout_ns_action'] = timeout @@ -4092,7 +4183,7 @@ def role_delete(ctx, name): @cli_osm.command(name='role-list', short_help='list all roles') -@click.option('--filter', default=None, +@click.option('--filter', default=None, multiple=True, help='restricts the list to the projects matching the filter') @click.pass_context def role_list(ctx, filter): @@ -4102,6 +4193,8 @@ def role_list(ctx, filter): logger.debug("") # try: check_client_version(ctx.obj, ctx.command.name) + if filter: + filter='&'.join(filter) resp = ctx.obj.role.list(filter) # except ClientException as e: # print(str(e))