X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fscripts%2Fosm.py;h=e5be118821a0caf875293aa5edc3ab83fb2f5c62;hb=d1ccf0e88aeacb065b97a6c5cb82e6de53537b94;hp=d0d78f52b65e4e5421f54fdc3bc6678b36ce07df;hpb=6380fc0aa54cc7c3700a5a1f655e619471684611;p=osm%2Fosmclient.git diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index d0d78f5..e5be118 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -1,5 +1,4 @@ -# Copyright 2017 Sandvine -# +# Copyright ETSI Contributors and Others. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -13,496 +12,301 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -""" -OSM shell/cli -""" import click -from osmclient.client import client +from osmclient import client from osmclient.common.exceptions import ClientException -from prettytable import PrettyTable -import json -import time - - -@click.group() -@click.option('--hostname', - default=None, - envvar='OSM_HOSTNAME', - help='hostname of server. ' + - 'Also can set OSM_HOSTNAME in environment') -@click.option('--so-port', - default=8008, - envvar='OSM_SO_PORT', - help='hostname of server. ' + - 'Also can set OSM_SO_PORT in environment') -@click.option('--so-project', - default='default', - envvar='OSM_SO_PROJECT', - help='Project Name in SO. ' + - 'Also can set OSM_SO_PROJECT in environment') -@click.option('--ro-hostname', - default=None, - envvar='OSM_RO_HOSTNAME', - help='hostname of RO server. ' + - 'Also can set OSM_RO_HOSTNAME in environment') -@click.option('--ro-port', - default=9090, - envvar='OSM_RO_PORT', - help='hostname of RO server. ' + - 'Also can set OSM_RO_PORT in environment') +from osmclient.cli_commands import ( + alarms, + k8scluster, + metrics, + netslice_instance, + netslice_ops, + netslice_template, + nfpkg, + ns, + nslcm_ops, + nslcm, + nspkg, + other, + packages, + pdus, + rbac, + repo, + sdnc, + subscriptions, + vca, + vim, + vnf, + wim, +) +import yaml +import pycurl +import os +import logging + + +@click.group( + context_settings=dict(help_option_names=["-h", "--help"], max_content_width=160) +) +@click.option( + "--hostname", + default="127.0.0.1", + envvar="OSM_HOSTNAME", + help="hostname of server. " + "Also can set OSM_HOSTNAME in environment", +) +@click.option( + "--user", + default=None, + envvar="OSM_USER", + help="user (defaults to admin). " + "Also can set OSM_USER in environment", +) +@click.option( + "--password", + default=None, + envvar="OSM_PASSWORD", + help="password (defaults to admin). " + "Also can set OSM_PASSWORD in environment", +) +@click.option( + "--project", + default=None, + envvar="OSM_PROJECT", + help="project (defaults to admin). " + "Also can set OSM_PROJECT in environment", +) +@click.option( + "-v", + "--verbose", + count=True, + help="increase verbosity (-v INFO, -vv VERBOSE, -vvv DEBUG)", +) +@click.option("--all-projects", default=None, is_flag=True, help="include all projects") +@click.option( + "--public/--no-public", + default=None, + help="flag for public items (packages, instances, VIM accounts, etc.)", +) +@click.option( + "--project-domain-name", + "project_domain_name", + default=None, + envvar="OSM_PROJECT_DOMAIN_NAME", + help="project domain name for keystone authentication (default to None). " + + "Also can set OSM_PROJECT_DOMAIN_NAME in environment", +) +@click.option( + "--user-domain-name", + "user_domain_name", + default=None, + envvar="OSM_USER_DOMAIN_NAME", + help="user domain name for keystone authentication (default to None). " + + "Also can set OSM_USER_DOMAIN_NAME in environment", +) @click.pass_context -def cli(ctx, hostname, so_port, so_project, ro_hostname, ro_port): +def cli_osm(ctx, **kwargs): + global logger + hostname = kwargs.pop("hostname", None) if hostname is None: print( - "either hostname option or OSM_HOSTNAME " + - "environment variable needs to be specified") - exit(1) - ctx.obj = client.Client( - host=hostname, - so_port=so_port, - so_project=so_project, - ro_host=ro_hostname, - ro_port=ro_port) - - -@cli.command(name='ns-list') -@click.pass_context -def ns_list(ctx): - resp = ctx.obj.ns.list() - table = PrettyTable( - ['ns instance name', - 'id', - 'operational status', - 'config status']) - for ns in resp: - nsopdata = ctx.obj.ns.get_opdata(ns['id']) - nsr = nsopdata['nsr:nsr'] - table.add_row( - [nsr['name-ref'], - nsr['ns-instance-config-ref'], - nsr['operational-status'], - nsr['config-status']]) - table.align = 'l' - print(table) - - -@cli.command(name='nsd-list') -@click.pass_context -def nsd_list(ctx): - resp = ctx.obj.nsd.list() - table = PrettyTable(['nsd name', 'id']) - for ns in resp: - table.add_row([ns['name'], ns['id']]) - table.align = 'l' - print(table) - - -@cli.command(name='vnfd-list') -@click.pass_context -def vnfd_list(ctx): - resp = ctx.obj.vnfd.list() - table = PrettyTable(['vnfd name', 'id']) - for vnfd in resp: - table.add_row([vnfd['name'], vnfd['id']]) - table.align = 'l' - print(table) - - -@cli.command(name='vnf-list') -@click.pass_context -def vnf_list(ctx): - resp = ctx.obj.vnf.list() - table = PrettyTable( - ['vnf name', - 'id', - 'operational status', - 'config status']) - for vnfr in resp: - if 'mgmt-interface' not in vnfr: - vnfr['mgmt-interface'] = {} - vnfr['mgmt-interface']['ip-address'] = None - table.add_row( - [vnfr['name'], - vnfr['id'], - vnfr['operational-status'], - vnfr['config-status']]) - table.align = 'l' - print(table) - - -@cli.command(name='vnf-show') -@click.argument('vnf_name') -@click.option('--filter', default=None) -@click.pass_context -def vnf_show(ctx, vnf_name, filter): - try: - resp = ctx.obj.vnf.get(vnf_name) - except ClientException as inst: - print(inst.message) - exit(1) - - table = PrettyTable(['field', 'value']) - for k, v in resp.items(): - if filter is None or filter in k: - table.add_row([k, json.dumps(v, indent=2)]) - table.align = 'l' - print(table) - - -@cli.command(name='vnf-monitoring-show') -@click.argument('vnf_name') -@click.pass_context -def vnf_monitoring_show(ctx, vnf_name): - try: - resp = ctx.obj.vnf.get_monitoring(vnf_name) - except ClientException as inst: - print(inst.message) - exit(1) - - table = PrettyTable(['vnf name', 'monitoring name', 'value', 'units']) - if resp is not None: - for monitor in resp: - table.add_row( - [vnf_name, - monitor['name'], - monitor['value-integer'], - monitor['units']]) - table.align = 'l' - print(table) - - -@cli.command(name='ns-monitoring-show') -@click.argument('ns_name') -@click.pass_context -def ns_monitoring_show(ctx, ns_name): - try: - resp = ctx.obj.ns.get_monitoring(ns_name) - except ClientException as inst: - print(inst.message) - exit(1) - - table = PrettyTable(['vnf name', 'monitoring name', 'value', 'units']) - for key, val in resp.items(): - for monitor in val: - table.add_row( - [key, - monitor['name'], - monitor['value-integer'], - monitor['units']]) - table.align = 'l' - print(table) - - -@cli.command(name='ns-create') -@click.option('--ns_name', - prompt=True) -@click.option('--nsd_name', - prompt=True) -@click.option('--vim_account', - prompt=True) -@click.option('--admin_status', - default='ENABLED', - help='administration status') -@click.option('--ssh_keys', - default=None, - help='comma separated list of keys to inject to vnfs') -@click.option('--config', - default=None, - help='ns specific yaml configuration') -@click.pass_context -def ns_create(ctx, - nsd_name, - ns_name, - vim_account, - admin_status, - ssh_keys, - config): - try: - ctx.obj.ns.create( - nsd_name, - ns_name, - config=config, - ssh_keys=ssh_keys, - account=vim_account) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='ns-delete') -@click.argument('ns_name') -@click.pass_context -def ns_delete(ctx, ns_name): - try: - ctx.obj.ns.delete(ns_name) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='upload-package') -@click.argument('filename') -@click.pass_context -def upload_package(ctx, filename): - try: - ctx.obj.package.upload(filename) - ctx.obj.package.wait_for_upload(filename) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='ns-show') -@click.argument('ns_name') -@click.option('--filter', default=None) -@click.pass_context -def ns_show(ctx, ns_name, filter): - try: - ns = ctx.obj.ns.get(ns_name) - except ClientException as inst: - print(inst.message) - exit(1) - - table = PrettyTable(['field', 'value']) - - for k, v in ns.items(): - if filter is None or filter in k: - table.add_row([k, json.dumps(v, indent=2)]) - - nsopdata = ctx.obj.ns.get_opdata(ns['id']) - nsr_optdata = nsopdata['nsr:nsr'] - for k, v in nsr_optdata.items(): - if filter is None or filter in k: - table.add_row([k, json.dumps(v, indent=2)]) - table.align = 'l' - print(table) - - -@cli.command(name='ns-scaling-show') -@click.argument('ns_name') -@click.pass_context -def show_ns_scaling(ctx, ns_name): - resp = ctx.obj.ns.list() - - table = PrettyTable( - ['group-name', - 'instance-id', - 'operational status', - 'create-time', - 'vnfr ids']) - - for ns in resp: - if ns_name == ns['name']: - nsopdata = ctx.obj.ns.get_opdata(ns['id']) - scaling_records = nsopdata['nsr:nsr']['scaling-group-record'] - for record in scaling_records: - if 'instance' in record: - instances = record['instance'] - for inst in instances: - table.add_row( - [record['scaling-group-name-ref'], - inst['instance-id'], - inst['op-status'], - time.strftime('%Y-%m-%d %H:%M:%S', - time.localtime( - inst['create-time'])), - inst['vnfrs']]) - table.align = 'l' - print(table) - - -@cli.command(name='ns-scale') -@click.argument('ns_name') -@click.option('--ns_scale_group', prompt=True) -@click.option('--index', prompt=True) -@click.pass_context -def ns_scale(ctx, ns_name, ns_scale_group, index): - ctx.obj.ns.scale(ns_name, ns_scale_group, index) - - -@cli.command(name='nsd-delete') -@click.argument('nsd_name') -@click.pass_context -def nsd_delete(ctx, nsd_name): - try: - ctx.obj.nsd.delete(nsd_name) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='vnfd-delete') -@click.argument('vnfd_name') -@click.pass_context -def vnfd_delete(ctx, vnfd_name): - try: - ctx.obj.vnfd.delete(vnfd_name) - except ClientException as inst: - print(inst.message) + ( + "either hostname option or OSM_HOSTNAME " + + "environment variable needs to be specified" + ) + ) exit(1) + # Remove None values + kwargs = {k: v for k, v in kwargs.items() if v is not None} + sol005 = os.getenv("OSM_SOL005", True) + ctx.obj = client.Client(host=hostname, sol005=sol005, **kwargs) + logger = logging.getLogger("osmclient") -@cli.command(name='config-agent-list') -@click.pass_context -def config_agent_list(ctx): - table = PrettyTable(['name', 'account-type', 'details']) - for account in ctx.obj.vca.list(): - table.add_row( - [account['name'], - account['account-type'], - account['juju']]) - table.align = 'l' - print(table) - - -@cli.command(name='config-agent-delete') -@click.argument('name') -@click.pass_context -def config_agent_delete(ctx, name): - try: - ctx.obj.vca.delete(name) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='config-agent-add') -@click.option('--name', - prompt=True) -@click.option('--account_type', - prompt=True) -@click.option('--server', - prompt=True) -@click.option('--user', - prompt=True) -@click.option('--secret', - prompt=True, - hide_input=True, - confirmation_prompt=True) -@click.pass_context -def config_agent_add(ctx, name, account_type, server, user, secret): - try: - ctx.obj.vca.create(name, account_type, server, user, secret) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='vim-create') -@click.option('--name', - prompt=True, - help='Name to create datacenter') -@click.option('--user', - prompt=True, - help='VIM username') -@click.option('--password', - prompt=True, - hide_input=True, - confirmation_prompt=True, - help='VIM password') -@click.option('--auth_url', - prompt=True, - help='VIM connector url') -@click.option('--tenant', - prompt=True, - help='tenant name') -@click.option('--config', - default=None, - help='VIM specific config parameters') -@click.option('--account_type', - default='openstack', - help='VIM type') -@click.option('--description', - default='no description') -@click.pass_context -def vim_create(ctx, - name, - user, - password, - auth_url, - tenant, - config, - account_type, - description): - vim = {} - vim['vim-username'] = user - vim['vim-password'] = password - vim['vim-url'] = auth_url - vim['vim-tenant-name'] = tenant - vim['config'] = config - vim['vim-type'] = account_type - vim['description'] = description +# pylint: disable=no-value-for-parameter +def cli(): try: - ctx.obj.vim.create(name, vim) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='vim-delete') -@click.argument('name') -@click.pass_context -def vim_delete(ctx, name): - try: - ctx.obj.vim.delete(name) - except ClientException as inst: - print(inst.message) - exit(1) - - -@cli.command(name='vim-list') -@click.option('--ro_update/--no_ro_update', - default=False, - help='update list from RO') -@click.pass_context -def vim_list(ctx, ro_update): - resp = ctx.obj.vim.list(ro_update) - table = PrettyTable(['vim name', 'uuid']) - for vim in resp: - table.add_row([vim['name'], vim['uuid']]) - table.align = 'l' - print(table) - - -@cli.command(name='vim-show') -@click.argument('name') -@click.pass_context -def vim_show(ctx, name): - try: - resp = ctx.obj.vim.get(name) - except ClientException as inst: - print(inst.message) - exit(1) - - table = PrettyTable(['key', 'attribute']) - for k, v in resp.items(): - table.add_row([k, json.dumps(v, indent=2)]) - table.align = 'l' - print(table) - - -@cli.command(name='ro-dump') -@click.pass_context -def ro_dump(ctx): - resp = ctx.obj.vim.get_resource_orchestrator() - table = PrettyTable(['key', 'attribute']) - for k, v in resp.items(): - table.add_row([k, json.dumps(v, indent=2)]) - table.align = 'l' - print(table) - - -@cli.command(name='vcs-list') -@click.pass_context -def vcs_list(ctx): - resp = ctx.obj.utils.get_vcs_info() - table = PrettyTable(['component name', 'state']) - for component in resp: - table.add_row([component['component_name'], component['state']]) - table.align = 'l' - print(table) - - -if __name__ == '__main__': + cli_osm.add_command(alarms.alarm_list) + cli_osm.add_command(alarms.alarm_show) + cli_osm.add_command(alarms.alarm_update) + cli_osm.add_command(alarms.ns_alarm_create) + + cli_osm.add_command(k8scluster.k8scluster_add) + cli_osm.add_command(k8scluster.k8scluster_delete) + cli_osm.add_command(k8scluster.k8scluster_list) + cli_osm.add_command(k8scluster.k8scluster_show) + cli_osm.add_command(k8scluster.k8scluster_update) + + cli_osm.add_command(metrics.ns_metric_export) + + cli_osm.add_command(k8scluster.k8scluster_delete) + cli_osm.add_command(k8scluster.k8scluster_list) + cli_osm.add_command(k8scluster.k8scluster_show) + cli_osm.add_command(k8scluster.k8scluster_update) + + cli_osm.add_command(netslice_instance.nsi_create1) + cli_osm.add_command(netslice_instance.nsi_create2) + cli_osm.add_command(netslice_instance.nsi_delete1) + cli_osm.add_command(netslice_instance.nsi_delete2) + cli_osm.add_command(netslice_instance.nsi_list1) + cli_osm.add_command(netslice_instance.nsi_list2) + cli_osm.add_command(netslice_instance.nsi_show1) + cli_osm.add_command(netslice_instance.nsi_show2) + + cli_osm.add_command(netslice_ops.nsi_op_list1) + cli_osm.add_command(netslice_ops.nsi_op_list2) + cli_osm.add_command(netslice_ops.nsi_op_show1) + cli_osm.add_command(netslice_ops.nsi_op_show2) + + cli_osm.add_command(netslice_template.nst_create1) + cli_osm.add_command(netslice_template.nst_create2) + cli_osm.add_command(netslice_template.nst_delete1) + cli_osm.add_command(netslice_template.nst_delete2) + cli_osm.add_command(netslice_template.nst_list1) + cli_osm.add_command(netslice_template.nst_list2) + cli_osm.add_command(netslice_template.nst_show1) + cli_osm.add_command(netslice_template.nst_show2) + cli_osm.add_command(netslice_template.nst_update1) + cli_osm.add_command(netslice_template.nst_update2) + + cli_osm.add_command(nfpkg.nfpkg_create) + cli_osm.add_command(nfpkg.nfpkg_delete) + cli_osm.add_command(nfpkg.nfpkg_list) + cli_osm.add_command(nfpkg.nfpkg_show) + cli_osm.add_command(nfpkg.nfpkg_update) + cli_osm.add_command(nfpkg.vnfd_create1) + cli_osm.add_command(nfpkg.vnfd_create2) + cli_osm.add_command(nfpkg.vnfd_delete1) + cli_osm.add_command(nfpkg.vnfd_delete2) + cli_osm.add_command(nfpkg.vnfd_list1) + cli_osm.add_command(nfpkg.vnfd_list2) + cli_osm.add_command(nfpkg.vnfd_show1) + cli_osm.add_command(nfpkg.vnfd_show2) + cli_osm.add_command(nfpkg.vnfd_update1) + cli_osm.add_command(nfpkg.vnfd_update2) + + cli_osm.add_command(ns.ns_create) + cli_osm.add_command(ns.ns_delete) + cli_osm.add_command(ns.ns_list) + cli_osm.add_command(ns.ns_show) + + cli_osm.add_command(nslcm_ops.ns_op_list) + cli_osm.add_command(nslcm_ops.ns_op_show) + + cli_osm.add_command(nslcm.ns_action) + cli_osm.add_command(nslcm.vnf_scale) + cli_osm.add_command(nslcm.ns_update) + cli_osm.add_command(nslcm.ns_heal) + cli_osm.add_command(nslcm.vnf_heal) + + cli_osm.add_command(nspkg.nsd_create1) + cli_osm.add_command(nspkg.nsd_create2) + cli_osm.add_command(nspkg.nsd_delete1) + cli_osm.add_command(nspkg.nsd_delete2) + cli_osm.add_command(nspkg.nsd_list1) + cli_osm.add_command(nspkg.nsd_list2) + cli_osm.add_command(nspkg.nsd_show1) + cli_osm.add_command(nspkg.nsd_show2) + cli_osm.add_command(nspkg.nsd_update1) + cli_osm.add_command(nspkg.nsd_update2) + + cli_osm.add_command(other.get_version) + + cli_osm.add_command(packages.descriptor_translate) + cli_osm.add_command(packages.package_build) + cli_osm.add_command(packages.package_create) + cli_osm.add_command(packages.package_translate) + cli_osm.add_command(packages.package_validate) + cli_osm.add_command(packages.upload_package) + + cli_osm.add_command(pdus.pdu_create) + cli_osm.add_command(pdus.pdu_delete) + cli_osm.add_command(pdus.pdu_list) + cli_osm.add_command(pdus.pdu_show) + cli_osm.add_command(pdus.pdu_update) + + cli_osm.add_command(rbac.project_create) + cli_osm.add_command(rbac.project_delete) + cli_osm.add_command(rbac.project_list) + cli_osm.add_command(rbac.project_show) + cli_osm.add_command(rbac.project_update) + + cli_osm.add_command(rbac.role_create) + cli_osm.add_command(rbac.role_delete) + cli_osm.add_command(rbac.role_list) + cli_osm.add_command(rbac.role_show) + cli_osm.add_command(rbac.role_update) + + cli_osm.add_command(rbac.user_create) + cli_osm.add_command(rbac.user_delete) + cli_osm.add_command(rbac.user_list) + cli_osm.add_command(rbac.user_show) + cli_osm.add_command(rbac.user_update) + + cli_osm.add_command(repo.repo_add) + cli_osm.add_command(repo.repo_delete) + cli_osm.add_command(repo.repo_list) + cli_osm.add_command(repo.repo_show) + cli_osm.add_command(repo.repo_update) + + cli_osm.add_command(repo.repo_index) + cli_osm.add_command(repo.nfpkg_repo_list1) + cli_osm.add_command(repo.nfpkg_repo_list2) + cli_osm.add_command(repo.nfpkg_repo_list2) + cli_osm.add_command(repo.nspkg_repo_list) + cli_osm.add_command(repo.nspkg_repo_list2) + cli_osm.add_command(repo.nsd_repo_show) + cli_osm.add_command(repo.nsd_repo_show2) + cli_osm.add_command(repo.vnfd_show1) + cli_osm.add_command(repo.vnfd_show2) + + cli_osm.add_command(sdnc.sdnc_create) + cli_osm.add_command(sdnc.sdnc_delete) + cli_osm.add_command(sdnc.sdnc_list) + cli_osm.add_command(sdnc.sdnc_show) + cli_osm.add_command(sdnc.sdnc_update) + + cli_osm.add_command(subscriptions.subscription_create) + cli_osm.add_command(subscriptions.subscription_delete) + cli_osm.add_command(subscriptions.subscription_list) + cli_osm.add_command(subscriptions.subscription_show) + + cli_osm.add_command(vca.vca_add) + cli_osm.add_command(vca.vca_delete) + cli_osm.add_command(vca.vca_list) + cli_osm.add_command(vca.vca_show) + cli_osm.add_command(vca.vca_update) + + cli_osm.add_command(vim.vim_create) + cli_osm.add_command(vim.vim_delete) + cli_osm.add_command(vim.vim_list) + cli_osm.add_command(vim.vim_show) + cli_osm.add_command(vim.vim_update) + + cli_osm.add_command(vnf.nf_list) + cli_osm.add_command(vnf.vnf_list1) + cli_osm.add_command(vnf.vnf_show) + + cli_osm.add_command(wim.wim_create) + cli_osm.add_command(wim.wim_delete) + cli_osm.add_command(wim.wim_list) + cli_osm.add_command(wim.wim_show) + cli_osm.add_command(wim.wim_update) + + cli_osm() + exit(0) + except pycurl.error as exc: + print(exc) + print( + 'Maybe "--hostname" option or OSM_HOSTNAME environment variable needs to be specified' + ) + except ClientException as exc: + print("ERROR: {}".format(exc)) + except (FileNotFoundError, PermissionError) as exc: + print("Cannot open file: {}".format(exc)) + except yaml.YAMLError as exc: + print("Invalid YAML format: {}".format(exc)) + exit(1) + # TODO capture other controlled exceptions here + # TODO remove the ClientException captures from all places, unless they do something different + + +if __name__ == "__main__": cli()